-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcleanM3u.js
More file actions
178 lines (175 loc) · 6.57 KB
/
cleanM3u.js
File metadata and controls
178 lines (175 loc) · 6.57 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
//UBO m3u-prune.js https://en.wikipedia.org/wiki/M3U
export default function() {
const items = [
'ryplay', //如意云
/^lz-?cdn/,'cdnlz',/^lz\d*uu/, //量子云
'ffzy', //非凡云
'bfikuncdn', //艾昆云
'modujx', //魔都云
/^high\d*-p/,'hdzyk',/^y[yz]zy/,'vipyz-' //神马云 high\d*-playback play-cdn
];
const hasFileExt = (url, ex) => {
return url.endsWith(ex) || url.includes(ex +'?', 22);
};
const urlFromArg = arg => {
if ( typeof arg === 'string' ) { return arg; }
if ( arg instanceof Request ) { return arg.url; }
return String(arg);
};
let iItem = -1; // 匹配项
const matchM3u = url => {
if (!hasFileExt(url, '.m3u8')) return !1;
const m = url.match(/^https?:\/\/([\w\-]+)\.([\w\-]+)(\.?)/);
if (m[1].startsWith('yzzy')) iItem = 9;//神马云
else {
const u = m[3] ? m[2] : m[1];
iItem = items.findIndex(k => k instanceof RegExp ? k.test(u) : u.startsWith(k));
}
return true;
};
const pruner = (text) => {
let iDouhao = 0, isAd = (t, a, g3) => t <= 19;
const reGetTime = /,.{38,}?F:/s;
const replacer = (rawStr, g1, g2, g3) => {
if (iDouhao == 0) iDouhao = g2.lastIndexOf(',')-g2.length;
const times = g2.slice(8,iDouhao).split(reGetTime);
const t = times.reduce((a, b) => +b + a, 0);
return isAd(t,times,g3) ? '' : rawStr.slice(21);
};
text = text.trim();
if (
text.length < 211 ||
!text.startsWith('#EXTM3U') ||
!text.endsWith('#EXT-X-ENDLIST')
) return text;
if (text.slice(22,188).includes('#EXT-X-DISCONTINUITY')) {
text = text.replace(/\s+#EXT-X-DISCONTINUITY/,'');
}
if (iItem == -1) {
console.log('合金HTML5扩展: Remove ad\'s lines of m3u8!');
return text.replace(/\s+(#EXT-X-DISCONTINUITY).+?\1/gs,'');
}
if (4 == iItem) {
isAd = t => t <= 22;
console.log('合金HTML5扩展:已删除非凡云的m3u8广告!');
// #EXTINF:2.233333, 2cf507fe7d7bbeeb898f89cb9b9f47e6.ts 一切片二行~长54,5-8个切片:
return text.replace(/(#EXT-X-DISCONTINUITY\n)(.{54})\1.{270,}?\1/gs, '$2')
.replace(/(#EXT-X-DISCONTINUITY\n)(.{270,}?)\1/gs, replacer)
// .replace(/\s+#EXT-X-DISCONTINUITY/g,'');
}
if (5 == iItem) {
const n = text.lastIndexOf('#EXT-X-DISCONTINUITY');
text = text.slice(0,n) +'#EXT-X-ENDLIST';
console.log('合金HTML5扩展:已删除艾昆云的m3u8广告!');
return text.replace(/(#EXT-X-DISCONTINUITY\n).{255,}?\1/gs, '')
}
if (6 == iItem) {
const n = text.lastIndexOf('#EXT-X-DISCONTINUITY');
text = text.slice(0,n) +'#EXT-X-ENDLIST';
console.log('合金HTML5扩展:已删除魔都云的m3u8广告!');
return text.replace(/\s+(#EXT-X-DISCONTINUITY).{360,}?\1/gs, '')
}
if (0 == iItem) {
iDouhao = 216 - (54-16);
isAd = (t, a, g3) => {
const time5 = g3 ? +g3.slice(8,16) : 0;
if (time5 > 2) return false;
if (t == 16) return true;
t += time5;
return t == 16 || t == 17;
};
console.log('已删除如意云的m3u8广告!');
return text.replace(/(#EXT-X-DISCONTINUITY\n).{8}4.0{6}.{38}(.{216})(.{54})?\1/gs, replacer);
/*
const time0 = 4; // 项0~5
const a = text.split('#EXT-X-DISCONTINUITY\n');
let i = a.length - 9;
for (;i > 9;--i) {
const s = a[i];
if (+s.slice(8,16) != time0) continue;
const time5 = s.length == 324 ? +s.slice(278,286) :
s.length == 270 ? 0 : 99;
if (time5 > 2) continue;
const lines = s.slice(62,232).split(reGetTime);
let time = lines.reduce((a, b) => +b + a, time0);
if (time != 20) time += time5;
if ([20,21,22].includes(time)) {
// console.log('已删除: \n'+ s);
a[i] = '';
i -= 22;
}
}
console.log('已删除如意云的m3u8广告!');
return a.join('');
// 木耳云
const n = text.lastIndexOf('\n') - text.lastIndexOf(',') - 1; // ts'line.length +1
// 3、4个相同时长(正则子组2)
const re = new RegExp(String.raw`(\n#EXT-X-DISCONTINUITY)(.+?,).{${n}}\2.{${n}}\2.{${n},${n*2+22}}\1`,'gs');
const re2Line = new RegExp(`(\\n#EXT-X-DISCONTINUITY).{${n+11},${n+22}}\\1`,'s');
console.log('合金HTML5扩展:已删除木耳云的m3u8广告!');
return text.replace(re2Line,'').replace(re,'') */
}
if (iItem > 6) {
isAd = t => t <= 20;
const n = text.lastIndexOf('#EXT-X-DISCONTINUITY');
text = text.slice(0,n) +'#EXT-X-ENDLIST';
console.log('合金HTML5扩展:已删除神马云的m3u8广告!');
//五、六项切片:216-270
return text.replace(/(#EXT-X-DISCONTINUITY\n)(.{211,}?)\1/gs, replacer)
}
console.log(`合金HTML5扩展:已删除量子云的m3u8广告!`);
// return text.replace(/(#EXT-X-DISCONTINUITY\n).{34}ts.{148,}?\1/gs, '');
//37*4=148,37*7+1=260 #EXT-X-DISCONTINUITY #EXTINF:6.667, 33ebec35ff70821164.ts
return text.replace(/(#EXT-X-DISCONTINUITY\n)(.{185,}?)\1/gs,
(rawStr, g1, g2) => g2.length % 37 ? rawStr.slice(21) : '');
/*
const lines = text.split(/\s+#EXT-X-DISCONTINUITY\s+|\s+/);
let i = lines.length-2;
// for (;!/^0\d+$/.test(lines[i].slice(-9,-3));i-=2) {
// lines[i] = lines[i-1] = void 0;
// }
const max = +lines[i].slice(-8,-3);// -9位断定为 0
const llen = lines[i].length;
// const preWord = lines[i].slice(-12,-9); "dfa" line[i] : adfa005123.ts
for (i-=6;i > 33;i-=2) {
if (llen == lines[i].length && +lines[i].slice(-9,-3) < max) continue;
lines[i] = lines[i-1] = void 0;
}
console.log(`合金HTML5扩展:已删除量子云的m3u8广告!`);
return lines.filter(Boolean).join('\n'); */
};
const realFetch = self.fetch;
self.fetch = new Proxy(self.fetch, {
apply(target, thisArg, args) {
if (!matchM3u(urlFromArg(args[0]))) {
return Reflect.apply(target, thisArg, args);
}
return realFetch(...args).then(realResponse =>
realResponse.text().then(text =>
new Response(pruner(text), {
status: realResponse.status,
statusText: realResponse.statusText,
headers: realResponse.headers,
})
)
);
}
});
self.XMLHttpRequest.prototype.open = new Proxy(self.XMLHttpRequest.prototype.open, {
apply: async (target, thisArg, args) => {
const url = urlFromArg(args[1]);
if (matchM3u(url)) thisArg.addEventListener('readystatechange', function() {
// thisArg.finalUrl && matchM3u(thisArg.finalUrl);
if ( thisArg.readyState !== 4 ) { return; }
const type = thisArg.responseType;
if ( type !== '' && type !== 'text' ) { return; }
const textin = thisArg.responseText;
const textout = pruner(textin);
if ( textout === textin ) { return; }
Reflect.defineProperty(thisArg, 'response', { value: textout });
Reflect.defineProperty(thisArg, 'responseText', { value: textout });
});
return Reflect.apply(target, thisArg, args);
}
});
}