-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
60 lines (52 loc) · 1.82 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
// A global variable is created for better readability
var extensionStatus
// Styles for the badge in the extension's icon
function badgeOn() {
chrome.action.setBadgeText({ text: 'on' })
chrome.action.setBadgeTextColor({ color: '#FFFFFF' })
chrome.action.setBadgeBackgroundColor({ color: '#2fc439' })
}
function badgeOff() {
chrome.action.setBadgeText({ text: 'off' })
chrome.action.setBadgeTextColor({ color: '#FFFFFF' })
chrome.action.setBadgeBackgroundColor({ color: '#c73434' })
}
// Save the new status of the extension (ON or OFF) in the local storage of the browser
// Also updates the global variable and apply the styles
function enableExtension() {
chrome.storage.local.set({ status: 'enabled' }).then(() => {
extensionStatus = 'enabled'
badgeOn()
})
}
function disableExtension() {
chrome.storage.local.set({ status: 'disabled' }).then(() => {
extensionStatus = 'disabled'
badgeOff()
})
}
// Change the URL of the current tab to the New Tab page
function changeCurrentTabUrl() {
let tab = chrome.tabs.getCurrent().then(() => {
chrome.tabs.update(tab.id, { url: "chrome://newtab" })
})
}
// Switcher to turn ON and OFF the extension when its icon is clicked
chrome.action.onClicked.addListener(() => {
if (extensionStatus === 'disabled') {
enableExtension()
} else {
disableExtension()
}
})
// Check the last saved status (ON or OFF) when a new window is opened
// And change the URL to the New Tab page if the extension was enabled
// Also updates the global variable and the badge's styles
chrome.windows.onCreated.addListener(() => {
chrome.storage.local.get(['status']).then((value) => {
extensionStatus = value.status
if (extensionStatus === 'enabled') { changeCurrentTabUrl(); badgeOn() }
else if (extensionStatus === 'disabled') { badgeOff() }
else { changeCurrentTabUrl(); enableExtension() }
})
})