-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
90 lines (73 loc) · 2.05 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
/*
Default settings. If there is nothing in storage, use these values.
*/
var defaultSettings = {
onActivated: true,
onClosed: true,
onWindowClosed: false,
};
var settingsCache = defaultSettings;
function cacheStoredSettings(storedSettings) {
settingsCache = storedSettings
}
/*
On startup, check whether we have stored settings.
If we don't, then store the default settings.
*/
async function checkStoredSettings() {
const storedSettings = await browser.storage.local.get()
console.log('checkStoredSettings', storedSettings)
if (!Object.keys(storedSettings).length) {
browser.storage.local.set(defaultSettings);
} else {
cacheStoredSettings(storedSettings)
}
}
checkStoredSettings()
browser.storage.onChanged.addListener(async (_changes, area) => {
console.log('onChanged', _changes)
if (area === "local") {
checkStoredSettings()
}
})
var allTabs;
browser.tabs.query({}).then((tabs) => allTabs = tabs);
async function bump(url) {
if (!url.match(/^about:/)) {
await browser.history.addUrl(
{
url: url,
transition: "auto_bookmark"
}
)
console.log("bumped", url);
}
allTabs = await browser.tabs.query({});
}
browser.tabs.onActivated.addListener(async (activeInfo) => {
if (settingsCache.onActivated) {
console.log("onActivated", activeInfo);
const tab = await browser.tabs.get(activeInfo.tabId);
bump(tab.url)
}
}
);
browser.windows.onFocusChanged.addListener(async (windowId) => {
if (settingsCache.onActivated) {
console.log("Newly focused window: ", windowId);
const win = await browser.windows.get(windowId);
if (win.type == "normal") {
let tabs = await browser.tabs.query({ active: true, windowId });
bump(tabs[0].url);
}
}
});
browser.tabs.onRemoved.addListener((tabId, removeInfo) => {
if (settingsCache.onClosed) {
if (!removeInfo.isWindowClosing || settingsCache.onWindowClosed) {
const removedTab = allTabs.find(t => t.id == tabId);
console.log("onRemoved", removedTab.title, removeInfo)
bump(removedTab.url)
}
}
})