1

I'd like to simulate a trusted click event for a chrome extension using the debugger api seen here.

However, chrome.debugger is undefined.

chrome.debugger.attach(target, "1.2", function() {
    chrome.debugger.sendCommand(target, "Input.dispatchMouseEvent", arguments)
})

manifest.json

"permissions": [
    "debugger", "storage"
 ]

Am I missing something? How can I effectively call chrome.debugger? When I look at the permissions in chrome://extensions it shows that I have "Access the page debugger backend"

user2954587
  • 4,661
  • 6
  • 43
  • 101
  • 1
    Without [MCVE](/help/mcve) my guess would be you're doing it in a content script, which have access only to a few basic API, or you've set a breakpoint in devtools (or used a `debugger` statement) prior to the first access to any `chrome` API (this is a known bug in Chrome). – wOxxOm Dec 08 '18 at 21:16
  • where can you use it if not in a content script? background? @wOxxOm – user2954587 Dec 09 '18 at 05:31
  • In any extension page, including the background page, of course. – wOxxOm Dec 09 '18 at 05:42
  • @wOxxOm got it, thanks. can i call bind onClick events to html elements on specific pages from a background script/? – user2954587 Dec 09 '18 at 05:50
  • 1
    No, declaring a background script creates a separate hidden background page where that script(s) run. To access the web page DOM from browser tabs you need a content script. See also [How to access the webpage DOM rather than the extension page DOM?](//stackoverflow.com/a/4532567) – wOxxOm Dec 09 '18 at 05:52
  • thanks. one last questions since you seem knowledgeable. ideas on how to make a trusted event for a browser extension? (https://stackoverflow.com/questions/34853588/how-to-trigger-an-istrusted-true-click-event-using-javascript) – user2954587 Dec 09 '18 at 05:54
  • The thread you've linked contains an answer. Be more specific or ask a new question. – wOxxOm Dec 09 '18 at 05:55

1 Answers1

0

You need to attach the debugger interface to the tab via a background script:

let tabId = tab.id;
let debuggeeId = { tabId };
chrome.debugger.attach(debuggeeId, version, onAttach.bind(null, debuggeeId));

And then

const onDebuggerEnabled = (debuggeeId) => {
  debuggerEnabled = true
}

const onAttach = (debuggeeId) => {
chrome.debugger.sendCommand(
    debuggeeId, "Debugger.enable", {},
    onDebuggerEnabled.bind(null, debuggeeId));
}

Then you can use message passing to send requests to the background script from the content script: https://developer.chrome.com/extensions/messaging

And

if (debuggerEnabled) {
chrome.debugger.sendCommand(debuggeeId, "Input.dispatchMouseEvent", { type: "mousePressed", x: xC, y: yC, button: "left" }, function (e) { console.log('mousedown', e) });

chrome.debugger.sendCommand(debuggeeId, "Input.dispatchMouseEvent", { type: "mouseReleased", x: xC, y: yC, button: "left" }, function (e) { console.log('mouseup', e) });
} 
// xC, yC are your coordinates

This is a working example: https://github.com/Sentero-esp12/IsTrusted-event-Debugger-API

teg_brightly
  • 468
  • 6
  • 20