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