-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
36 lines (32 loc) · 1.12 KB
/
popup.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
const $ = (v) => document.querySelector(v);
const $$ = (v) => document.querySelectorAll(v);
let changeColorBtn = $$(".changeColor");
chrome.storage.sync.get("bgcolor", function (data) {
const { bgcolor } = data;
for (let i = 0; i < bgcolor.length; i++) {
changeColorBtn[i].style.backgroundColor = bgcolor[i];
}
const randomIdx = randomNum(bgcolor.length);
const code = `document.body.style.backgroundColor = "${bgcolor[randomIdx]}";
document.querySelector("#contents").style.color = "#eee";`;
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.executeScript(tabs[0].id, {
code: `${code}`,
});
});
});
for (let i = 0; i < changeColorBtn.length; i++) {
changeColorBtn[i].addEventListener("click", function (e) {
chrome.storage.sync.get("bgcolor", function (data) {
const { bgcolor } = data;
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.executeScript(tabs[0].id, {
code: `document.body.style.backgroundColor = "${bgcolor[i]}";`,
});
});
});
});
}
function randomNum(length) {
return Math.floor(Math.random() * length)
}