-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopy_Wayground_question.js
More file actions
95 lines (84 loc) · 3.41 KB
/
Copy_Wayground_question.js
File metadata and controls
95 lines (84 loc) · 3.41 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// ==UserScript==
// @name Copy Wayground question
// @namespace https://github.com/vuquan2005/ScriptsMonkey
// @version 1.0
// @description Copy formatted text from #questionText when clicking on specific pill element
// @author QuanVu
// @match https://wayground.com/join/game/*
// @grant none
// ==/UserScript==
(function () {
"use strict";
function waitForSelector(selector, timeout = 10000, delay = 100, scope = document) {
return new Promise((resolve, reject) => {
const element = scope.querySelector(selector);
if (element) {
return setTimeout(() => resolve(element), delay);
}
let timeoutId;
if (timeout > 0) {
timeoutId = setTimeout(() => {
observer.disconnect();
reject(
new Error(`⏱️ Timeout: Không tìm thấy "${selector}" trong ${timeout}ms.`)
);
}, timeout);
}
const observer = new MutationObserver(() => {
const element = scope.querySelector(selector);
if (element) {
clearTimeout(timeoutId);
observer.disconnect();
setTimeout(() => resolve(element), delay);
}
});
observer.observe(scope.documentElement, {
childList: true,
subtree: true,
});
});
}
async function run() {
await waitForSelector(
"div.pill.rounded-full.absolute.align-middle.flex.flex-row",
10000,
1000
);
const targetPill = document.querySelector(
"div.pill.rounded-full.absolute.align-middle.flex.flex-row"
);
await waitForSelector('span[data-cy="current-question-number"]', 10000, 500);
const questionNum = document.querySelector('span[data-cy="current-question-number"]');
console.log("Running...\n", questionNum.textContent.trim(), " : ", numOfQues);
if (numOfQues != questionNum.textContent.trim()) numOfQues = questionNum.textContent.trim();
else return;
console.log("Ques: ", numOfQues);
targetPill.addEventListener("click", function () {
const questionText = document.getElementById("questionText");
let textToCopy = questionText.innerHTML
.replace(/<br\s*\/?>/gi, "\n")
// .replace(/<(b|strong)>(.*?)<\/(b|strong)>/gi, "**$2**")
// .replace(/<i>(.*?)<\/i>/gi, "*$1*")
.replace(/<u>(.*?)<\/u>/gi, "_$1_")
.replace(/<\/?[^>]+(>|$)/g, "")
.trim();
navigator.clipboard.writeText(textToCopy).catch((err) => {
console.error("Lỗi khi sao chép:", err);
alert("Lỗi sao chép: " + (err.message || "Không xác định"));
});
});
}
var numOfQues = "";
waitForSelector("div.pill.rounded-full.absolute.align-middle.flex.flex-row", 20000).then(
(el) => {
run();
setInterval(() => { run(); }, 5000);
const observe = new MutationObserver(run);
observe.observe(document.querySelector(".quiz-container-inner"), {
childList: true,
characterData: true,
subtree: true,
});
}
);
})();