-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbackground.js
More file actions
33 lines (25 loc) · 981 Bytes
/
background.js
File metadata and controls
33 lines (25 loc) · 981 Bytes
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
chrome.tabs.onCreated.addListener(tabToWindow)
chrome.action.onClicked.addListener(onClick)
chrome.commands.onCommand.addListener(async (_, tab) => {
if (tab) onClick(tab)
})
async function tabToWindow({ id: tabId, windowId }) {
const { tab2window } = await chrome.storage.local.get()
if (!tab2window) return
const tabs = await chrome.tabs.query({ windowId })
if (tabs.length < 2) return
await chrome.windows.create({ focused: true, tabId })
}
async function onClick({ id, url }) {
const host = (new URL(url)).hostname
chrome.storage.local.get({ blockListUrls: [] }, ({ blockListUrls }) => {
const index = blockListUrls.indexOf(host);
(index === -1) ? blockListUrls.push(host) : blockListUrls.splice(index, 1)
chrome.storage.local.set({ blockListUrls })
chrome.tabs.reload(id)
});
}
chrome.runtime.onInstalled.addListener(openOptions)
function openOptions(details) {
if (details?.reason === 'install') chrome.runtime.openOptionsPage()
}