-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
76 lines (66 loc) · 1.87 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
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
(function() {
const STORAGE_KEY = "layer-toggle-enabled-0301314611"; // must be unique to extension and match background.js
const LAYER_ID = "layer-toggle-9694466260"; // must be unique to extension
const LAYER_CSS = `position: fixed !important;
inset: 0 !important;
padding: 0 !important;
margin: 0 !important;
z-index: 2147483647 !important;
transform: none !important;
pointer-events: none;
border: 0.25rem solid green;
color: green;
background: rgba(0, 255, 0, 0.1);
`;
/**
* @param {string} id
* @returns {HTMLElement}
*/
const createLayer = (id) => {
const layer = document.createElement("div");
layer.id = id;
layer.innerHTML = "<h2 style=\"display: inline-block; margin: 1rem; background: white;\">Layer Active</h2>"
layer.insertAdjacentHTML('afterbegin', `<style>
#${id} {
${LAYER_CSS}
}
#${id} ~ * {
z-index: 0 !important;
}
</style>`);
return layer;
};
/**
* @param {string} id
* @returns {HTMLElement}
*/
const getLayer = (id) => document.getElementById(id) || createLayer(id);
/**
*
* @param {HTMLElement} layer
* @returns {boolean}
*/
const isLayerActive = (layer) => document.body.contains(layer);
/**
* @param {HTMLElement} layer
* @param {boolean=} active
* @returns {void}
*/
const toggleLayer = (layer, active) => {
const currentlyActive = isLayerActive(layer);
let shouldActivateLayer = !currentlyActive;
if (typeof active === "boolean") {
shouldActivateLayer = active;
}
if (shouldActivateLayer === currentlyActive) return;
if (shouldActivateLayer) {
document.body.append(layer);
} else {
layer.remove();
}
};
chrome.storage.local.get(STORAGE_KEY, (result) => {
const layer = getLayer(LAYER_ID);
toggleLayer(layer, result[STORAGE_KEY]);
});
}());