-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
135 lines (112 loc) · 3.8 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const activeTabs = {};
let activeTimer = null;
let previousText = null;
// TODO settings page
const settings = {
timerInterval: 300,
reloadPersist: true,
fetchHttp: false,
url: "http://localhost:2653",
};
function contentScript() {
browser.runtime.onMessage.addListener(function onMessage(req) {
switch (req.action) {
case "eject":
browser.runtime.onMessage.removeListener(onMessage);
break;
case "insert":
const el = document.createElement("p");
el.classList.add("clipboard-inserted");
el.textContent = req.content;
document.body.appendChild(el);
break;
}
});
}
function activateTimer() {
activeTimer = setInterval(() => {
const textPromise = settings.fetchHttp
? fetch(settings.url, { mode: "no-cors" })
.then(res => {
if (!res.ok) {
return Promise.reject(new Error(`error querying ${settings.url}`));
}
return res.text();
})
: navigator.clipboard.readText();
textPromise
.then(content => {
if (content === null || content.length === 0 || content === previousText) return;
previousText = content;
Object.keys(activeTabs).map(x => parseInt(x)).forEach(id => browser.tabs.sendMessage(id, {
action: "insert", content
}));
})
//TODO show error to user
.catch(console.error);
}, parseInt(settings.timerInterval));
}
function activate(tabId, url) {
browser.scripting.executeScript({ target: { tabId }, func: contentScript });
activeTabs[tabId] = url;
browser.browserAction.setBadgeBackgroundColor({ tabId, color: "green" });
browser.browserAction.setBadgeText({ tabId, text: "ON" });
if (activeTimer === null) {
activateTimer();
}
}
function deactivate(tabId, dead = false, completelyDead = false) {
delete activeTabs[tabId];
if (!completelyDead) {
browser.browserAction.setBadgeText({ tabId, text: "" });
}
if (!dead) {
browser.tabs.sendMessage(tabId, { action: "eject" });
}
if (Object.keys(activeTabs).length === 0 && activeTimer !== null) {
clearInterval(activeTimer);
activeTimer = null;
}
}
const confKeys = ["timerInterval", "reloadPersist", "fetchHttp", "url"]
function updateSettings(data) {
confKeys
.filter(k => data.hasOwnProperty(k))
.forEach(k => settings[k] = data[k]);
}
browser.storage.local.get(confKeys).then(updateSettings);
browser.storage.onChanged.addListener((changes, area) => {
if (area === "local") {
confKeys
.filter(k => changes.hasOwnProperty(k))
.forEach(k => settings[k] = changes[k].newValue);
}
});
browser.browserAction.onClicked.addListener(() => {
browser.tabs.query({ active: true, currentWindow: true })
.then(tabs => {
const cur = tabs[0];
if (!activeTabs.hasOwnProperty(cur.id)) {
activate(cur.id, cur.url);
} else {
deactivate(cur.id)
}
});
});
browser.tabs.onRemoved.addListener((tabId, removeInfo) => {
if (activeTabs.hasOwnProperty(tabId)) {
deactivate(tabId, true, true);
}
});
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
// deactivate when we change url
if (changeInfo.url) {
if (activeTabs.hasOwnProperty(tabId)) {
if (!settings.reloadPersist || activeTabs[tabId] !== changeInfo.url) {
deactivate(tabId, true);
} else {
activate(tabId, changeInfo.url);
}
}
}
});