-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
69 lines (58 loc) · 2.04 KB
/
script.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
const TITLE_SELECTOR = 'a.no-underline.whitespace-normal';
const DIFFICULTY_SELECTOR = '[class*="text-difficulty-"]';
const CODE_SELECTOR = '.view-lines';
chrome.runtime.onMessage.addListener((obj, sender, sendResponse) => {
if (obj.type == 'scrapeLeetCode') sendResponse(getCodeAndUrl());
if (obj.type == 'queryTitle') {
(async () => {
await sleep(250);
const elem = await waitForElm(DIFFICULTY_SELECTOR);
const info = updateTitle();
if (info) {
sendResponse(info);
}
})();
}
return true;
});
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
const difficulties = new Set(['Easy', 'Medium', 'Hard']);
const updateTitle = () => {
const diff = document.querySelector(DIFFICULTY_SELECTOR);
if (diff && difficulties.has(diff.innerText)) {
const title = document.querySelector(TITLE_SELECTOR).innerText;
const difficulty = document
.querySelector(DIFFICULTY_SELECTOR)
.innerText.toLowerCase();
const formattedTitle = title
.toLowerCase()
.replace(/[^0-9a-z\s]/g, '')
.replace(/\s+/g, '-')
.replace(/\d+/, (match) => match.padStart(5, '0'));
return { difficulty, formattedTitle };
}
};
const getCodeAndUrl = () => {
const currentURL = window.location.href;
const code = document.querySelector(CODE_SELECTOR).innerText;
return { url: currentURL, code };
};
const waitForElm = async (selector) => {
return new Promise((resolve) => {
if (document.querySelector(selector)) {
return resolve(document.querySelector(selector));
}
const observer = new MutationObserver((mutations) => {
if (document.querySelector(selector)) {
observer.disconnect();
resolve(document.querySelector(selector));
}
});
observer.observe(document.body, {
childList: true,
subtree: true,
});
});
};