-
Notifications
You must be signed in to change notification settings - Fork 40
/
background.js
65 lines (58 loc) · 2.02 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
const browserAppData = this.browser || this.chrome;
const tabs = {};
const inspectFile = 'inspect.js';
const activeIcon = 'active-64.png';
const defaultIcon = 'default-64.png';
const inspect = {
toggleActivate: (id, type, icon) => {
this.id = id;
browserAppData.tabs.executeScript(id, { file: inspectFile }, () => { browserAppData.tabs.sendMessage(id, { action: type }); });
browserAppData.browserAction.setIcon({ tabId: id, path: { 19: 'icons/' + icon } });
}
};
function isSupportedProtocolAndFileType(urlString) {
if (!urlString) { return false; }
const supportedProtocols = ['https:', 'http:', 'file:'];
const notSupportedFiles = ['xml', 'pdf', 'rss'];
const extension = urlString.split('.').pop().split(/\#|\?/)[0];
const url = document.createElement('a');
url.href = urlString;
return supportedProtocols.indexOf(url.protocol) !== -1 && notSupportedFiles.indexOf(extension) === -1;
}
function toggle(tab) {
if (isSupportedProtocolAndFileType(tab.url)) {
if (!tabs[tab.id]) {
tabs[tab.id] = Object.create(inspect);
inspect.toggleActivate(tab.id, 'activate', activeIcon);
} else {
inspect.toggleActivate(tab.id, 'deactivate', defaultIcon);
for (const tabId in tabs) {
if (tabId == tab.id) delete tabs[tabId];
}
}
}
}
function deactivateItem(tab) {
if (tab[0]) {
if (isSupportedProtocolAndFileType(tab[0].url)) {
for (const tabId in tabs) {
if (tabId == tab[0].id) {
delete tabs[tabId];
inspect.toggleActivate(tab[0].id, 'deactivate', defaultIcon);
}
}
}
}
}
function getActiveTab() {
browserAppData.tabs.query({ active: true, currentWindow: true }, tab => { deactivateItem(tab); });
}
browserAppData.commands.onCommand.addListener(command => {
if (command === 'toggle-xpath') {
browserAppData.tabs.query({ active: true, currentWindow: true }, tab => {
toggle(tab[0]);
});
}
});
browserAppData.tabs.onUpdated.addListener(getActiveTab);
browserAppData.browserAction.onClicked.addListener(toggle);