-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
content.js
45 lines (39 loc) · 1.54 KB
/
content.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
/** @type {HTMLScriptElement} */
let $settings
// Get initial config and inject it and the main script into the Twitter page
chrome.storage.local.get((/** @type {Partial<import("./types").Config>} */ storedConfig) => {
// Update deprecated config values
// @ts-ignore
if (storedConfig.twitterBlueChecks == 'dim') {
storedConfig.twitterBlueChecks = 'replace'
}
$settings = document.createElement('script')
$settings.type = 'text/json'
$settings.id = 'tnt_settings'
document.documentElement.appendChild($settings)
$settings.innerText = JSON.stringify(storedConfig)
let $main = document.createElement('script')
$main.src = chrome.runtime.getURL('script.js')
$main.onload = function() {
this.remove()
}
document.documentElement.appendChild($main)
chrome.storage.onChanged.addListener(onConfigChange)
})
// Inject config changes from options pages into the settings <script>
function onConfigChange(changes) {
let configChanges = Object.fromEntries(
Object.entries(changes).map(([key, {newValue}]) => [key, newValue])
)
$settings.innerText = JSON.stringify(configChanges)
}
// Store config changes sent from the injected script
window.addEventListener('message', (event) => {
if (event.source !== window) return
if (event.data.type === 'tntConfigChange' && event.data.changes) {
chrome.storage.onChanged.removeListener(onConfigChange)
chrome.storage.local.set(event.data.changes, () => {
chrome.storage.onChanged.addListener(onConfigChange)
})
}
}, false)