-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
84 lines (65 loc) · 2.7 KB
/
content.js
File metadata and controls
84 lines (65 loc) · 2.7 KB
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
// content.js
const bookmarkImgURL = chrome.runtime.getURL("assets/bookmark.png");
const AZ_PROBLEM_KEY = "AZ_PROBLEM_KEY";
const observer = new MutationObserver(() => {
addBookmarkButton();
});
observer.observe(document.body, {childList: true, subtree: true});
addBookmarkButton();
function onProblemsPage(){
return window.location.pathname.startsWith('/problems/');
}
function addBookmarkButton() {
if(!onProblemsPage() || document.getElementById("add-bookmark-button")) return;
// ROBUST FIX: Find the "Ask Doubt" button by its text content
// This prevents the code from breaking if the class name changes
const buttons = Array.from(document.querySelectorAll('button'));
const askDoubtButton = buttons.find(btn => btn.innerText.includes("Ask Doubt") || btn.innerText.includes("Doubt"));
if (!askDoubtButton) return;
const bookmarkButton = document.createElement('img');
bookmarkButton.id = "add-bookmark-button";
bookmarkButton.src = bookmarkImgURL;
bookmarkButton.style.height = "30px";
bookmarkButton.style.width = "30px";
bookmarkButton.style.cursor = "pointer";
bookmarkButton.style.marginLeft = "10px"; // Add spacing so it looks nice
askDoubtButton.parentNode.insertAdjacentElement("afterend", bookmarkButton);
bookmarkButton.addEventListener("click", addNewBookmarkHandler);
}
async function addNewBookmarkHandler() {
const currentBookmarks = await getCurrentBookmarks();
const azProblemUrl = window.location.href;
const uniqueId = extractUniqueId(azProblemUrl);
// ROBUST FIX: Get problem title from H1 tag instead of random class name
let problemName = "Unknown Problem";
const titleElement = document.querySelector('h1');
if (titleElement) {
problemName = titleElement.innerText;
}
if(currentBookmarks.some((bookmark) => bookmark.id === uniqueId)) {
alert("Problem already bookmarked!");
return;
}
const bookmarkObj = {
id: uniqueId,
name: problemName,
url: azProblemUrl
}
const updatedBookmarks = [...currentBookmarks, bookmarkObj];
chrome.storage.sync.set({[AZ_PROBLEM_KEY]: updatedBookmarks}, () => {
console.log("Updated the bookmarks correctly to ", updatedBookmarks);
alert("Bookmark Saved!");
})
}
function extractUniqueId(url) {
const start = url.indexOf("problems/") + "problems/".length;
const end = url.indexOf("?", start);
return end === -1 ? url.substring(start) : url.substring(start, end);
}
function getCurrentBookmarks() {
return new Promise((resolve, reject) => {
chrome.storage.sync.get([AZ_PROBLEM_KEY], (results) => {
resolve(results[AZ_PROBLEM_KEY] || []);
});
});
}