我在这里寻求一些帮助,因为我看到的示例只是从选项卡到扩展,而不是相反。
我正在寻找使用自定义 Chrome 扩展程序调试的页面/选项卡的源代码。我希望扩展程序调用消息并将响应发送回扩展面板 javascript 进行调用。
显现
"permissions": [
"tabs",
"<all_urls>",
"debugger"
],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"]
}
],
背景.js
chrome.browserAction.onClicked.addListener(function() {
chrome.tabs.query({active:true, windowId:chrome.windows.WINDOW_ID_CURRENT}, function(tabs) {
debuggee = {tabId:tabs[0].id};
chrome.debugger.attach(debuggee, version, onAttach.bind(null, tabs[0].id));
});
});
function onAttach(tabId) {
chrome.windows.create({url: "spy.html?" + tabId, type: "panel", width: 900, height: 700}, function(window) {
winId = window.id;
});
内容.js
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
if (request.data == "getHTML") {
sendResponse({data: document.getElementById('header').innerHTML});
}
});
间谍.html
<script src="spy.js" type="text/javascript"></script>
间谍.js
window.addEventListener("load", function() {
chrome.debugger.sendCommand({tabId:tabId}, "DOM.getDocument");
chrome.debugger.onEvent.addListener(onEvent);
});
function onEvent(debuggeeId, message, params) {
if (message=="DOM.documentUpdated") {
chrome.tabs.sendMessage(tabId, {data: "getHTML"}, function(response) {console.log(response.data);});
}
结果
端口错误:无法建立连接。接收端不存在。miscellaneous_bindings:235 chromeHidden.Port.dispatchOnDisconnect miscellaneous_bindings:235
“未定义”的事件处理程序错误:无法读取未定义的属性“数据”类型错误:无法
在 chrome-extension://fpdkndicjblnkakkiiapbbdflkehjmgm/headers.js:132:91
在 miscellaneous_bindings:279:11
处读取未定义的属性“数据” chrome.Event.dispatchToListener (event_bindings:387:21)
在 chrome.Event.dispatch_ (event_bindings:373:27)
在 chrome.Event.dispatch (event_bindings:393:17)
在 Object.chromeHidden.Port.dispatchOnDisconnect (miscellaneous_bindings:238 :27)
当我尝试运行它时出现此错误。我错过了什么?