-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
64 lines (59 loc) · 2.23 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
// Register the message display script for all newly opened message tabs.
messenger.messageDisplayScripts.register({
js: [{ file: "messageDisplay/message-content-script.js" }],
css: [{ file: "messageDisplay/message-content-styles.css" }],
});
// Inject script and CSS in all already open message tabs.
let openTabs = await messenger.tabs.query();
let messageTabs = openTabs.filter(
tab => ["mail", "messageDisplay"].includes(tab.type)
);
for (let messageTab of messageTabs) {
browser.tabs.executeScript(messageTab.id, {
file: "messageDisplay/message-content-script.js"
})
browser.tabs.insertCSS(messageTab.id, {
file: "messageDisplay/message-content-styles.css"
})
}
/**
* Add a handler for the communication with other parts of the extension,
* like our message display script.
*
* Note: It is best practice to always define a synchronous listener
* function for the runtime.onMessage event.
* If defined asynchronously, it will always return a Promise
* and therefore answer all messages, even if a different listener
* defined elsewhere is supposed to handle these.
*
* The listener should only return a Promise for messages it is
* actually supposed to handle.
*/
messenger.runtime.onMessage.addListener((message, sender, sendResponse) => {
// Check what type of message we have received and invoke the appropriate
// handler function.
if (message && message.hasOwnProperty("command")) {
return commandHandler(message, sender);
}
// Return false if the message was not handled by this listener.
return false;
});
// The actual (asynchronous) handler for command messages.
async function commandHandler(message, sender) {
// Get the message currently displayed in the sending tab, abort if
// that failed.
const messageHeader = await messenger.messageDisplay.getDisplayedMessage(
sender.tab.id
);
if (!messageHeader) {
return;
}
// Check for known commands.
switch (message.command) {
case "getBannerDetails":
return { text: `Attenzione: questa mail urta la tua' sensibilita' di sveglione che mica ti fregano a te`};
break;
default:
break;
}
}