-
Notifications
You must be signed in to change notification settings - Fork 3
/
background.js
164 lines (143 loc) · 4.11 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
"use strict";
import {RegexMatcher} from "./matcher.mjs";
// noinspection JSUnresolvedVariable
const tabs = browser.tabs;
if (!tabs)
throw new Error("Tabs are not available");
const sync = browser.storage.sync;
if (!sync)
throw new Error("Synchronized storage area is not found");
const backgroundPage = browser.runtime.getBackgroundPage();
if (!backgroundPage)
throw new Error("Background page is not found");
function debug() {
//console.debug.apply(console, arguments);
}
function handleError() {
const message = [];
message.push(...arguments);
if (arguments.length > 0) {
if (arguments[0].stack) {
message.push(arguments[0].stack);
}
}
console.error.apply(console, message);
}
function logFunction(name, ...args) {
debug(name, ...args);
}
debug("starting");
function isEligibleForSquash(pinnedTab, newTab) {
if (!pinnedTab.pinned)
return false;
if (pinnedTab.hidden)
return false;
if (newTab.hidden)
return false;
if (newTab.pinned)
return false;
return pinnedTab.id !== newTab.openerTabId;
}
async function getMatcher() {
const data = await sync.get("patterns");
let patterns = data.patterns;
if (!patterns) {
patterns = RegexMatcher.defaultPatterns();
}
patterns = RegexMatcher.convertLinesToRegExp(
patterns,
(line, error, pattern) => handleError('Error on line', line, pattern, error));
return new RegexMatcher(patterns, debug);
}
// Returns true if newTab should be squashed into pinnedTab
// Main business logic of this plugin
// Actual work is done by returned delegate
async function shouldSquashPredicate() {
let matcher = await getMatcher();
return (pinnedTab, newTab) => {
if (!isEligibleForSquash(pinnedTab, newTab))
return false;
return matcher.match(pinnedTab, newTab);
};
}
const watchedTabs = {};
async function reopenIn(originTab, targetTab) {
logFunction("reopenIn", originTab, targetTab);
try {
await tabs.remove(originTab.id);
} catch (e) {
handleError("Failed to remove a tab: ", e);
return;
}
const updateRequest = {
active: originTab.active
};
if (targetTab.url !== originTab.url)
updateRequest.url = originTab.url;
try {
await tabs.update(targetTab.id, updateRequest);
} catch (e) {
handleError("Failed to perform navigation", e);
}
}
async function findDuplicate(tab) {
logFunction("findDuplicate", tab);
if (!tab)
return null;
if (!tab.url)
return null;
if (tab.pinned)
return null;
const shouldSquash = await shouldSquashPredicate();
const tabQuery = {
pinned: true,
status: "complete",
windowType: "normal"
};
logFunction("query", tabQuery);
const pinnedTabs = await tabs.query(tabQuery);
for (let pinnedTab of pinnedTabs) {
if (tab === pinnedTab)
continue;
if (!shouldSquash(pinnedTab, tab))
continue;
debug("Matching pinned tab found:", tab, pinnedTab);
return pinnedTab;
}
debug("No matching pinned tab", tab, pinnedTabs);
}
async function tryReuseTab(tab) {
logFunction("tryReuseTab", tab);
const duplicate = await findDuplicate(tab);
if (duplicate) {
await reopenIn(tab, duplicate);
return true;
}
return false;
}
tabs.onCreated.addListener(tab => {
logFunction("onCreated", tab);
const tabId = tab.id;
watchedTabs[tabId] = {
active: tab.active,
created: tab.lastAccessed
};
setTimeout(() => delete watchedTabs[tabId], 10000);
});
tabs.onUpdated.addListener((tabId, change, tab) => {
if (!change.url)
return;
const watched = watchedTabs[tabId];
if (!watched) {
debug("Tab " + tabId + " is not watched");
return;
}
logFunction("onUpdated", tabId, change, tab);
tryReuseTab(tab)
.then((wasReused) => {
if (wasReused) delete watchedTabs[tabId];
})
.catch(e => handleError(e))
});
getMatcher().catch(handleError);
debug("startup complete");