-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
82 lines (71 loc) · 2.37 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
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === "scrape") {
scrapeContent()
.then(sendResponse)
.catch((error) => {
console.error("Error during scraping:", error);
sendResponse({ success: false, message: error.message });
});
// Keeps the message channel open for async response
return true;
}
});
async function scrapeContent() {
try {
console.log("Scraping process has started.");
const element =
document.querySelector(
"div.problem-statement-container div.challenge-body-html",
) ||
document.querySelector(
"div.layout-pane.coding-question__question-pane.layout-pane--primary.layout-pane--smooth div.coding-question__left-pane",
);
if (!element) {
throw new Error("Content not found on the page.");
}
const content = element.innerText.trim();
console.log("Raw content scraped:", content);
const languageElement = document.querySelector(
"div.custom-select.select-language div.css-1hwfws3",
);
const language = languageElement ? languageElement.textContent.trim() : "";
const codeElements = document.querySelectorAll(
"div.monaco-scrollable-element.editor-scrollable.vs.mac div.view-line",
);
const code = Array.from(codeElements)
.map((el) => el.textContent)
.join("\n")
.trim();
const formattedContent = await formatContent(content, language, code);
console.log("Formatted content:", formattedContent);
return {
success: true,
message: "Content has been formatted.",
formattedContent: formattedContent,
};
} catch (error) {
console.error("Error during scraping:", error);
throw error;
}
}
async function formatContent(content, language, code) {
try {
const response = await fetch("http://localhost:8005/prometheusHackerRank", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ content, language, code }),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data.formattedContent;
} catch (error) {
console.error("Error in formatContent:", error);
throw new Error(
"Error formatting content. Please check the console for details.",
);
}
}