-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathbackground.js
More file actions
78 lines (69 loc) · 2.07 KB
/
Copy pathbackground.js
File metadata and controls
78 lines (69 loc) · 2.07 KB
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
73
74
75
76
77
78
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.scripting.executeScript(
{
target: { tabId: id },
files: [inspectFile]
},
() => {
browserAppData.tabs.sendMessage(id, { action: type });
}
);
browserAppData.action.setIcon({ tabId: id, path: { 19: "icons/" + icon } });
}
};
function isSupportedProtocolAndFileType(urlString) {
if (!urlString) {
return false;
}
const url = new URL(urlString);
const supportedProtocols = ["https:", "http:", "file:"];
const notSupportedFiles = ["xml", "pdf", "rss"];
const extension = url.href.split(".").pop().split(/\#|\?/)[0];
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.action.onClicked.addListener(toggle);