forked from tarnas14/vimflowy
-
Notifications
You must be signed in to change notification settings - Fork 9
/
contentscript_idle.js
47 lines (45 loc) · 1.62 KB
/
contentscript_idle.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
// Define an array of supported option keys, which we'll use to fetch the
// options from Chrome storage.
const SUPPORTED_OPTIONS = ['jkToEsc'];
// API that the content script uses to notify page scripts of option changes.
// The function dispatches the custom event defined above, passing the updated
// options as the event detail.
function sendOptions(options) {
window.dispatchEvent(
new CustomEvent('optionChangeEvent', { detail: { options }}),
);
}
// Register an event listener for changes to the extension's options in Chrome
// storage. When the options change, we extract the updated values and send them
// to the page scripts via a custom event.
chrome.storage.onChanged.addListener(function(changes, namespace) {
sendOptions(
Object.fromEntries(
Object.entries(changes).map(([k,v]) => [k, v.newValue])
)
)
});
var scripts = [
"keybindings.js",
"transparentKeybindings.js",
"vimflowy.js",
"options.js"
];
let scriptsLoaded = 0;
for (var i=0; i < scripts.length; i++)
{
var s = document.createElement('script');
s.src = chrome.extension.getURL(scripts[i]);
(document.head||document.documentElement).appendChild(s);
s.onload = function() {
scriptsLoaded += 1;
// Once all scripts have loaded, fetch the extension's options from
// Chrome storage and send them to the page scripts via a custom event.
if (scriptsLoaded == scripts.length) {
chrome.storage.sync.get(SUPPORTED_OPTIONS, (options) => {
sendOptions(options);
});
}
this.parentNode.removeChild(this);
};
}