-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathbackground.js
More file actions
100 lines (91 loc) · 2.7 KB
/
background.js
File metadata and controls
100 lines (91 loc) · 2.7 KB
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
//#region Constants
const IS_SAFARI = location.protocol.startsWith('safari-web-extension:')
const DISABLED_ICONS = {
16: 'icons/icon16-disabled.png',
32: 'icons/icon32-disabled.png',
48: 'icons/icon48-disabled.png',
64: 'icons/icon64-disabled.png',
96: 'icons/icon96-disabled.png',
128: 'icons/icon128-disabled.png',
}
const ENABLED_ICONS = {
16: 'icons/icon16.png',
32: 'icons/icon32.png',
48: 'icons/icon48.png',
64: 'icons/icon64.png',
96: 'icons/icon96.png',
128: 'icons/icon128.png',
}
//#endregion
//#region Functions
/**
* @param {string} previous
* @param {string} current
* @param {string} threshold
*/
function crossesVersionThreshold(previous, current, threshold) {
return isVersionLessThan(previous, threshold) && !isVersionLessThan(current, threshold)
}
/**
* @param {string} v1
* @param {string} v2
*/
function isVersionLessThan(v1, v2) {
let a = v1.split('.').map(Number)
let b = v2.split('.').map(Number)
for (let i = 0; i < Math.max(a.length, b.length); i++) {
const diff = (a[i] || 0) - (b[i] || 0)
if (diff < 0) return true
if (diff > 0) return false
}
return false
}
function log(...messages) {
console.log('[background]', ...messages)
}
function updateToolbarIcon(enabled) {
let title = chrome.i18n.getMessage(enabled ? 'extensionName' : 'extensionNameDisabled')
if (chrome.runtime.getManifest().manifest_version == 3) {
chrome.action.setTitle({title})
if (!IS_SAFARI) {
chrome.action.setIcon({path: enabled ? ENABLED_ICONS : DISABLED_ICONS})
} else {
chrome.action.setBadgeText({text: enabled ? '' : '⏻'})
}
} else {
chrome.browserAction.setTitle({title})
chrome.browserAction.setIcon({path: enabled ? ENABLED_ICONS : DISABLED_ICONS})
}
}
//#endregion
//#region Events
chrome.runtime.onInstalled.addListener((details) => {
log('chrome.runtime.onInstalled', {details})
if (details.reason == 'install') {
chrome.tabs.create({
url: 'https://soitis.dev/control-panel-for-youtube/welcome',
})
}
else if (details.reason == 'update') {
let previous = details.previousVersion
let current = chrome.runtime.getManifest().version
let significantVersions = [
crossesVersionThreshold(previous, current, '1.31') && '1.31',
].filter(Boolean)
if (significantVersions.length > 0) {
chrome.tabs.create({
url: `https://soitis.dev/control-panel-for-youtube/updated?version=${significantVersions[0]}`,
active: false,
})
}
}
})
chrome.storage.local.onChanged.addListener((changes) => {
if (changes.enabled) {
updateToolbarIcon(changes.enabled.newValue)
}
})
//#endregion
chrome.storage.local.get({enabled: true}, ({enabled}) => {
updateToolbarIcon(enabled)
})