Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ecc521
GitHub Repository: ecc521/gimkit-bot
Path: blob/master/ClickIsTrusted-master/background.js
1634 views
1
'use strict';
2
3
//part 1. activating/deactivating the debugger via the browserAction button.
4
const activeTabs = {};
5
6
function updateIcon(tabId) {
7
const color = activeTabs[tabId] ? 'red' : 'black';
8
chrome.browserAction.setIcon({path: `images/${color}_16.png`});
9
}
10
11
function detachTab(tabId) {
12
chrome.debugger.detach({tabId: tabId}, function () {
13
console.log("detached debugger to tab: " + tabId);
14
delete activeTabs[tabId];
15
updateIcon(tabId);
16
});
17
}
18
19
function attachTab(tabId) {
20
chrome.debugger.attach({tabId: tabId}, "1.3", function () {
21
console.log("attached debugger to tab: " + tabId);
22
activeTabs[tabId] = true;
23
updateIcon(tabId);
24
});
25
}
26
27
chrome.browserAction.onClicked.addListener(function callback(data) {
28
console.log("BrowserAction icon clicked. Attaching/detaching the tab.");
29
const tabId = data.id;
30
activeTabs[tabId] ? detachTab(tabId) : attachTab(tabId);
31
});
32
33
chrome.tabs.onActivated.addListener(function callback(data) {
34
// console.log("A tab activated. Updating icon.");
35
updateIcon(data.tabId);
36
})
37
38
chrome.runtime.onMessage.addListener(function handleMessage(request, sender, sendResponse) {
39
//filtering out inactive tabs
40
var tabId = sender.tab.id;
41
if (!activeTabs[tabId])
42
return;
43
console.log("received request from clientScript on active tab", request);
44
dispatchNativeEvent(request, tabId);
45
});
46
47
//part 2. turning messages into native events for active tabs.
48
function dispatchNativeEvent(event, tabId) {
49
//convert the command into an approved native event here.
50
let cmd;
51
if (event.type.startsWith("mouse"))
52
cmd = "Input.dispatchMouseEvent";
53
else if (event.type.startsWith("touch"))
54
cmd = "Input.dispatchTouchEvent";
55
else if (event.type === "keyDown" || event.type === "keyUp" || event.type === "char" || event.type === "rawKeyDown")
56
cmd = "Input.dispatchKeyEvent";
57
else if (event.type === "beforeinput-is-trusted")
58
cmd = "Input.insertText";
59
else
60
throw new Error("Illegal native event: ", event);
61
chrome.debugger.sendCommand({tabId: tabId}, cmd, event, function () {
62
console.log("sendCommand", cmd, event);
63
});
64
}
65