-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
99 lines (88 loc) · 2.97 KB
/
background.js
File metadata and controls
99 lines (88 loc) · 2.97 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
96
97
98
99
chrome.runtime.onInstalled.addListener(() => {
console.log("Gemini Helper installed. Ready to go.");
});
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "askGemini") {
fetch("http://localhost:3000/ask", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ text: message.text })
})
.then(res => res.json())
.then(data => {
const answer = data.answer || " No answer available.";
if (sender.tab && sender.tab.id !== undefined) {
chrome.tabs.sendMessage(sender.tab.id, {
action: "showAnswerBox",
answer: answer
});
} else if (sender.frameId !== undefined && sender.documentId) {
chrome.webNavigation.getAllFrames({ tabId: sender.tab.id }, (frames) => {
const targetFrame = frames.find(f => f.frameId === sender.frameId);
if (targetFrame) {
chrome.tabs.sendMessage(sender.tab.id, {
action: "showAnswerBox",
answer: answer
}, { frameId: sender.frameId });
}
});
} else {
console.warn("Failed to send answer to content script because tab or frame not found.");
}
})
.catch(err => {
if (sender.tab && sender.tab.id !== undefined) {
chrome.tabs.sendMessage(sender.tab.id, {
action: "showAnswerBox",
answer: "Error: " + err.message
});
}
});
return true;
}
});
// Inject bypass copy and right-click protections
document.addEventListener("DOMContentLoaded", () => {
const unlockCopy = () => {
const tags = document.querySelectorAll("*");
tags.forEach(el => {
el.style.userSelect = "text";
el.style.webkitUserSelect = "text";
el.style.msUserSelect = "text";
el.style.MozUserSelect = "text";
el.style.pointerEvents = "auto";
});
};
const bypassEvents = (e) => {
e.stopPropagation();
e.stopImmediatePropagation();
};
const events = [
"contextmenu", "copy", "cut", "paste",
"mousedown", "mouseup", "keydown", "keyup",
"selectstart", "dragstart"
];
events.forEach(evt => {
document.addEventListener(evt, bypassEvents, true);
window.addEventListener(evt, bypassEvents, true);
});
const cleanHandlers = () => {
document.querySelectorAll("*").forEach(el => {
[
"oncopy", "oncut", "onpaste", "onselectstart",
"oncontextmenu", "onmousedown", "onmouseup", "ondragstart"
].forEach(attr => {
if (el.hasAttribute(attr)) el.setAttribute(attr, "");
});
});
};
const observer = new MutationObserver(() => {
unlockCopy();
cleanHandlers();
});
observer.observe(document, { childList: true, subtree: true });
setTimeout(() => {
unlockCopy();
cleanHandlers();
}, 300);
});