-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserscript.js
141 lines (113 loc) · 4.75 KB
/
userscript.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// ==UserScript==
// @name Copy NeetCode Question in Markdown
// @namespace TheSR
// @version 1.0
// @description Adds a copy button to copy question from neetcode.io in Markdown format
// @author TheSR
// @match *://neetcode.io/problems/*
// @grant none
// @run-at document-idle
// ==/UserScript==
(function () {
"use strict";
// Function to convert HTML to Markdown
function htmlToMarkdown(html) {
let markdown = html;
markdown = markdown.replace(/<h1[^>]*>(.*?)<\/h1>/g, "# $1\n");
markdown = markdown.replace(/<h2[^>]*>(.*?)<\/h2>/g, "## $1\n");
markdown = markdown.replace(/<h3[^>]*>(.*?)<\/h3>/g, "### $1\n");
markdown = markdown.replace(/<strong[^>]*>(.*?)<\/strong>/g, "**$1**");
markdown = markdown.replace(/<code[^>]*>(.*?)<\/code>/g, "`$1`");
markdown = markdown.replace(
/<pre[^>]*><code[^>]*>([\s\S]*?)<\/code><\/pre>/g,
"```java\n$1```"
);
markdown = markdown.replace(/<p[^>]*>(.*?)<\/p>/g, "$1\n");
markdown = markdown.replace(/<ul[^>]*>(.*?)<\/ul>/gs, (_, content) => {
return content.replace(/<li[^>]*>(.*?)<\/li>/g, "- $1\n");
});
markdown = markdown.replace(/<br\s*\/?>/g, "\n");
markdown = markdown.replace(
/<details[^>]*>\s*<summary[^>]*>([\s\S]*?)<\/summary>\s*<p[^>]*>([\s\S]*?)<\/p>\s*<\/details>/g,
(_, summary, content) => {
content = content.trim();
return `<details>\n<summary>${summary}</summary>\n\n${content}\n\n</details>`;
}
);
markdown = markdown.replace(
/<(?!\/?(details|summary|strong)\b)[^>]+>/g,
""
);
return markdown.trim();
}
// Function to copy formatted content in Markdown
function copyToClipboard(button) {
const contentElement = document.querySelector(
".my-article-component-container"
);
const difficultyElement = document.querySelector(".difficulty-btn");
const difficulty = difficultyElement
? difficultyElement.innerText.trim()
: "Easy";
const difficultyColor =
difficulty === "Easy"
? "green"
: difficulty === "Medium"
? "orange"
: "red";
const contentClone = contentElement.cloneNode(true);
const buttonsToExclude = contentClone.querySelectorAll(
".copy-to-clipboard-button"
);
buttonsToExclude.forEach(button => button.remove());
const markdownContent = htmlToMarkdown(contentClone.innerHTML);
const title = document.querySelector("h1").innerText.trim();
const formattedContent = `# ${title}\n\n**Difficulty:** <span style="color: ${difficultyColor};">${difficulty}</span>\n\n${markdownContent}`;
navigator.clipboard
.writeText(formattedContent)
.then(() => {
button.innerText = "Copied!";
button.style.backgroundColor = "#4caf50"
setTimeout(() => {
button.innerText = "Copy Content";
button.style.backgroundColor = "";
}, 1000);
})
.catch(err => {
console.error("Failed to copy: ", err);
});
}
// Function to add the copy button
function addButton(targetElement) {
if (targetElement.querySelector(".copy-to-clipboard-button")) return;
const button = document.createElement("button");
button.classList.add("copy-to-clipboard-button");
button.innerText = "Copy Content";
button.style.marginLeft = "auto";
button.style.display = "inline-block";
targetElement.style.display = "flex";
targetElement.style.alignItems = "center";
targetElement.appendChild(button);
button.addEventListener("click", () => copyToClipboard(button));
}
// Function to observe changes in the DOM and reapply the button
function observePage() {
const targetNode = document.body;
const config = { childList: true, subtree: true };
const callback = function (mutationsList, observer) {
for (let mutation of mutationsList) {
if (mutation.type === "childList") {
const targetElement = document.querySelector(
".question-tab > div:nth-child(1) > div:nth-child(1)"
);
if (targetElement) {
addButton(targetElement);
}
}
}
};
const observer = new MutationObserver(callback);
observer.observe(targetNode, config);
}
observePage();
})();