-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
102 lines (85 loc) · 2.48 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
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
const DATA_URL = "https://rtbf.ir/data/data.json";
const DIFFICULTY_COLORS = {
"impossible-label": "#000000",
"hard-label": "#a32100",
"easy-label": "#129141",
"medium-label": "#ffa800",
};
const DIFFICULTY_IMAGES = {
"impossible-label": "/assets/images/d_impossible_logo.png",
"hard-label": "/assets/images/d_hard_logo.png",
"easy-label": "/assets/images/d_easy_logo.png",
"medium-label": "/assets/images/d_medium_logo.png",
};
function changeIcon(difficulty, tabId) {
browser.browserAction.setIcon({ path: DIFFICULTY_IMAGES[difficulty], tabId });
}
function hideSpinner() {
document.querySelector(".spinner-container").style.display = "none";
}
function showDifficulty(item, tabId) {
const {
difficulty: difficultyLabel,
keytype: difficulty,
info,
deleteurl,
name,
} = item;
changeIcon(difficulty, tabId);
document.querySelector(".difficulty-text").innerText = difficultyLabel;
document.querySelector(".difficulty-text").style.backgroundColor =
DIFFICULTY_COLORS[difficulty];
document
.querySelector(".difficulty-info")
.insertAdjacentHTML("afterbegin", info);
if (deleteurl !== "#") {
document.querySelector(".remove-button").style.display = "block";
document.querySelector(".remove-button").href = "https://" + deleteurl;
}
document.querySelector(".service-name").innerText = 'در "' + name + '"';
hideSpinner();
document.querySelector(".difficulty-container").style.display = "flex";
}
function showNotSupported() {
hideSpinner();
document.querySelector(".not-supported-container").style.display = "block";
}
browser?.tabs?.query(
{
active: true,
lastFocusedWindow: true,
},
function (tabs) {
const currentUrl = tabs[0].url;
const urlObj = new URL(currentUrl);
const domain = urlObj.hostname;
const tabId = tabs[0].id;
fetch(DATA_URL)
.then((response) => response.json())
.then((websites) => {
for (let item of websites) {
if (domain === item.website) {
showDifficulty(item, tabId);
return;
}
}
showNotSupported();
})
.catch((error) => {
console.error(
"There has been a problem with fetch websites data:",
error
);
});
}
);
document.addEventListener("DOMContentLoaded", function () {
var closeButton = document.getElementById("closeButton");
closeButton.addEventListener(
"click",
function () {
window.close();
},
false
);
});