forked from Tithen-Firion/UserScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetflix_-_subtitle_downloader.user.js
More file actions
317 lines (280 loc) · 10.1 KB
/
Netflix_-_subtitle_downloader.user.js
File metadata and controls
317 lines (280 loc) · 10.1 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// ==UserScript==
// @name Netflix - subtitle downloader
// @description Allows you to download subtitles from Netflix
// @license MIT
// @version 3.2.1
// @namespace tithen-firion.github.io
// @include https://www.netflix.com/*
// @grant unsafeWindow
// @require https://cdn.jsdelivr.net/gh/Stuk/jszip@579beb1d45c8d586d8be4411d5b2e48dea018c06/dist/jszip.min.js?version=3.1.5
// @require https://cdn.jsdelivr.net/gh/eligrey/FileSaver.js@283f438c31776b622670be002caf1986c40ce90c/dist/FileSaver.min.js?version=2018-12-29
// ==/UserScript==
class ProgressBar {
constructor(max) {
this.current = 0;
this.max = max;
let container = document.querySelector('#userscript_progress_bars');
if(container === null) {
container = document.createElement('div');
container.id = 'userscript_progress_bars'
document.body.appendChild(container)
container.style
container.style.position = 'fixed';
container.style.top = 0;
container.style.left = 0;
container.style.width = '100%';
container.style.background = 'red';
container.style.zIndex = '99999999';
}
this.progressElement = document.createElement('div');
this.progressElement.style.width = 0;
this.progressElement.style.height = '10px';
this.progressElement.style.background = 'green';
container.appendChild(this.progressElement);
}
increment() {
this.current += 1;
if(this.current <= this.max)
this.progressElement.style.width = this.current / this.max * 100 + '%';
}
destroy() {
this.progressElement.remove();
}
}
const MAIN_TITLE = '.player-status-main-title, .ellipsize-text>h4, .video-title>h4';
const TRACK_MENU = '#player-menu-track-settings, .audio-subtitle-controller';
const NEXT_EPISODE = '.player-next-episode:not(.player-hidden), .button-nfplayerNextEpisode';
const WEBVTT = 'webvtt-lssdh-ios8';
const DOWNLOAD_MENU = `<lh class="list-header">Netflix subtitle downloader</lh>
<li class="list-header">Netflix subtitle downloader</li>
<li class="track download">Download subs for this episode</li>
<li class="track download-all">Download subs from this ep till last available</li>
<li class="track force-all-lang">Force Netflix to show all languages: <span></span></li>
<li class="track lang-setting">Languages to download: <span></span></li>`;
const SCRIPT_CSS = `.player-timed-text-tracks, .track-list-subtitles{ border-right:1px solid #000 }
.player-timed-text-tracks+.player-timed-text-tracks, .track-list-subtitles+.track-list-subtitles{ border-right:0 }
.subtitle-downloader-menu { list-style:none }
#player-menu-track-settings .subtitle-downloader-menu li.list-header,
.audio-subtitle-controller .subtitle-downloader-menu lh.list-header{ display:none }`;
const SUB_TYPES = {
'subtitles': '',
'closedcaptions': '[cc]'
};
let zip;
let subCache = {};
let batch = false;
let forceSubs = localStorage.getItem('NSD_force-all-lang') !== 'false';
let langs = localStorage.getItem('NSD_lang-setting') || '';
const setForceText = () => {
document.querySelector('.subtitle-downloader-menu > .force-all-lang > span').innerHTML = (forceSubs ? 'on' : 'off');
};
const setLangsText = () => {
document.querySelector('.subtitle-downloader-menu > .lang-setting > span').innerHTML = (langs === '' ? 'all' : langs);
};
const toggleForceLang = () => {
forceSubs = !forceSubs;
if(forceSubs)
localStorage.removeItem('NSD_force-all-lang');
else
localStorage.setItem('NSD_force-all-lang', forceSubs);
document.location.reload();
};
const setLangToDownload = () => {
const result = prompt('Languages to download, comma separated. Leave empty to download all subtitles.\nExample: en,de,fr', langs);
if(result !== null) {
langs = result;
if(langs === '')
localStorage.removeItem('NSD_lang-setting');
else
localStorage.setItem('NSD_lang-setting', langs);
setLangsText();
}
};
const popRandomElement = arr => {
return arr.splice(arr.length * Math.random() << 0, 1)[0];
};
// get show name or full name with episode number
const __getTitle = full => {
if(typeof full === 'undefined')
full = true;
const titleElement = document.querySelector(MAIN_TITLE);
if(titleElement === null)
return null;
const title = [titleElement.textContent.replace(/[:*?"<>|\\\/]+/g, '_').replace(/ /g, '.')];
if(full) {
const episodeElement = titleElement.nextElementSibling;
if(episodeElement) {
const m = episodeElement.textContent.match(/^[^\d]*(\d+)[^\d]+(\d+)[^\d]*$/);
if(m && m.length == 3) {
title.push(`S${m[1].padStart(2, '0')}E${m[2].padStart(2, '0')}`);
}
else {
title.push(episodeElement.textContent.trim().replace(/[:*?"<>|\\\/]+/g, '_').replace(/ /g, '.'));
}
}
title.push('WEBRip.Netflix');
}
return title.join('.');
};
// helper function, periodically checking for the title and resolving promise if found
const _getTitle = (full, resolve) => {
const title = __getTitle(full);
if(title === null)
window.setTimeout(_getTitle, 200, full, resolve);
else
resolve(title);
};
// promise of a title
const getTitle = full => new Promise(resolve => {
_getTitle(full, resolve);
});
const processSubInfo = async result => {
const tracks = result.timedtexttracks;
const titleP = getTitle();
const subs = {};
for(const track of tracks) {
if(track.isNoneTrack)
continue;
if(typeof track.ttDownloadables[WEBVTT] === 'undefined')
continue;
let type = SUB_TYPES[track.rawTrackType];
if(typeof type === 'undefined')
type = `[${track.rawTrackType}]`;
const lang = track.language + type + (track.isForcedNarrative ? '-forced' : '');
subs[lang] = Object.values(track.ttDownloadables[WEBVTT].downloadUrls);
}
subCache[result.movieId] = {titleP, subs};
if(batch) {
downloadAll();
}
};
const getMovieID = () => window.location.pathname.split('/').pop();
const _save = async (_zip, title) => {
const content = await _zip.generateAsync({type:'blob'});
saveAs(content, title + '.zip');
};
const _download = async _zip => {
const showTitle = getTitle(false);
const {titleP, subs} = subCache[getMovieID()];
const downloaded = [];
let filteredLangs;
if(langs === '')
filteredLangs = Object.keys(subs);
else {
const regularExpression = new RegExp('^(' + langs.replace(/\-/g, '\\-').replace(/\s/g, '').replace(/,/g, '|') + ')');
filteredLangs = [];
for(const lang of Object.keys(subs)) {
if(lang.match(regularExpression))
filteredLangs.push(lang);
}
}
const progress = new ProgressBar(filteredLangs.length);
for(const lang of filteredLangs) {
const urls = subs[lang]
while(urls.length > 0) {
let url = popRandomElement(urls);
const result = await fetch(url, {mode: "cors"});
progress.increment();
const data = await result.text();
if(data.length > 0) {
downloaded.push({lang, data});
break;
}
}
}
progress.destroy();
const title = await titleP;
downloaded.forEach(x => {
const {lang, data} = x;
_zip.file(`${title}.${lang}.vtt`, data);
});
return await showTitle;
};
const downloadThis = async () => {
const _zip = new JSZip();
const showTitle = await _download(_zip);
_save(_zip, showTitle);
};
const downloadAll = async () => {
zip = zip || new JSZip();
batch = true;
const showTitle = await _download(zip);
const nextEp = document.querySelector(NEXT_EPISODE);
if(nextEp)
nextEp.click();
else {
await _save(zip, showTitle);
zip = undefined;
batch = false;
}
};
const processMessage = e => {
processSubInfo(e.detail);
}
const injection = () => {
const WEBVTT = 'webvtt-lssdh-ios8';
const MANIFEST_URL = "/manifest";
const forceSubs = localStorage.getItem('NSD_force-all-lang') !== 'false';
// hijack JSON.parse and JSON.stringify functions
((parse, stringify) => {
JSON.parse = function (text) {
const data = parse(text);
if (data && data.result && data.result.timedtexttracks && data.result.movieId) {
window.dispatchEvent(new CustomEvent('netflix_sub_downloader_data', {detail: data.result}));
}
return data;
};
JSON.stringify = function (data) {
if (data && data.url === MANIFEST_URL) {
for (let v of Object.values(data)) {
try {
if (v.profiles)
v.profiles.unshift(WEBVTT);
if (v.showAllSubDubTracks != null && forceSubs)
v.showAllSubDubTracks = true;
}
catch (e) {
if (e instanceof TypeError)
continue;
else
throw e;
}
}
}
return stringify(data);
};
})(JSON.parse, JSON.stringify);
}
window.addEventListener('netflix_sub_downloader_data', processMessage, false);
// inject script
const sc = document.createElement('script');
sc.innerHTML = '(' + injection.toString() + ')()';
document.head.appendChild(sc);
document.head.removeChild(sc);
// add CSS style
const s = document.createElement('style');
s.innerHTML = SCRIPT_CSS;
document.head.appendChild(s);
// add menu when it's not there
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
mutation.addedNodes.forEach(function(node) {
if(node.nodeName.toUpperCase() == 'DIV') {
let trackMenu = (node.parentNode || node).querySelector(TRACK_MENU);
if(trackMenu !== null && trackMenu.querySelector('.subtitle-downloader-menu') === null) {
let ol = document.createElement('ol');
ol.setAttribute('class', 'subtitle-downloader-menu player-timed-text-tracks track-list track-list-subtitles');
ol.innerHTML = DOWNLOAD_MENU;
trackMenu.appendChild(ol);
ol.querySelector('.download').addEventListener('click', downloadThis);
ol.querySelector('.download-all').addEventListener('click', downloadAll);
ol.querySelector('.force-all-lang').addEventListener('click', toggleForceLang);
ol.querySelector('.lang-setting').addEventListener('click', setLangToDownload);
setForceText();
setLangsText();
}
}
});
});
});
observer.observe(document.body, { childList: true, subtree: true });