-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
56 lines (46 loc) · 1.84 KB
/
background.js
File metadata and controls
56 lines (46 loc) · 1.84 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
let questions = [];
let intervalId = null;
fetch(chrome.runtime.getURL("question.txt"))
.then(res => res.text())
.then(text => {
questions = text.split("\n").map(l => l.trim()).filter(Boolean);
})
.catch(err => console.error("❌ Gagal membaca question.txt:", err));
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg.action === "start") {
if (!questions.length) return sendResponse({ success: false, error: "question.txt kosong!" });
if (intervalId) clearInterval(intervalId);
intervalId = setInterval(async () => {
const randomQ = questions[Math.floor(Math.random() * questions.length)];
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (tab) {
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: sendChat,
args: [randomQ]
});
}
}, msg.interval * 1000);
sendResponse({ success: true });
}
if (msg.action === "stop") {
if (intervalId) clearInterval(intervalId);
intervalId = null;
sendResponse({ success: true });
}
return true;
});
function sendChat(question) {
const textarea = document.querySelector('textarea[placeholder="Type your question here..."]');
if (!textarea) return console.error("❌ Textarea tidak ditemukan!");
const nativeSetter = Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype, "value").set;
nativeSetter.call(textarea, question);
textarea.dispatchEvent(new Event("input", { bubbles: true }));
setTimeout(() => {
const button = document.querySelector('button[aria-label="submit"]');
if (button) {
button.removeAttribute("disabled");
button.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true, view: window }));
} else console.error("❌ Tombol tidak ditemukan!");
}, 300);
}