-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
74 lines (70 loc) · 1.99 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
// Registering a command listener to open the popup via shortcuts
messenger.commands.onCommand.addListener(commandListener);
console.log("Registering composeScripts");
messenger.composeScripts.register({
css: [],
js: [
{file: "compose/rules.js"},
{file: "compose/compose.js"}
]
});
async function commandListener(cmd) {
switch (cmd) {
case "open_compose_action_popup":
console.log("opening popup");
messenger.composeAction.openPopup();
break;
case "toggle_smart_quote":
console.log("toggle smart quote");
messenger.storage.local.set({
smartQuoteActive: !(await messenger.storage.local.get({smartQuoteActive: true})).smartQuoteActive
});
ports.forEach(function(port){
port.postMessage({command: "toggleSmartQuoteActive"});
});
break;
}
}
// collect ports to composer scripts to be able to talk to them
const ports = [];
messenger.runtime.onConnect.addListener(function(port){
ports.push(port);
port.onDisconnect.addListener(function(){
const index = ports.indexOf(port);
if (index !== -1){
ports.splice(index, 1);
}
});
port.onMessage.addListener(function(message){
console.log(port, message);
switch (message.command){
case "updateIcon":
messenger.composeAction.setIcon({
tabId: port.sender.tab.id,
path: message.smartQuoteActive? "images/iconActive.svg": "images/iconInactive.svg"
});
break;
}
});
});
messenger.runtime.onMessage.addListener(function(message, sender, sendResponse){
switch (message.command){
case "proxyToTab":
ports.forEach(function(port){
if (port.sender.tab.id === message.tabId){
console.log("proxy to tab", message);
message.message.id = Math.random();
port.onMessage.addListener(function listener(answer){
console.log("tab answered", answer);
if (answer.id === message.message.id){
port.onMessage.removeListener(listener);
delete answer.id;
sendResponse(answer);
}
});
port.postMessage(message.message);
}
});
return true;
}
});