-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathparser.js
More file actions
297 lines (257 loc) · 7.52 KB
/
parser.js
File metadata and controls
297 lines (257 loc) · 7.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
export const useParserStore = defineStore('parser', () => {
// 解析列表
const parsers = ref([])
// 加载状态
const loading = ref(false)
// 错误信息
const error = ref(null)
// 计算属性
const enabledParsers = computed(() =>
parsers.value.filter(parser => parser.enabled !== false)
)
const disabledParsers = computed(() =>
parsers.value.filter(parser => parser.enabled === false)
)
const parserCount = computed(() => parsers.value.length)
// 从配置地址加载解析列表
const loadParsersFromConfig = async (configUrl) => {
loading.value = true
error.value = null
try {
const response = await fetch(configUrl)
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`)
}
const data = await response.json()
if (data.parses && Array.isArray(data.parses)) {
// 为每个解析器添加唯一ID和启用状态
parsers.value = data.parses.map((parser, index) => ({
...parser,
id: parser.id || `parser_${Date.now()}_${index}`,
enabled: parser.enabled !== false, // 默认启用
order: index
}))
// 保存到本地存储
saveToLocalStorage()
return true
} else {
throw new Error('配置数据格式错误:缺少parses字段')
}
} catch (err) {
error.value = err.message
console.error('加载解析配置失败:', err)
return false
} finally {
loading.value = false
}
}
// 从本地存储加载
const loadFromLocalStorage = () => {
try {
const stored = localStorage.getItem('drplayer_parsers')
if (stored) {
const data = JSON.parse(stored)
if (Array.isArray(data)) {
parsers.value = data
return true
}
}
} catch (err) {
console.error('从本地存储加载解析配置失败:', err)
}
return false
}
// 保存到本地存储
const saveToLocalStorage = () => {
try {
localStorage.setItem('drplayer_parsers', JSON.stringify(parsers.value))
} catch (err) {
console.error('保存解析配置到本地存储失败:', err)
}
}
// 添加解析器
const addParser = (parser) => {
const newParser = {
...parser,
id: `parser_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
enabled: true,
order: parsers.value.length
}
parsers.value.push(newParser)
saveToLocalStorage()
return newParser
}
// 更新解析器
const updateParser = (id, updates) => {
const index = parsers.value.findIndex(p => p.id === id)
if (index !== -1) {
parsers.value[index] = { ...parsers.value[index], ...updates }
saveToLocalStorage()
return true
}
return false
}
// 删除解析器
const deleteParser = (id) => {
const index = parsers.value.findIndex(p => p.id === id)
if (index !== -1) {
parsers.value.splice(index, 1)
saveToLocalStorage()
return true
}
return false
}
// 切换启用状态
const toggleParser = (id) => {
const parser = parsers.value.find(p => p.id === id)
if (parser) {
parser.enabled = !parser.enabled
saveToLocalStorage()
return parser.enabled
}
return false
}
// 重新排序
const reorderParsers = (newOrder) => {
parsers.value = newOrder.map((parser, index) => ({
...parser,
order: index
}))
saveToLocalStorage()
}
// 根据ID映射重新排序(只更新参与拖拽的解析器)
const reorderParsersById = (orderMap) => {
// 创建一个新的解析器数组,保持原有的解析器不变
const updatedParsers = [...parsers.value]
// 只更新参与拖拽的解析器的order属性
updatedParsers.forEach(parser => {
if (orderMap.has(parser.id)) {
parser.order = orderMap.get(parser.id)
}
})
// 按order排序
updatedParsers.sort((a, b) => a.order - b.order)
// 重新分配连续的order值
updatedParsers.forEach((parser, index) => {
parser.order = index
})
parsers.value = updatedParsers
saveToLocalStorage()
}
// 测试解析器
const testParser = async (parser, testUrl) => {
try {
// 构建解析请求URL
const parseUrl = parser.url.replace(/\{url\}/g, encodeURIComponent(testUrl))
const response = await fetch(parseUrl, {
method: 'GET',
headers: parser.header || {},
timeout: 10000 // 10秒超时
})
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`)
}
const result = await response.text()
// 简单验证返回结果是否包含视频链接
const hasVideoUrl = /https?:\/\/[^\s]+\.(mp4|m3u8|flv)/i.test(result)
return {
success: true,
hasVideoUrl,
response: result,
message: hasVideoUrl ? '解析成功,检测到视频链接' : '解析完成,但未检测到视频链接'
}
} catch (err) {
return {
success: false,
error: err.message,
message: `解析失败: ${err.message}`
}
}
}
// 导出配置
const exportParsers = () => {
const exportData = {
parses: parsers.value.map(parser => ({
name: parser.name,
url: parser.url,
type: parser.type,
ext: parser.ext,
header: parser.header
})),
exportTime: new Date().toISOString(),
version: '1.0'
}
const blob = new Blob([JSON.stringify(exportData, null, 2)], {
type: 'application/json'
})
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = `drplayer_parsers_${new Date().toISOString().split('T')[0]}.json`
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
URL.revokeObjectURL(url)
}
// 导入配置
const importParsers = async (file) => {
try {
const text = await file.text()
const data = JSON.parse(text)
if (data.parses && Array.isArray(data.parses)) {
// 合并导入的解析器
const importedParsers = data.parses.map((parser, index) => ({
...parser,
id: `imported_${Date.now()}_${index}`,
enabled: true,
order: parsers.value.length + index
}))
parsers.value.push(...importedParsers)
saveToLocalStorage()
return { success: true, count: importedParsers.length }
} else {
throw new Error('导入文件格式错误:缺少parses字段')
}
} catch (err) {
return { success: false, error: err.message }
}
}
// 清空所有解析器
const clearAllParsers = () => {
parsers.value = []
saveToLocalStorage()
}
// 初始化时从本地存储加载
loadFromLocalStorage()
// loadParsers作为loadFromLocalStorage的别名,用于兼容性
const loadParsers = () => {
loadFromLocalStorage()
}
return {
// 状态
parsers,
loading,
error,
// 计算属性
enabledParsers,
disabledParsers,
parserCount,
// 方法
loadParsers,
loadParsersFromConfig,
loadFromLocalStorage,
saveToLocalStorage,
addParser,
updateParser,
deleteParser,
toggleParser,
reorderParsers,
reorderParsersById,
testParser,
exportParsers,
importParsers,
clearAllParsers
}
})