/
worker.js
36 lines (32 loc) · 976 Bytes
/
worker.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
/**
* Helper function to retrieve the currently opened tab
* @returns {url: string} an object containing the url (and other stuff)
*/
async function getCurrentTab() {
let queryOptions = { active: true, currentWindow: true };
let [tab] = await chrome.tabs.query(queryOptions);
return tab;
}
/**
* Registers the swipe-handler script in the current tab
* @param {} tab currently active tab
*/
async function register(tab) {
const { tabId } = tab;
// catch Settings Pages
const { url } = await getCurrentTab();
if (url.startsWith("chrome://") || url.startsWith("edge://")) {
return;
}
// execute the script
chrome.scripting.executeScript({
target: { tabId },
files: ["swipe-handler.js"],
});
}
// register on tab activation (i.e. tab change)
chrome.tabs.onActivated.addListener((tab) => register(tab));
// register on tab update (i.e. refresh)
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) =>
register({ tabId, tab })
);