-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinjectScript.js
97 lines (83 loc) · 3.02 KB
/
injectScript.js
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
const WEBVTT = 'webvtt-lssdh-ios8';
const DFXP = 'dfxp-ls-sdh';
const SIMPLE = 'simplesdh';
const ALL_FORMATS = [WEBVTT, DFXP, SIMPLE];
const FORMAT_NAMES = {};
FORMAT_NAMES[WEBVTT] = 'WebVTT';
FORMAT_NAMES[DFXP] = 'DFXP/XML';
const EXTENSIONS = {};
EXTENSIONS[WEBVTT] = 'vtt';
EXTENSIONS[DFXP] = 'dfxp';
EXTENSIONS[SIMPLE] = 'xml';
const SUB_TYPES = {
subtitles: '',
closedcaptions: '[cc]',
};
const idOverrides = {};
const subCache = {};
const processSubInfo = async result => {
const tracks = result.timedtexttracks;
// const titleP = await getTitle();
const subs = {};
for (const track of tracks) {
if (track.isNoneTrack) continue;
let type = SUB_TYPES[track.rawTrackType];
if (typeof type === 'undefined') type = `[${track.rawTrackType}]`;
const lang = track.language + type + (track.isForcedNarrative ? '-forced' : '');
const formats = {};
for (let format of ALL_FORMATS) {
if (typeof track.ttDownloadables[format] !== 'undefined')
formats[format] = [Object.values(track.ttDownloadables[format].downloadUrls), EXTENSIONS[format]];
}
if (Object.keys(formats).length > 0) subs[lang] = formats;
}
subCache[result.movieId] = { subs };
console.log({ result, subs });
};
const processMessage = e => {
const override = e.detail.id_override;
if (typeof override !== 'undefined') idOverrides[override[0]] = override[1];
else processSubInfo(e.detail);
};
const injection = () => {
const WEBVTT = 'webvtt-lssdh-ios8';
const MANIFEST_PATTERN = new RegExp('manifest|licensedManifest');
// 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) {
/*{
let text = stringify(data);
if (text.includes('dfxp-ls-sdh'))
console.log(text, data);
}*/
if (data && typeof data.url === 'string' && data.url.search(MANIFEST_PATTERN) > -1) {
for (let v of Object.values(data)) {
try {
if (v.profiles) v.profiles.unshift(WEBVTT);
if (v.showAllSubDubTracks != null) v.showAllSubDubTracks = true;
} catch (e) {
if (e instanceof TypeError) continue;
else throw e;
}
}
}
if (data && typeof data.movieId === 'number') {
try {
let videoId = data.params.sessionParams.uiplaycontext.video_id;
if (typeof videoId === 'number' && videoId !== data.movieId)
window.dispatchEvent(new CustomEvent('netflix_sub_downloader_data', { detail: { id_override: [videoId, data.movieId] } }));
} catch (ignore) {}
}
return stringify(data);
};
})(JSON.parse, JSON.stringify);
};
injection();
window.addEventListener('netflix_sub_downloader_data', processMessage, false);