How to set a created event's `isTrusted` property in javascript using Greasemonkey

Viewed 3328

I would like to create and fire a KeyPress Event through a userscript in Greasemonkey but I observed that the createEvent's isTrusted is set to false all the time.

isTrusted is read-only property but I read that it can be set to True by extensions like Greasemonkey but it's not working for me.

Is there a way to set isTrusted to true?.

2 Answers

Theoretically, You could create a trusted event with a WebSocket that javascript uses to communicate to a python script running in the background with would press the key, but that's a lot of code.

Python Code:

import asyncio
import websockets
import pyautogui
async def watchlist(websocket, path):
    while (1==1):
        key = await websocket.recv()
        pyautogui.press(key)

start_server = websockets.serve(watchlist, "localhost", 8765)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()



Js Code to press key:

ws = new WebSocket("ws://localhost:8765")
function sendkey(key){
     ws.send(key)
}
sendkey('a')
Related