-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontent.js
68 lines (59 loc) · 2.09 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
console.log('👨💻 Author: Saurav Hathi \n🌟 GitHub: https://github.com/sauravhathi \n🚀Linkedin: https://www.linkedin.com/in/sauravhathi');
// code div.ace_content ace_layer ace_text-layer
async function copyTextToClipboard(text) {
if (!text) {
return Promise.reject("Text not found");
}
return navigator.clipboard.writeText(text)
.then(() => 'Copied to clipboard!')
.catch((error) => {
throw new Error(`Error copying to clipboard: ${error}`);
});
}
/**
* Watches for a specific element and copies its text to clipboard on double click.
* @function
* @returns {void}
*/
function watchForElement() {
const targetSelector = 'div[aria-labelledby="each-type-question"]';
/**
* Handles the double click event on the target element and copies its text to clipboard.
* @function
* @param {Event} event - The double click event.
* @returns {void}
*/
const handleDoubleClick = (event) => {
const targetElement = event.target.closest(targetSelector);
if (targetElement) {
const cleanedText = targetElement.innerText.replace(/\n{3,}/g, '\n');
copyTextToClipboard(cleanedText)
.then((message) => {
console.log(message);
})
.catch((error) => {
alert(error.message);
});
}
};
document.addEventListener('dblclick', handleDoubleClick);
const observer = new MutationObserver((mutationsList, observer) => {
if (document.querySelector(targetSelector)) {
observer.disconnect();
}
});
observer.observe(document, { childList: true, subtree: true });
}
document.addEventListener("keydown", (e) => {
if ((e.ctrlKey && e.key === "b") || (e.altKey && e.key === "b")) {
navigator.clipboard.readText().then((text) => {
if (document.activeElement instanceof HTMLInputElement || document.activeElement instanceof HTMLTextAreaElement) {
document.activeElement.value = text;
document.activeElement.dispatchEvent(new Event('input', { bubbles: true }));
}
}).catch((err) => {
console.error('Failed to paste: ', err);
});
}
});
watchForElement();