2
2
// @name Netflix - subtitle downloader
3
3
// @description Allows you to download subtitles from Netflix
4
4
// @license MIT
5
- // @version 3.0.11
5
+ // @version 3.0.12
6
6
// @namespace tithen-firion.github.io
7
7
// @include https://www.netflix.com/*
8
8
// @grant unsafeWindow
@@ -36,9 +36,8 @@ let zip;
36
36
let subCache = { } ;
37
37
let batch = false ;
38
38
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 ] ;
42
41
} ;
43
42
44
43
// get show name or full name with episode number
@@ -91,7 +90,7 @@ const processSubInfo = async result => {
91
90
if ( typeof type === 'undefined' )
92
91
type = `[${ track . rawTrackType } ]` ;
93
92
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 ) ;
95
94
}
96
95
subCache [ result . movieId ] = { titleP, subs} ;
97
96
@@ -112,10 +111,16 @@ const _download = async _zip => {
112
111
const showTitle = getTitle ( false ) ;
113
112
const { titleP, subs} = subCache [ getMovieID ( ) ] ;
114
113
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
+ }
119
124
}
120
125
const title = await titleP ;
121
126
0 commit comments