-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
86 lines (77 loc) · 2.22 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
/////////////////////////////////////////////////
/**
* @param {string} url
*/
async function updateMainTab(url) {
console.log("7. updating main tab to: ", url)
const mainTab = await getMainTab()
return await browser.tabs.update(mainTab.id, { url: url })
}
/////////////////////////////////////////////////
/////////////////////////////////////////////////
/**
* @param {browser.tabs.Tab} newTab
*/
function keepOneTab(newTab) {
console.log("1. new tab has opened")
// check if the new tab is actually a new window
getMainTab()
.then((mainTab) => {
if (mainTab.id === newTab.id) {
console.log("2. new window")
return "new window"
} else {
console.log("2. regular new tab")
return "new tab"
}
})
.then((theSituation) => {
console.log("3. dealing with the situation!")
function onUpdate(id, update) {
console.log("5. new tab has been updated")
if (isWebsite(update.url)) {
browser.tabs.remove(id).then(() => {
updateMainTab(update.url)
})
}
}
// if it's actually a regular new tab, add listener for url updates:
if (theSituation === "new tab") {
console.log("4. adding new onUpdate event listener to tab ", newTab.id)
browser.tabs.onUpdated.addListener(onUpdate, {
tabId: newTab.id,
properties: ["url"],
})
}
})
}
////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////
async function getMainTab() {
const allTabs = await browser.tabs.query({ currentWindow: true })
return allTabs[0]
}
///////////////////////////////////////////////////////////////
/**
*
* @param {string} url
* @returns {boolean}
*/
function isWebsite(url) {
if (url.startsWith("http")) {
console.log("6. it's a website")
return true
} else {
console.log("6. it's not a website")
return false
}
}
// /**
// * @param {browser.tabs.Tab} newTab
// */
// function closePreviousTab(newTab) {
// console.log(browser.sessions.)
// // browser.tabs.remove(newTab.openerTabId)
// }
browser.tabs.onCreated.addListener(keepOneTab)
// doesn't work when opening extension settings!!!!!!!!!