最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin9浏览0评论

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')
发布评论

评论列表(0)

  1. 暂无评论