Skip to content

Commit aa7e5c0

Browse files
committed
[netflix subs] fix 0 bytes subs
1 parent 49f52c4 commit aa7e5c0

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

Netflix_-_subtitle_downloader.user.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// @name Netflix - subtitle downloader
33
// @description Allows you to download subtitles from Netflix
44
// @license MIT
5-
// @version 3.0.11
5+
// @version 3.0.12
66
// @namespace tithen-firion.github.io
77
// @include https://www.netflix.com/*
88
// @grant unsafeWindow
@@ -36,9 +36,8 @@ let zip;
3636
let subCache = {};
3737
let batch = false;
3838

39-
const randomProperty = obj => {
40-
const keys = Object.keys(obj);
41-
return obj[keys[keys.length * Math.random() << 0]];
39+
const popRandomElement = arr => {
40+
return arr.splice(arr.length * Math.random() << 0, 1)[0];
4241
};
4342

4443
// get show name or full name with episode number
@@ -91,7 +90,7 @@ const processSubInfo = async result => {
9190
if(typeof type === 'undefined')
9291
type = `[${track.rawTrackType}]`;
9392
const lang = track.language + type + (track.isForcedNarrative ? '-forced' : '');
94-
subs[lang] = randomProperty(track.ttDownloadables[WEBVTT].downloadUrls);
93+
subs[lang] = Object.values(track.ttDownloadables[WEBVTT].downloadUrls);
9594
}
9695
subCache[result.movieId] = {titleP, subs};
9796

@@ -112,10 +111,16 @@ const _download = async _zip => {
112111
const showTitle = getTitle(false);
113112
const {titleP, subs} = subCache[getMovieID()];
114113
const downloaded = [];
115-
for(const [lang, url] of Object.entries(subs)) {
116-
const result = await fetch(url, {mode: "cors"});
117-
const data = await result.text();
118-
downloaded.push({lang, data});
114+
for(const [lang, urls] of Object.entries(subs)) {
115+
while(urls.length > 0) {
116+
let url = popRandomElement(urls);
117+
const result = await fetch(url, {mode: "cors"});
118+
const data = await result.text();
119+
if(data.length > 0) {
120+
downloaded.push({lang, data});
121+
break;
122+
}
123+
}
119124
}
120125
const title = await titleP;
121126

0 commit comments

Comments
 (0)