How to trigger an ‘isTrusted=true’ click event using JavaScript?

前端 未结 2 503
名媛妹妹
名媛妹妹 2020-12-02 23:00

I\'m trying to impersonate user clicks and mouse movements using a Chrome extension.

For example:
In my content script there is a button click.



        
相关标签:
2条回答
  • 2020-12-02 23:38

    I'm not sure if this is possible, since it's a read-only property that signifies exactly what you're trying to fake, namely if the event originated from the end user or from a script. There used to be browser-based differences, (IE used to have all events as trusted) but I don't know if this is still the case.

    https://developer.mozilla.org/en-US/docs/Web/API/Event

    There may still be ways around this, as mentioned for firefox in this topic:Are events generated by Firefox extension 'trusted'?

    But you'll have to have a look at the chrome documentation to check if they have similar methods of delegating an event back to the window, since it does mention extension events are/can become trusted in some cases.

    0 讨论(0)
  • 2020-12-02 23:45

    You can inject trusted events using the debugger interface.

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

    https://developer.chrome.com/extensions/debugger

    https://chromedevtools.github.io/devtools-protocol/1-2/Input

    0 讨论(0)
提交回复
热议问题