您不需要添加公共API的權限。如果網站不允許跨源請求,則只需添加權限即可。
此外,通過內容腳本使用Web偵聽器爲您的鍵盤快捷方式添加偵聽器不是一個好的解決方案,因爲它需要權限警告,並且通常效率不高。相反,您應該使用Chrome的commands API與activetab。您的擴展程序僅在用戶點擊鍵盤快捷方式時啓動,用戶可以通過chrome:// extensions底部的快捷方式鏈接自定義快捷方式。
爲此,請將"permissions": [ "activeTab" ]
添加到您的清單。當用戶點擊這些按鍵
"commands": { "showcontentdialog": { "suggested_key": { "default": "Ctrl+Shift+Y" }, "description": "show content dialog" } }
接下來,設置你的背景頁面監聽器和inject您的內容腳本:添加您的組合鍵
chrome.commands.onCommand.addListener(function(command) {
if(command.name == "showcontentdialog") {
chrome.tabs.executeScript({ file: "main.js" })
}
})
此外,你應該使用,而不是設置innerHTML的appendChild,像這樣:
var dialog = document.createElement("dialog")
dialog.textContent = "This is a dialog"
var button = document.createElement("button")
button.textContent = "Close"
dialog.appendChild(button)
button.addEventListener("click", function() {
dialog.close()
})
document.body.appendChild(dialog)
dialog.showModal()
您的擴展程序是否需要訪問站點,不包括注入對話框? –
我不需要訪問用戶當前所在的站點,我只想使用'getSelection()。toString()'獲取當前突出顯示的文本。但是,我確實需要向MediaWiki API發出跨域請求。據我瞭解,我可以在Chrome擴展清單中聲明一個許可,以允許跨站點域請求,儘管通常不允許使用JS。是對的嗎? –