我寫了一個簡單的擴展來了解它是如何作業的。
清單 V3:
{ "name": "Sample",
"version": "1.0",
"manifest_version": 3,
"action": {
"default_name": "Sample",
"default_popup": "popup.html"
},
"background": {
"service_worker": "background.js"
},
"permissions": ["tabs", "storage", "activeTab", "clipboardRead", "scripting"],
"host_permissions": ["http://*/", "file://*/*", "https://*/", "*://*/*"]
}
popup.html:
.....
.....
<button id='getInfo' >GET</button>
.....
.....
popup.js:
....
document.getElementById("getInfo").addEventListener("click", function() {
chrome.tabs.query(
{active: true, currentWindow: true},
(tabs) => {
chrome.scripting.executeScript({
target: {tabId: tabs[0].id},
file: ['func.js'],
});
});
}
函式.js:
alert("extension sample")
當我按下“GET”按鈕時,我在擴展管理器頁面中收到錯誤訊息。錯誤說:“錯誤處理回應:TypeError:呼叫 scripting.executeScript(scripting.ScriptInjection 注入,可選函式回呼)時出錯:引數'injection'出錯:意外屬性:'file'。”
我嘗試過使用函式而不是檔案,它可以作業。
我該如何解決這個錯誤?謝謝你。
uj5u.com熱心網友回復:
chrome.scripting.executeScript沒有file財產。你應該使用files財產。
在下面的 Google 官方檔案中搜索“ executeScript”以查看示例。
https://developer.chrome.com/docs/extensions/reference/scripting/
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/517896.html
標籤:谷歌浏览器谷歌浏览器扩展
