-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbackground.js
51 lines (47 loc) · 1.38 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
"use strict";
const CODE_SHOW_SNOWFLAKE = "(" + function() {
const SNOWFLAKE = "\u2744 ️";
if (!document.title.startsWith(SNOWFLAKE)) {
document.title = SNOWFLAKE + document.title;
// Eventually restore the title, in case tabs.discard() does not
// work. For instance, if a "beforeunload" event exists.
setTimeout(() => {
if (document.title.startsWith(SNOWFLAKE)) {
document.title = document.title.replace(SNOWFLAKE, "");
}
}, 500);
}
} + ")();";
var prependSnowflake = false;
browser.menus.create({
id: "discard",
type: "normal",
title: browser.i18n.getMessage("menuItemDiscard"),
contexts: ["tab"]
});
browser.menus.onClicked.addListener(async (info, tab) => {
if (tab.discarded) {
return;
}
if (prependSnowflake) {
// Try to prepend a snowflake before the title,
// before discarding the tab.
try {
await browser.tabs.executeScript(tab.id, {
runAt: "document_start",
code: CODE_SHOW_SNOWFLAKE,
});
} catch (e) {
// This can happen if the tab is a privileged page, e.g. about:addons.
}
}
browser.tabs.discard(tab.id);
});
browser.storage.onChanged.addListener((changes) => {
if (changes.prependSnowflake) {
prependSnowflake = changes.prependSnowflake.newValue;
}
});
browser.storage.local.get("prependSnowflake", (items) => {
prependSnowflake = !!items.prependSnowflake;
});