-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
67 lines (63 loc) · 2.6 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
const visibilityToggles = {
togglePartTitleBreadcrumb: ".part-title, .breadcrumb",
toggleLevel: "td.level",
toggleFinishedCount: "td.finished-count",
toggleAcceptanceRate: "td.acceptance-rate",
};
document.addEventListener("DOMContentLoaded", function () {
// 프로그래머스 문제 풀이 사이트인지 확인하는 로직
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
const currentTabUrl = tabs[0].url;
if (currentTabUrl.includes("school.programmers.co.kr")) {
document.getElementById("toggleContainer").style.display = "block";
document.getElementById("otherSite").style.display = "none";
} else {
document.getElementById("toggleContainer").style.display = "none";
document.getElementById("otherSite").style.display = "block";
}
});
// 깃허브 링크로 이동시키는 로직
var link = document.getElementById("github");
link.addEventListener("click", function () {
chrome.tabs.create({ url: this.href });
});
// 로컬에서 상태 불러와서 popup에 반영하는 로직
chrome.storage.local.get("visibilitySettings", function (data) {
if (data.visibilitySettings) {
document.getElementById("togglePartTitleBreadcrumb").checked =
data.visibilitySettings[".part-title, .breadcrumb"];
document.getElementById("toggleLevel").checked =
data.visibilitySettings["td.level"];
document.getElementById("toggleFinishedCount").checked =
data.visibilitySettings["td.finished-count"];
document.getElementById("toggleAcceptanceRate").checked =
data.visibilitySettings["td.acceptance-rate"];
}
});
});
Object.entries(visibilityToggles).forEach(([buttonId, selector]) => {
document.getElementById(buttonId).addEventListener("change", (event) => {
const isHidden = event.target.checked;
updateVisibilitySettings(selector, isHidden);
});
});
function updateVisibilitySettings(selector, isHidden) {
chrome.tabs.query({ active: true, currentWindow: true }, async (tabs) => {
await chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
function: toggleElementsVisibility,
args: [selector, isHidden],
});
});
}
function toggleElementsVisibility(selector, isHidden) {
const elements = document.querySelectorAll(selector);
elements.forEach((element) => {
element.style.visibility = isHidden ? "hidden" : "visible";
});
chrome.storage.local.get("visibilitySettings", (data) => {
const settings = data.visibilitySettings || {};
settings[selector] = isHidden;
chrome.storage.local.set({ visibilitySettings: settings });
});
}