Skip to content

Commit a27d8fc

Browse files
authored
Fix multiple pinned ingress tabs
* Fix multiple pinned ingress tabs #11 * Fix (only in Firefox) js errors by gen_dashboard_... #8
1 parent 1595b43 commit a27d8fc

File tree

7 files changed

+136
-200
lines changed

7 files changed

+136
-200
lines changed

app/popup/popup.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@ let app = new Vue({
3333
return ((typeof obj !== 'object') || (Object.keys(obj).length === 0))
3434
},
3535
'openIITC': function () {
36-
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
37-
chrome.runtime.sendMessage({'type': "requestOpenIntel", 'tab': tabs[0].id});
38-
window.close();
39-
})
36+
chrome.runtime.sendMessage({'type': "requestOpenIntel"});
37+
window.close();
4038
},
4139
'toggleIITC': function () {
4240
let checkedValue = this.IITC_is_enabled;

manifest.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@
3232
],
3333
"persistent": false
3434
},
35+
"content_scripts": [
36+
{
37+
"matches" : ["https://intel.ingress.com/*"],
38+
"run_at": "document_start",
39+
"js": ["scripts/loader.js"]
40+
}
41+
],
3542
"browser_action": {
3643
"browser_style": true,
3744
"default_popup": "app/popup/popup.html",

scripts/background.js

Lines changed: 59 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
let activeIITCTab = null;
1+
let lastIITCTab = null;
22
const {
3-
onActivated,
43
onUpdated,
54
onRemoved
65
} = chrome.tabs;
76
// tab
8-
onActivated.addListener(onActivatedListener);
97
onUpdated.addListener(onUpdatedListener);
108
onRemoved.addListener(onRemovedListener);
119

1210
chrome.runtime.onMessage.addListener(function(request) {
1311
switch (request.type) {
1412
case "requestOpenIntel":
15-
onRequestOpenIntel(request.tab).finally();
13+
onRequestOpenIntel().finally();
1614
break;
1715
case "toggleIITC":
1816
onToggleIITC(request.value).finally();
@@ -29,61 +27,38 @@ chrome.webNavigation.onBeforeNavigate.addListener(
2927
{url: [{pathSuffix: ".user.js"}]}
3028
);
3129

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;
4030

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);
5237
}
5338
}
54-
if (active) {
55-
if (isIngressUrl(url)) {
56-
console.log('detected ingress.com/intel page on active tab %d', id);
57-
return;
58-
}
5939

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+
});
6746
}
6847

6948
async function onToggleIITC(value) {
7049
chrome.storage.local.set({'IITC_is_enabled': value}, async function() {
71-
if (activeIITCTab) {
72-
let isActive = false;
7350

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) {
7956

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);
8559
}
86-
}
60+
61+
});
8762
});
8863

8964
}
@@ -92,66 +67,26 @@ async function onToggleIITC(value) {
9267
// tab listeners
9368
async function onUpdatedListener(tabId, status) {
9469
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-
11070
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);
11474
console.log('requested iitc launch');
115-
console.log('initializing iitc');
116-
initialize(tabId);
75+
initialize();
76+
lastIITCTab = tabId;
11777
}
11878
}
119-
12079
return false;
12180
}
12281
}
12382

12483
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;
14686
}
147-
148-
const {
149-
active
150-
} = await getTabInfo(tabId);
151-
//if (active) chrome.pageAction.show(tabId);
15287
}
15388

154-
function initialize(tabId) {
89+
function initialize() {
15590

15691
chrome.storage.local.get([
15792
"IITC_is_enabled",
@@ -169,21 +104,15 @@ function initialize(tabId) {
169104
let iitc_version = data[channel+'_iitc_version'];
170105
if ((status === undefined || status === true) && iitc_code !== undefined) {
171106

172-
chrome.tabs.executeScript(tabId, {
173-
runAt: "document_end",
174-
file: './scripts/pre.js'
175-
}, () => {
176107
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);
180109

181110
let plugins_local = data[channel+'_plugins_local'];
182111
if (plugins_local !== undefined) {
183112
Object.keys(plugins_local).forEach(function(id) {
184113
let plugin = plugins_local[id];
185114
if (plugin['status'] === 'on') {
186-
loadJS(tabId, "document_end", id, preparationUserScript(plugin, id));
115+
injectUserScript(preparationUserScript(plugin, id));
187116
}
188117
});
189118
}
@@ -193,41 +122,42 @@ function initialize(tabId) {
193122
Object.keys(plugins_user).forEach(function(id) {
194123
let plugin = plugins_user[id];
195124
if (plugin['status'] === 'on') {
196-
loadJS(tabId, "document_end", id, preparationUserScript(plugin, id));
125+
injectUserScript(preparationUserScript(plugin, id));
197126
}
198127
});
199128
}
200129

201-
202-
});
130+
203131

204132
}
205133
});
206134

207135
}
208136

209137

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+
});
228159
}
229-
console.info('plugin %s loaded', id);
230-
if (callback) callback();
160+
231161
});
232162

233163
}
@@ -240,10 +170,9 @@ function setTabActive(tabId) {
240170
setWindowFocused(tab.windowId)
241171
} catch (e) {
242172
console.log(e);
243-
activeIITCTab = null;
173+
lastIITCTab = null;
244174
console.log('repeated click with updated params');
245-
let id = await getActiveTab();
246-
onRequestOpenIntel(id);
175+
onRequestOpenIntel().finally();
247176
}
248177
});
249178
}
@@ -256,24 +185,6 @@ function getTabInfo(tabId) {
256185
return new Promise(resolve => chrome.tabs.get(tabId, resolve));
257186
}
258187

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-
} */
277188
function isIngressUrl(url) {
278189
if (url) {
279190
return (/intel.ingress.com/.test(url))

scripts/defaults.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,4 @@ let network_host = {
33
'release': "https://iitc.modos189.ru/build/release",
44
'test': "https://iitc.modos189.ru/build/test",
55
'local': "http://127.0.0.1:8000"
6-
}
7-
8-
let loaded_plugins = [];
6+
}

0 commit comments

Comments
 (0)