-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
74 lines (59 loc) · 2.6 KB
/
Copy pathcontent.js
File metadata and controls
74 lines (59 loc) · 2.6 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
let currentUrl = window.location.href;
function monitorUrlChange() {
const observer = new MutationObserver(() => {
const newUrl = window.location.href;
if (newUrl !== currentUrl) {
currentUrl = newUrl;
console.log("Detected URL change:", currentUrl);
// Check if the new URL is a problem page
if (/https:\/\/leetcode\.com\/problems\/[\w-]+/.test(newUrl)) {
const problemTitle = document.querySelector('div[data-cy="question-title"]')?.innerText.trim();
// Notify background script of the new problem
chrome.runtime.sendMessage({
type: "leetcodeProblemTitle",
problemTitle: problemTitle || "Unknown Problem",
});
}
}
});
observer.observe(document.body, { childList: true, subtree: true });
}
// Start monitoring URL changes
monitorUrlChange();
function extractProblemTitle() {
// Selector for the newer version of LeetCode
const newTitleElement = document.querySelector(
'a.no-underline.hover\\:text-blue-s.dark\\:hover\\:text-dark-blue-s.truncate.cursor-text.whitespace-normal'
);
// Selector for the older version of LeetCode
const oldTitleElement = document.querySelector('div[data-cy="question-title"]');
let problemTitle = null;
if (newTitleElement) {
// Extract the title for the newer version
const fullText = newTitleElement.innerText.trim();
const titleMatch = fullText.match(/^\d+\.\s.+/); // Matches "3319. Problem Title"
problemTitle = titleMatch ? titleMatch[0] : null;
} else if (oldTitleElement) {
// Extract the title for the older version
const fullText = oldTitleElement.innerText.trim();
const titleMatch = fullText.match(/^\d+\.\s.+/); // Matches "1234. Problem Title"
problemTitle = titleMatch ? titleMatch[0] : null;
}
if (problemTitle) {
console.log("Extracted Problem Title:", problemTitle);
// Send the title to the background script
chrome.runtime.sendMessage({ type: "leetcodeProblemTitle", problemTitle }, () => {
console.log("Problem Title Sent:", problemTitle);
});
} else {
console.error("Problem Title not found or format not matched.");
}
}
// Observe changes to dynamically loaded content
const observer = new MutationObserver(() => {
extractProblemTitle();
});
// Start observing changes in the DOM
observer.observe(document.body, { childList: true, subtree: true });
// Extract problem details on script load
extractProblemTitle();