我正在开发chrome扩展程序,该扩展程序需要将消息发送到Content Script,并且一切正常,直到我用单独的窗口替换默认弹出窗口为止。之后,关于消息的通信将无法进行。
我的清单:
pygame.draw.rect(background, green, [20, 20, 50, 20], 0)
侦听器(内容脚本):
{
"manifest_version": 2,
"name": "Overlight Live Reload",
"description": "Overlight Testing Tool",
"version": "0.0.1",
"content_security_policy": "script-src 'self' http://localhost:3000/; object-src 'self'",
"permissions": ["activeTab", "tabs", "storage", "declarativeContent", "<all_urls>"],
"browser_action": {
"default_title": "Overlight"
},
"background": {
"scripts": ["background/background.js"],
"persistent": false
},
"icons": {
"16": "images/get_started16.png",
"32": "images/get_started32.png",
"48": "images/get_started48.png",
"128": "images/get_started128.png"
},
"content_scripts": [
{
"matches": ["http://*/*","https://*/*"],
"css": [],
"js": ["contentScripts/lodash.js", "contentScripts/listeners.js"]
}
]
}
从扩展应用执行:
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.greeting == "hello")
sendResponse({farewell: "goodbye"});
});
创建扩展窗口的背景幕提示:
export const testSendToPage = () => {
console.log('Sending message to web page')
window.chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
window.chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
console.log(response.farewell);
});
});
}
一切正常,当创建单独的窗口时,我使用的是默认弹出窗口。当我尝试从分离的窗口异常中使用window.chrome.tabs.executeScript时,我已经知道了。
运行tabs.executeScript时未经检查的runtime.lastError:无法访问URL“ chrome-extension://ciipdholchghlckepfaecipbjleanmbg/popup.html”的内容。扩展清单必须请求访问此主机的权限。
从内容脚本到退出应用程序的消息效果很好。如何从扩展程序分隔窗口中的扩展程序应用程序与内容脚本进行通信?