-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopup.js
46 lines (42 loc) · 1.41 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
37
38
39
40
41
42
43
44
45
const NUM_OF_RESPONSES = 3;
const injectContentScript = async (tab) => {
try {
const { id } = tab;
const scriptResult = await chrome.scripting.executeScript(
{
target: { tabId: id },
files: ['content.js']
}
);
return scriptResult[0].result;
} catch (error) {
console.warn(error);
return undefined;
}
}
// Update the relevant fields with the new data.
const setDOMInfo = (result) => {
for (let i = 1; i <= NUM_OF_RESPONSES; i++) {
// Background Colors
document.getElementById(`row-1-col-${i}-square`).style.backgroundColor = result.sortedBgColors[i - 1] || '-';
document.getElementById(`row-1-col-${i}-text`).textContent = result.sortedBgColors[i - 1] || '-';
// Font Colors
document.getElementById(`row-2-col-${i}-square`).style.backgroundColor = result.sortedFontColors[i - 1] || '-';
document.getElementById(`row-2-col-${i}-text`).textContent = result.sortedFontColors[i - 1] || '-';
// Font Families
document.getElementById(`row-3-col-${i}-text`).textContent = result.sortedFontFamilies[i - 1] || '-';
}
};
// Once the DOM is ready...
window.addEventListener('DOMContentLoaded', () => {
// Query for the active tab...
chrome.tabs.query({
active: true,
currentWindow: true
}, async (tabs) => {
const scriptResult = await injectContentScript(tabs[0]);
if (scriptResult) {
setDOMInfo(scriptResult);
}
});
});