-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
109 lines (93 loc) · 2.52 KB
/
util.js
File metadata and controls
109 lines (93 loc) · 2.52 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
101
102
103
104
105
106
107
108
109
var injected = false;
function setStyle(sw) {
// console.log(`setStyle`);
sw.style.cssText = `display: block;
position: fixed;
top: 0px;
right: 0px;
height: 20px;
padding: 2px 10px;
background-color: yellow;
color: black;
z-index: 9999;
font-size: smaller;`;
sw.innerHTML = "" + window.innerWidth;
}
function createSWElement() {
// console.log(`createSWElement`);
let screenWidthElm = document.createElement("div");
screenWidthElm.id = "screenWidth";
setStyle(screenWidthElm);
return screenWidthElm;
}
function getSWElement() {
let screenWidthElm = document.querySelectorAll("#screenWidth")[0];
if (screenWidthElm) {
return screenWidthElm;
}
}
function displayElement(screenWidthElm) {
// console.log(`displayElement`);
document.body.appendChild(screenWidthElm);
}
function init() {
// console.log(`init`);
let screenWidthElm = getSWElement();
if (!screenWidthElm) {
screenWidthElm = createSWElement();
displayElement(screenWidthElm);
addListeners();
}
}
function finish() {
// console.log(`finish`);
let screenWidthElm = getSWElement();
if (screenWidthElm) {
let node = document.getElementById("screenWidth");
if (node.parentNode) {
node.parentNode.removeChild(node);
}
// console.log(`screenWidth should haven been removed`);
}
removeListeners();
}
function updateSW() {
let screenWidthElm = getSWElement();
if (screenWidthElm) {
screenWidthElm.innerHTML = "" + window.innerWidth;
}
// console.log(`updateSW => ${window.innerWidth}`);
}
function removeListeners() {
// console.log(`removeListeners`);
// window.removeEventListener("load", init);
window.removeEventListener("resize", updateSW);
window.removeEventListener("unload", finish);
}
function addListeners() {
// window.addEventListener("load", init);
window.addEventListener("resize", updateSW);
window.addEventListener("unload", finish);
}
if (!injected) {
chrome.runtime.onMessage.addListener(function (
request,
sender,
sendResponse
) {
// console.log(
// sender.tab
// ? "from a content script:" + sender.tab.url
// : "from the extension"
// );
// console.log(`status: ${request.status}`);
if (request.status === "OFF") {
sendResponse({ farewell: "goodbye" });
finish();
}
if (request.status === "ON") {
sendResponse({ hello: "hallo" });
init();
}
});
}