-
Notifications
You must be signed in to change notification settings - Fork 23
/
background.js
72 lines (66 loc) · 2.66 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const browserAction = typeof browser == 'object' ? chrome.browserAction : chrome.action; // Browser compatibility
class onClickListener {
constructor(callback) {
const CONTROL_TIME = 500; // Max time between click events occurrence
let click = 0;
let timer;
if (callback && callback instanceof Function) {
return tab => {
click += 1;
clearTimeout(timer);
timer = setTimeout(() => {
// Clear all timers
clearTimeout(timer);
callback.apply(this, [tab, click]);
click = 0;
}, CONTROL_TIME);
};
}
throw new Error('[InvalidArgumentException]');
}
}
let debugMode = '';
let odooVersion = 'legacy';
const onClickActivateDebugMode = (tab, click) => {
if (click <= 2) {
const debugOptions = {
0: [odooVersion === 'legacy' ? '' : '0', '/images/icons/off_48.png'],
1: ['1', '/images/icons/on_48.png'],
2: ['assets', '/images/icons/super_48.png'],
};
const selectedMode = debugMode && click === 1 ? 0 : click;
const tabUrl = new URL(tab.url);
const [debugOption, path] = debugOptions[selectedMode];
const params = new URLSearchParams(tabUrl.search);
params.set('debug', debugOption);
const url = tabUrl.origin + tabUrl.pathname + `?${params.toString()}` + tabUrl.hash;
browserAction.setIcon({ path });
chrome.tabs.update(tab.id, { url });
}
}
const adaptIcon = () => {
chrome.tabs.query({active: true, currentWindow: true}, tabs => {
if (tabs.length) {
chrome.tabs.sendMessage(tabs[0].id, {message: 'getOdooDebugInfo'}, response => {
if (chrome.runtime.lastError) {
return;
}
let path = '/images/icons/off_48.png';
if (response.odooVersion) {
if (response.debugMode === 'assets') {
path = '/images/icons/super_48.png';
} else if (response.debugMode === '1') {
path = '/images/icons/on_48.png';
}
odooVersion = response.odooVersion;
debugMode = response.debugMode;
}
browserAction.setIcon({ path });
});
}
});
}
browserAction.onClicked.addListener(new onClickListener((tab, click) => onClickActivateDebugMode(tab, click)));
chrome.tabs.onActivated.addListener(adaptIcon);
chrome.tabs.onUpdated.addListener(adaptIcon);
chrome.windows.onFocusChanged.addListener(adaptIcon);