-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
122 lines (101 loc) · 3.58 KB
/
content.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
122
const problemListKey = "algozenith_problems";
const newBookmark = window.location.href;
const observer = new MutationObserver(() => {
initBookmarkFeature();
});
observer.observe(document.body, { childList: true, subtree: true });
initBookmarkFeature();
function initBookmarkFeature() {
if (!onProblemsPage()) return;
const scoreElement = document.querySelector(
".ant-col.d-flex.flex-column.justify-content-center.align-items-start.me-2.css-19gw05y p.m-0.fs-6.problem_paragraph.fw-bold"
);
if (!scoreElement) {
console.error("Could not find the score element. Cannot add bookmark button.");
return;
}
if (document.getElementById("add-bookmark-button")) return;
const bookmarkButton = createBookmarkButton();
if (bookmarkButton) {
insertBookmarkButton(scoreElement, bookmarkButton);
}
}
function onProblemsPage() {
return window.location.pathname.startsWith("/problems");
}
function createBookmarkButton() {
if (document.getElementById("add-bookmark-button")) return null;
const button = document.createElement("img");
button.id = "add-bookmark-button";
button.src = chrome.runtime.getURL("assets/bookmark.png");
button.className = "btn_ref";
button.title = "Click to bookmark current problem";
button.style.cssText = `
height: 30px;
width: 30px;
cursor: pointer;
`;
button.addEventListener("click", handleBookmarkClick);
return button;
}
function insertBookmarkButton(referenceElement, button) {
const container = document.createElement("div");
container.className = "bookmark-button-container";
container.appendChild(button);
referenceElement.parentElement.insertAdjacentElement("afterend", container);
injectStyles();
}
function injectStyles() {
if (document.getElementById("bookmark-style")) return;
const style = document.createElement("style");
style.id = "bookmark-style";
style.textContent = `
.bookmark-button-container {
display: inline-block;
margin-left: 10px;
vertical-align: middle;
}
.ant-row.d-flex.gap-4.mt-3.css-19gw05y {
display: flex;
align-items: center;
gap: 10px;
}
.btn_ref:hover {
transform: scale(1.1);
}
`;
document.head.appendChild(style);
}
async function handleBookmarkClick() {
try {
const bookmarks = await fetchBookmarks();
const problemName = getProblemName();
const newBookmarkObj = { url: newBookmark, desc: problemName };
if (!bookmarks.some((bookmark) => bookmark.url === newBookmark)) {
await saveBookmark([...bookmarks, newBookmarkObj]);
console.log("Bookmark added:", newBookmarkObj);
} else {
console.log("Bookmark already exists for this URL.");
}
} catch (error) {
console.error("Error handling bookmark:", error);
}
}
function getProblemName() {
const problemElement = document.querySelector(".fw-bolder.problem_heading.fs-4");
return problemElement ? problemElement.textContent.trim() : "Problem Name Not Found";
}
function fetchBookmarks() {
return new Promise((resolve) => {
chrome.storage.sync.get([problemListKey], (data) => {
resolve(data[problemListKey] ? JSON.parse(data[problemListKey]) : []);
});
});
}
async function saveBookmark(bookmarks) {
try {
await chrome.storage.sync.set({ [problemListKey]: JSON.stringify(bookmarks) });
} catch (error) {
console.error("Error saving bookmark:", error);
}
}