11const root = browser ; //chrome;
2- window . onload = function ( ) {
2+ window . addEventListener ( 'load' , function ( ) {
33 window . workInfo = new workInfoType ( ) ;
44
55 let requestHeaders_by_url = { } ;
6- root . webRequest . onCompleted . addListener ( function ( d /*details*/ ) {
6+ root . webRequest . onCompleted . addListener ( async d => {
77 let ext = ( d . url . split ( '?' ) [ 0 ] . split ( '.' ) . pop ( ) || '' ) . toLowerCase ( ) ;
88 if ( ext === 'm3u8' ) {
99 let requestHeaders = requestHeaders_by_url [ d . url ] ;
1010 if ( requestHeaders ) delete requestHeaders_by_url [ d . url ] ;
11- window . workInfo . addM3u8Urls ( d . tabId , d . url , requestHeaders ) ;
12- try {
13- let t = window . workInfo . tabs [ d . tabId ] ;
14- t . connectPort . postMessage ( { method : 'setM3u8Urls' , data : t . m3u8_urls } ) ;
15- } catch ( ex ) {
16- console . log ( ex ) ;
17- }
18- window . workInfo . setUrlsCountText ( { tabId : d . tabId } ) ;
11+
12+ window . workInfo . addM3u8Urls ( d . tabId , d . url , requestHeaders ) ;
13+ await window . workInfo . setUrlsCountText ( d . tabId ) ;
1914 }
2015 } ,
21- { urls : [ " <all_urls>" ] } ) ;
16+ { urls : [ ' <all_urls>' ] } ) ;
2217
23- root . webRequest . onBeforeSendHeaders . addListener ( async function ( d /*details*/ ) {
18+ root . webRequest . onBeforeSendHeaders . addListener ( d => {
2419 let ext = ( d . url . split ( '?' ) [ 0 ] . split ( '.' ) . pop ( ) || '' ) . toLowerCase ( ) ;
2520 if ( ext === 'm3u8' ) {
26- //console.log('onBeforeSendHeaders() => tabId: ' + d.tabId + ', url: ' + d.url );
2721 requestHeaders_by_url [ d . url ] = JSON . stringify ( d . requestHeaders ) ;
2822 }
29- //else console.log('discarded => tabId: ' + d.tabId + ', url: ' + d.url );
3023 } ,
3124 { urls : [ '<all_urls>' ] } , [ 'requestHeaders' ] ) ;
3225
3326 // set handler to tabs
34- root . tabs . onActivated . addListener ( d => window . workInfo . onActivated ( d . tabId ) ) ;
35-
36- // set handler to tabs: need for send objects
37- if ( root . extension . onConnect ) {
38- root . extension . onConnect . addListener ( port => port . onMessage . addListener ( workInfo_methodCaller ) ) ;
39- }
27+ root . tabs . onActivated . addListener ( async d => await window . workInfo . activateTab ( d . tabId ) ) ;
4028
4129 // set handler to tabs
42- root . tabs . onUpdated . addListener ( function ( tabId , info , tab ) {
30+ root . tabs . onUpdated . addListener ( async ( tabId , info , tab ) => {
4331 if ( ! info ?. status || ( info . status . toLowerCase ( ) !== 'complete' ) ) return ;
4432
4533 // if user open empty tab or ftp protocol and etc.
46- if ( ( tabId === undefined ) || ! tab ?. url || ( ( tab . url . indexOf ( 'http:' ) === - 1 ) && ( tab . url . indexOf ( 'https:' ) === - 1 ) ) ) {
47- if ( tabId !== undefined ) root . browserAction . disable ( tabId ) ;
48- return ( 0 ) ;
34+ if ( ( tabId === undefined ) || ! tab ?. url || ( ! tab . url . startsWith ( 'http://' ) && ! tab . url . startsWith ( 'https://' ) ) ) {
35+ if ( tabId !== undefined ) await root . browserAction . disable ( tabId ) ;
4936 }
37+ else {
38+ let opt = await root . storage . local . get ( ) ;
39+ if ( ! opt . saveUrlListBetweenTabReload ) {
40+ window . workInfo . resetM3u8Urls ( tabId ) ;
41+ }
5042
51- window . workInfo . onActivated ( tabId ) ;
52- root . browserAction . enable ( tabId ) ;
53-
54- // save tab info if need
55- window . workInfo . addTab ( tab ) ;
56-
57- // connect with new tab, and save object
58- let port = root . tabs . connect ( tabId ) ;
59- let t = window . workInfo . tabs [ tabId ] ;
60- t . connectPort = port ;
61-
62- // run function in script_in_content.js
63- root . tabs . executeScript ( tabId , { code : "popupInfo_init()" } ) ;
64-
65- // send tabId, hosts and others information into script_in_content.js
66- port . postMessage ( { method : 'setTabId' , data : tabId } ) ;
67- port . postMessage ( { method : 'setM3u8Urls' , data : t . m3u8_urls } ) ;
68- port . postMessage ( { method : 'connect2Extension' } ) ;
43+ //if (opt.enableButtonEvenIfNoUrls) {
44+ // await root.browserAction.enable(tabId);
45+ //}
46+ await window . workInfo . activateTab ( tabId ) ;
47+ }
6948 } ) ;
7049
71- root . tabs . onRemoved . addListener ( tabId => window . workInfo . deleteTab ( tabId ) ) ;
72- } ;
73-
74- function workInfo_methodCaller ( obj ) {
75- if ( obj ?. method ) {
76- window . workInfo [ obj . method ] ( obj ?. data ) ;
77- }
78- }
50+ root . tabs . onRemoved . addListener ( async tabId => await window . workInfo . deleteTab ( tabId ) ) ;
51+ } ) ;
7952
8053window . workInfoType = function ( ) { } ;
8154window . workInfoType . prototype = {
8255 tabs : { } ,
83- active_tabId : null ,
56+ activeTabId : null ,
8457
85- addTab : function ( tab ) {
86- if ( tab && ( tab . id !== undefined ) ) {
87- let o = this . tabs [ tab . id ] ;
58+ activateTab : async function ( tabId ) {
59+ // set active tab
60+ this . activeTabId = tabId ;
61+
62+ let d = { m3u8_urls : [ ] } ;
63+ if ( tabId !== undefined ) {
64+ let o = this . tabs [ tabId ] ;
8865 if ( ! o ) {
89- this . tabs [ tab . id ] = { tab_obj : tab } ;
90- } else {
66+ o = this . tabs [ tabId ] = { m3u8_urls : [ ] } ;
67+ }
68+ else if ( ! o . m3u8_urls ) {
9169 o . m3u8_urls = [ ] ;
9270 }
93-
94- this . setUrlsCountText ( { tabId : tab . id } ) ;
71+ d . m3u8_urls = o . m3u8_urls ;
9572 }
73+ await this . setUrlsCountText ( tabId ) ;
9674 } ,
97- deleteTab : function ( tabId ) {
75+ deleteTab : async function ( tabId ) {
9876 let has = ! ! this . tabs [ tabId ] ;
99- if ( has ) delete this . tabs [ tabId ] ;
77+ if ( has ) {
78+ delete this . tabs [ tabId ] ;
79+ await this . setUrlsCountText ( tabId ) ;
80+ }
10081 } ,
101- deleteTabUrls : async function ( tabId ) {
102- let has = ! ! this . tabs [ tabId ] ;
103- if ( has ) delete this . tabs [ tabId ] ;
82+ deleteUrlsFromActivateTab : async function ( ) { await this . deleteTab ( this . activeTabId ) ; } ,
83+
84+ setUrlsCountText : async function ( tabId ) {
85+ //if (tabId !== undefined) {
86+ // const cnt = this.tabs[tabId]?.m3u8_urls?.length;
87+ // if (tabId === this.activeTabId) {
88+ // await root.browserAction.setBadgeText({ text: (cnt || '') + '' });
89+ // }
90+
91+ // let opt = await root.storage.local.get();
92+ // if (!opt.enableButtonEvenIfNoUrls) {
93+ // if (cnt) await root.browserAction.enable(tabId);
94+ // else await root.browserAction.disable(tabId)
95+ // }
96+ //}
10497
105- await this . setUrlsCountText ( { tabId : tabId } ) ;
106- //if (has) await this.save2Storage();
98+ if ( tabId !== undefined ) {
99+ let opt = await root . storage . local . get ( ) ;
100+ if ( opt . enableButtonEvenIfNoUrls ) {
101+ await root . browserAction . enable ( tabId ) ;
102+
103+ if ( tabId === this . activeTabId ) {
104+ const cnt = this . tabs [ tabId ] ?. m3u8_urls ?. length ;
105+ await root . browserAction . setBadgeText ( { text : ( cnt || '' ) + '' } ) ;
106+ }
107+ } else {
108+ const cnt = this . tabs [ tabId ] ?. m3u8_urls ?. length ;
109+ if ( tabId === this . activeTabId ) {
110+ await root . browserAction . setBadgeText ( { text : ( cnt || '' ) + '' } ) ;
111+ }
112+
113+ if ( cnt ) await root . browserAction . enable ( tabId ) ;
114+ else await root . browserAction . disable ( tabId )
115+ }
116+ }
117+ } ,
118+
119+ resetM3u8Urls : function ( tabId ) {
120+ const o = this . tabs [ tabId ] ;
121+ if ( o ) {
122+ o . m3u8_urls = [ ] ;
123+ o . requestHeaders = { } ;
124+ //await this.setUrlsCountText(tabId);
125+ }
107126 } ,
108127 addM3u8Urls : function ( tabId , m3u8_url , requestHeaders ) {
109128 if ( ( tabId !== undefined ) && m3u8_url ) {
@@ -123,35 +142,8 @@ window.workInfoType.prototype = {
123142 }
124143 }
125144 } ,
126- setUrlsCountText : function ( d ) {
127- let o = d ? this . tabs [ d . tabId ] : null , cnt = o ?. m3u8_urls ?. length ;
128- if ( cnt ) {
129- root . browserAction . setBadgeText ( { text : cnt + '' } ) ;
130- return ( 0 ) ;
131- } else {
132- root . browserAction . setBadgeText ( { text : '' } ) ;
133- }
134- } ,
135- onActivated : function ( tabId ) {
136- // set active tab
137- this . active_tabId = tabId ;
138-
139- let d = { m3u8_urls : [ ] } ;
140-
141- if ( tabId !== undefined ) {
142- let o = this . tabs [ tabId ] ;
143- if ( ! o ) {
144- o = this . tabs [ tabId ] = { m3u8_urls : [ ] } ;
145- }
146- else if ( ! o . m3u8_urls ) {
147- o . m3u8_urls = [ ] ;
148- }
149- d . m3u8_urls = o . m3u8_urls ;
150- }
151- this . setUrlsCountText ( { tabId : tabId } ) ;
152- } ,
153- getM3u8Urls : function ( ) {
154- let o = ( this . active_tabId !== undefined ) ? this . tabs [ this . active_tabId ] : null ;
145+ getM3u8UrlsByActiveTabId : function ( activeTabId ) {
146+ let o = this . tabs [ activeTabId ] || this . tabs [ this . activeTabId ]
155147 return ( o ?. m3u8_urls ? { m3u8_urls : o . m3u8_urls , requestHeaders : o . requestHeaders || { } } : { m3u8_urls : [ ] , requestHeaders : { } } ) ;
156148 }
157149} ;
0 commit comments