This repository has been archived by the owner on Mar 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
injector.js
78 lines (67 loc) · 2.48 KB
/
injector.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
const scriptList = {
calculation: {
file: 'src/averageCalculator.js',
enabled: true
},
loadMarkbook: {
file: 'src/loadMarkbook.js',
enabled: true
},
selectors: {
file: 'src/selectors.js',
enabled: true
},
liveModification: {
file: 'src/liveMarkbookCalculator.js',
enabled: true
},
betterTableLayout: {
file: 'src/betterTableLayout.js',
enabled: true
},
percentages: {
file: 'src/percentages.js',
enabled: true
},
fixBgColor: {
file: 'src/fixBgColor.js',
enabled: true
}
};
const injectScript = name => {
let script = document.createElement('script'); // Create a script element
script.src = chrome.extension.getURL(name); // Grab the absolute path to the script via the Chrome API
(document.head || document.documentElement).appendChild(script); // Append the script to the <head> if it exists
script.onload = () => {
script.parentNode.removeChild(script); // Script removes itself off the page once loaded
};
};
const injectSettings = settings => {
let script = document.createElement('script'); // Create a script element
script.innerHTML = `window.settings = ${JSON.stringify(settings)}`; // Set the value of the settings
(document.head || document.documentElement).appendChild(script); // Append the script to the document
script.onload = () => {
script.parentNode.removeChild(script); // Script removes itself off the page once loaded
};
};
const injectScriptList = scriptList => {
chrome.storage.sync.get(null, settings => {
injectSettings(settings); // Give the DOM access to settings
// Disable scripts from loading if they are not needed
if (!settings.calculation && !settings.quickview)
scriptList.calculation.enabled = false;
if (!settings.liveModification)
scriptList.liveModification.enabled = false;
if (!settings.percentages)
scriptList.percentages.enabled = false;
if (!settings.betterTableLayout)
scriptList.betterTableLayout.enabled = false;
if (!settings.percentages && !settings.liveModification)
scriptList.selectors.enabled = false;
Object.keys(scriptList).forEach(script => {
if (scriptList[script].enabled)
injectScript(scriptList[script].file);
});
});
};
injectScriptList(scriptList);