![](/img/trans.png)
[英]chrome.debugger.sendCommand() Input.dispatchMouseEvent error on MV3
[英]Why is Input.dispatchMouseEvent not dispatching an event?
我正在嘗試調度一個 IsTrusted 事件,該事件模擬用戶單擊屏幕上的某個位置。 我正在通過 Chrome 擴展程序嘗試此操作,盡管我運氣不佳。 控制台中沒有錯誤,我的巨大屏幕按鈕沒有被點擊。 這是我的background.js
:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
var activeTab = tabs[0];
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if( request.message === "clickElement" ) {
chrome.debugger.attach({tabId:tab.id}, "1.2", function(debugg) {
chrome.debugger.sendCommand(
{tabId:tab.id}, "Debugger.enable", {},
function() {
chrome.debugger.sendCommand({tabId:tab.id}, "Input.dispatchMouseEvent",
{
type:"mousePressed",
x:parseFloat(request.x),
y:parseFloat(request.y)
})
})
})
}
}
);
chrome.tabs.sendMessage(activeTab.id, {"message": "runbot"});
});
});
而我的content.js
只是發送帶有按鈕坐標的 clickElement 消息。
有任何想法嗎?
您需要做的是刪除這些廢話:
chrome.debugger.sendCommand(
{tabId:tab.id}, "Debugger.enable", {},
並將button: "left"
屬性添加到參數對象:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
var activeTab = tabs[0];
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if( request.message === "clickElement" ) {
chrome.debugger.attach({tabId:tab.id}, "1.2", function(debugg) {
chrome.debugger.sendCommand({tabId:tab.id}, "Input.dispatchMouseEvent",
{
type:"mousePressed",
button: "left",
x:parseFloat(request.x),
y:parseFloat(request.y)
})
})
}
}
);
chrome.tabs.sendMessage(activeTab.id, {"message": "runbot"});
});
});
還要確保您在manifest.json
擁有"debugger"
權限。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.