1
- let activeIITCTab = null ;
1
+ let lastIITCTab = null ;
2
2
const {
3
- onActivated,
4
3
onUpdated,
5
4
onRemoved
6
5
} = chrome . tabs ;
7
6
// tab
8
- onActivated . addListener ( onActivatedListener ) ;
9
7
onUpdated . addListener ( onUpdatedListener ) ;
10
8
onRemoved . addListener ( onRemovedListener ) ;
11
9
12
10
chrome . runtime . onMessage . addListener ( function ( request ) {
13
11
switch ( request . type ) {
14
12
case "requestOpenIntel" :
15
- onRequestOpenIntel ( request . tab ) . finally ( ) ;
13
+ onRequestOpenIntel ( ) . finally ( ) ;
16
14
break ;
17
15
case "toggleIITC" :
18
16
onToggleIITC ( request . value ) . finally ( ) ;
@@ -29,61 +27,38 @@ chrome.webNavigation.onBeforeNavigate.addListener(
29
27
{ url : [ { pathSuffix : ".user.js" } ] }
30
28
) ;
31
29
32
- async function onRequestOpenIntel ( id ) {
33
- if ( ! id ) return ;
34
- const {
35
- active,
36
- url
37
- } = await getTabInfo ( id ) ;
38
- if ( activeIITCTab ) {
39
- let isActive = false ;
40
30
41
- try {
42
- isActive = await getTabInfo ( activeIITCTab ) ;
43
- } catch ( e ) {
44
- console . warn ( 'tab not found:' , activeIITCTab ) ;
45
- }
46
-
47
- if ( ! ! isActive ) {
48
- console . log ( 'found activeIITCTab %s' , activeIITCTab ) ;
49
- return setTabActive ( activeIITCTab ) ;
50
- } else {
51
- activeIITCTab = null ;
31
+ async function onRequestOpenIntel ( ) {
32
+ if ( lastIITCTab ) {
33
+ const tabInfo = await getTabInfo ( lastIITCTab ) ;
34
+ if ( isIngressUrl ( tabInfo . url ) ) {
35
+ console . log ( 'detected ingress.com/intel page on tab %d' , lastIITCTab ) ;
36
+ return setTabActive ( lastIITCTab ) ;
52
37
}
53
38
}
54
- if ( active ) {
55
- if ( isIngressUrl ( url ) ) {
56
- console . log ( 'detected ingress.com/intel page on active tab %d' , id ) ;
57
- return ;
58
- }
59
39
60
- return chrome . tabs . create ( {
61
- url : 'https://intel.ingress.com/intel' ,
62
- pinned : true
63
- } , function ( tab ) {
64
- activeIITCTab = tab . id ;
65
- } ) ;
66
- }
40
+ return chrome . tabs . create ( {
41
+ url : 'https://intel.ingress.com/intel' ,
42
+ pinned : true
43
+ } , function ( tab ) {
44
+ lastIITCTab = tab . id ;
45
+ } ) ;
67
46
}
68
47
69
48
async function onToggleIITC ( value ) {
70
49
chrome . storage . local . set ( { 'IITC_is_enabled' : value } , async function ( ) {
71
- if ( activeIITCTab ) {
72
- let isActive = false ;
73
50
74
- try {
75
- isActive = await getTabInfo ( activeIITCTab ) ;
76
- } catch ( e ) {
77
- console . warn ( 'tab not found:' , activeIITCTab ) ;
78
- }
51
+ // Fetch all completly loaded Ingress Intel tabs
52
+ chrome . tabs . query ( {
53
+ "url" : "https://intel.ingress.com/*" ,
54
+ "status" : "complete"
55
+ } , function ( tabs ) {
79
56
80
- if ( ! ! isActive ) {
81
- console . log ( 'found activeIITCTab %s' , activeIITCTab ) ;
82
- return chrome . tabs . reload ( activeIITCTab ) ;
83
- } else {
84
- activeIITCTab = null ;
57
+ for ( let tab of Object . values ( tabs ) ) {
58
+ chrome . tabs . reload ( tab . id ) ;
85
59
}
86
- }
60
+
61
+ } ) ;
87
62
} ) ;
88
63
89
64
}
@@ -92,66 +67,26 @@ async function onToggleIITC(value) {
92
67
// tab listeners
93
68
async function onUpdatedListener ( tabId , status ) {
94
69
if ( status . status ) {
95
- console . log ( JSON . stringify ( status ) ) ;
96
- console . log ( status . status , ':tab updated #' , tabId ) ;
97
-
98
- const {
99
- active,
100
- url
101
- } = await getTabInfo ( tabId ) ;
102
- if ( tabId === activeIITCTab ) {
103
- console . log ( 'remove activeIITCTab' ) ;
104
- if ( status . status === 'loading' && status . url ) {
105
- console . info ( 'navigate to %s' , status . url ) ;
106
- }
107
- }
108
- console . log ( 'tab is active: ' , active ) ;
109
-
110
70
if ( status . status === 'complete' ) {
111
- if ( isIngressUrl ( url ) ) {
112
- loaded_plugins = [ ] ;
113
- console . log ( 'detected intel.ingress.com/intel page on active tab %d' , tabId ) ;
71
+ const tabInfo = await getTabInfo ( tabId ) ;
72
+ if ( isIngressUrl ( tabInfo . url ) ) {
73
+ console . log ( 'detected intel.ingress.com/intel page on tab %d' , tabId ) ;
114
74
console . log ( 'requested iitc launch' ) ;
115
- console . log ( 'initializing iitc' ) ;
116
- initialize ( tabId ) ;
75
+ initialize ( ) ;
76
+ lastIITCTab = tabId ;
117
77
}
118
78
}
119
-
120
79
return false ;
121
80
}
122
81
}
123
82
124
83
function onRemovedListener ( tabId ) {
125
- console . log ( tabId + ': closing' ) ;
126
- if ( activeIITCTab === tabId ) {
127
- activeIITCTab = null ;
128
- console . log ( 'removed iitc flags' )
129
- }
130
- }
131
-
132
- async function onActivatedListener ( {
133
- tabId,
134
- url
135
- } ) {
136
- if ( ! tabId ) {
137
- throw new Error ( 'not tabId found' )
138
- }
139
- const tabInfo = await getTabInfo ( tabId ) ;
140
- if ( tabInfo ) {
141
- const isIngressTab = isIngressUrl ( tabInfo . url )
142
-
143
- if ( isIngressTab ) {
144
- console . log ( 'tab has Intel url #' , tabId ) ;
145
- }
84
+ if ( lastIITCTab === tabId ) {
85
+ lastIITCTab = null ;
146
86
}
147
-
148
- const {
149
- active
150
- } = await getTabInfo ( tabId ) ;
151
- //if (active) chrome.pageAction.show(tabId);
152
87
}
153
88
154
- function initialize ( tabId ) {
89
+ function initialize ( ) {
155
90
156
91
chrome . storage . local . get ( [
157
92
"IITC_is_enabled" ,
@@ -169,21 +104,15 @@ function initialize(tabId) {
169
104
let iitc_version = data [ channel + '_iitc_version' ] ;
170
105
if ( ( status === undefined || status === true ) && iitc_code !== undefined ) {
171
106
172
- chrome . tabs . executeScript ( tabId , {
173
- runAt : "document_end" ,
174
- file : './scripts/pre.js'
175
- } , ( ) => {
176
107
let inject_iitc_code = preparationUserScript ( { 'version' : iitc_version , 'code' : iitc_code } ) ;
177
- loadJS ( tabId , "document_end" , "ingress-intel-total-conversion@jonatkins" , inject_iitc_code , function ( ) {
178
- activeIITCTab = tabId ;
179
- } ) ;
108
+ injectUserScript ( inject_iitc_code ) ;
180
109
181
110
let plugins_local = data [ channel + '_plugins_local' ] ;
182
111
if ( plugins_local !== undefined ) {
183
112
Object . keys ( plugins_local ) . forEach ( function ( id ) {
184
113
let plugin = plugins_local [ id ] ;
185
114
if ( plugin [ 'status' ] === 'on' ) {
186
- loadJS ( tabId , "document_end" , id , preparationUserScript ( plugin , id ) ) ;
115
+ injectUserScript ( preparationUserScript ( plugin , id ) ) ;
187
116
}
188
117
} ) ;
189
118
}
@@ -193,41 +122,42 @@ function initialize(tabId) {
193
122
Object . keys ( plugins_user ) . forEach ( function ( id ) {
194
123
let plugin = plugins_user [ id ] ;
195
124
if ( plugin [ 'status' ] === 'on' ) {
196
- loadJS ( tabId , "document_end" , id , preparationUserScript ( plugin , id ) ) ;
125
+ injectUserScript ( preparationUserScript ( plugin , id ) ) ;
197
126
}
198
127
} ) ;
199
128
}
200
129
201
-
202
- } ) ;
130
+
203
131
204
132
}
205
133
} ) ;
206
134
207
135
}
208
136
209
137
210
- function loadJS ( tabId , runAt , id , code , callback ) {
211
- if ( ! tabId ) { console . log ( 'no tabId!' ) ; return }
212
-
213
- if ( loaded_plugins . includes ( id ) ) {
214
- console . info ( 'Plugin %s is already loaded. Skip' , id ) ;
215
- return
216
- } else {
217
- loaded_plugins . push ( id ) ;
218
- }
219
-
220
- callback = ( typeof callback == 'function' ? callback : false ) ;
221
-
222
- chrome . tabs . executeScript ( tabId , {
223
- runAt : runAt ,
224
- code : code
225
- } , ( ) => {
226
- if ( chrome . runtime . lastError ) {
227
- console . log ( chrome . runtime . lastError . message ) ;
138
+ function injectUserScript ( code ) {
139
+ let inject = `
140
+ document.dispatchEvent(new CustomEvent('IITCButtonInitJS', {
141
+ detail: ` + JSON . stringify ( code ) + `
142
+ }));
143
+ `
144
+ // Fetch all completly loaded Ingress Intel tabs
145
+ chrome . tabs . query ( {
146
+ "url" : "https://intel.ingress.com/*" ,
147
+ "status" : "complete"
148
+ } , function ( tabs ) {
149
+
150
+ for ( let tab of Object . values ( tabs ) ) {
151
+ console . log ( tab ) ;
152
+ chrome . tabs . executeScript ( tab . id , {
153
+ code : inject
154
+ } , ( ) => {
155
+ if ( chrome . runtime . lastError ) {
156
+ console . log ( chrome . runtime . lastError . message ) ;
157
+ }
158
+ } ) ;
228
159
}
229
- console . info ( 'plugin %s loaded' , id ) ;
230
- if ( callback ) callback ( ) ;
160
+
231
161
} ) ;
232
162
233
163
}
@@ -240,10 +170,9 @@ function setTabActive(tabId) {
240
170
setWindowFocused ( tab . windowId )
241
171
} catch ( e ) {
242
172
console . log ( e ) ;
243
- activeIITCTab = null ;
173
+ lastIITCTab = null ;
244
174
console . log ( 'repeated click with updated params' ) ;
245
- let id = await getActiveTab ( ) ;
246
- onRequestOpenIntel ( id ) ;
175
+ onRequestOpenIntel ( ) . finally ( ) ;
247
176
}
248
177
} ) ;
249
178
}
@@ -256,24 +185,6 @@ function getTabInfo(tabId) {
256
185
return new Promise ( resolve => chrome . tabs . get ( tabId , resolve ) ) ;
257
186
}
258
187
259
- async function getActiveTab ( ) {
260
- return new Promise ( resolve => chrome . tabs . query ( { active : true } , resolve ) )
261
- . then ( function ( current ) {
262
- if ( current && current [ 0 ] ) {
263
- return current [ 0 ] . id
264
- } else {
265
- throw new Error ( 'current tab not found' )
266
- }
267
- } ) ;
268
- }
269
-
270
- /* function togglePageAction(state, id) {
271
- state = state ? 'show' : 'hide';
272
-
273
- chrome.pageAction.setIcon({ tabId: id, path: state ? "assets/images/48/logo.png" : "assets/images/19/logo-ok.png" });
274
- chrome.pageAction.setTitle({ tabId: id, title: state ? "open IITC" : "Intel Ingress Enable is Activated" });
275
- chrome.pageAction[state](id);
276
- } */
277
188
function isIngressUrl ( url ) {
278
189
if ( url ) {
279
190
return ( / i n t e l .i n g r e s s .c o m / . test ( url ) )
0 commit comments