如何使用 Greasemonkey 在 javascript 中设置创建的事件的 `isTrusted` 属性

问题描述 投票:0回答:2

我想通过 Greasemonkey 中的用户脚本创建并触发按键事件,但我观察到 createEvent 的

isTrusted
始终设置为
false

isTrusted
是只读属性,但我读到可以通过 Greasemonkey 等扩展将其设置为 True,但它对我不起作用。

有没有办法将

isTrusted
设置为
true
?.

javascript greasemonkey dom-events
2个回答
2
投票

不。目前您无法使用 Greasemonkey 执行此操作。大多数情况下,Greasemonkey 只运行 javascript,而 javascript 无法设置

isTrusted
。它将违反规范并破坏可信事件的全部目的。

也就是说,理论上,Firefox 插件应该能够欺骗

isTrusted
,但 Greasemonkey 尚未将该功能扩展到其用户。

向 Greasemonkey 添加

isTrusted
功能的功能请求。您可以去那里并在讨论中添加您的声音,或者您可以派生 Greasemonkey 代码并自己添加该功能。




Google Chrome 存在相关问题,但在该浏览器中可能无法进行欺骗

isTrusted
,即使对于扩展程序也是如此。


1
投票

理论上,您可以使用 WebSocket 创建一个可信事件,JavaScript 使用该事件与后台运行的 Python 脚本进行通信,该脚本将按下按键,但这需要大量代码。

Python代码:

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代码:

ws = new WebSocket("ws://localhost:8765")
function sendkey(key){
     ws.send(key)
}
sendkey('a')
© www.soinside.com 2019 - 2024. All rights reserved.