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

I would like to create and fire a KeyPress Event through a userscript in Greasemonkey but I observed th

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?.

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?.

Share Improve this question edited Jul 4, 2017 at 21:29 Brock Adams 93.7k23 gold badges241 silver badges305 bronze badges asked Jul 4, 2017 at 19:22 RevoRevo 671 silver badge4 bronze badges 1
  • Not a solution, but may be my old workaround with Python and WinApi is related: stackoverflow./a/78567959/10141885 – halt9k Commented Jun 2, 2024 at 23:53
Add a ment  | 

2 Answers 2

Reset to default 4

No. You cannot currently do this with Greasemonkey. For the most part, Greasemonkey just runs javascript, and javascript can't set isTrusted. It would defy the spec and defeat the whole purpose of trusted events.

That said, theoretically, a Firefox add-on should be able to spoof isTrusted, but Greasemonkey has not extended that capability to its users.

There is a feature request to add isTrusted capability to Greasemonkey. You can go there and add your voice to the discussion, or you can fork the Greasemonkey code and add the capability yourself.




There is a related question for Google Chrome, but spoofing isTrusted may not be possible in that browser, even for extensions.

Theoretically, you could create a trusted event with a WebSocket that JavaScript uses to municate to a Python script running in the background which 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_plete(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')

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744138684a4560180.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信