-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
121 lines (99 loc) · 2.98 KB
/
options.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
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
110
111
112
113
114
115
116
117
118
119
120
121
(async function() {
const CONSOLE_LOG = `console.log(state, link, speed);`;
const HIGHLIGHT = `
if (speed.fcp90 < 3000) return;
const new_el = document.createElement("div")
new_el.style.width = "100%";
new_el.style.height = "100%";
new_el.style.position = "absolute";
new_el.style.top = link.offsetTop + "px";
new_el.style.height = link.offsetHeight + "px";
new_el.style.left = link.offsetLeft + "px";
new_el.style.width = link.offsetWidth + "px";
new_el.style.pointerEvents = "none";
new_el.style.backgroundColor = "#ff0000";
new_el.style.opacity = 0.6;
if (link.offsetParent)
link.offsetParent.appendChild(new_el);
`
const FONT_COLOR = `
if (speed.fcp90 < 3000) return;
link.style.color = "red";
`
const WARNING_ICON = `
if (speed.fcp90 < 3000)
return;
link.style.whiteSpace = 'nowrap';
let lastTextNode = link;
while (lastTextNode && lastTextNode.nodeType != Node.TEXT_NODE) {
lastTextNode = lastTextNode.lastChild;
}
let wrapper = document.createElement('span');
wrapper.style.whiteSpace = 'nowrap';
let after = document.createElement('span');
after.style.cssText = \`
font-weight: bold;
color: red;
position: absolute;
\`;
after.textContent = '\\u26A0';
wrapper.append(after);
link.append(wrapper);
`;
function getState() {
return new Promise(resolve => {
chrome.storage.sync.get({state: null}, result => resolve(result.state));
});
}
// TODO: should this be fire & forget, or not?
// TODO: maybe throttle this?
function setState() {
if (state_.selected == "eval")
state_.eval = textarea_.value;
else if (state_.selected == 'log')
state_.eval = CONSOLE_LOG;
else if (state_.selected == 'highlight')
state_.eval = HIGHLIGHT;
else if (state_.selected == 'font_color')
state_.eval = FONT_COLOR;
else if (state_.selected == 'warning_icon')
state_.eval = WARNING_ICON;
return new Promise(resolve => {
chrome.storage.sync.set({state: state_}, result => {
chrome.runtime.sendMessage({updateCode: "true"}, (response) => resolve());
});
});
}
let state_ = await getState();
if (!state_) {
state_ = {
selected: "log",
eval: CONSOLE_LOG,
}
}
document.getElementById(state_.selected).checked = true;
let textarea_ = document.getElementById('textarea');
if (state_.selected == 'eval')
textarea_.value = state_.eval;
let timer_;
textarea_.onkeydown = (e) => {
if (timer_)
clearTimeout(timer_);
timer_ = setTimeout(function() {
timer_ = null;
setState();
}, 100);
};
textarea_.onfocus = (e) => {
document.getElementById('eval').checked = true;
selectedChanged('eval');
}
async function selectedChanged(selected) {
state_.selected = selected;
await setState();
}
document.getElementById("fieldset").addEventListener("change", (e) => {
let selected = e.target.id == 'textarea' ? 'eval' : e.target.id;
selectedChanged(selected);
});
})();