Google chrome extension 如何从调试选项卡获取DOM到扩展脚本
我在这里寻求一些帮助,因为我所看到的示例只是从选项卡到扩展,而不是相反 我正在寻找一个页面/标签的源代码,我正在用一个定制的Chrome扩展调试它。我希望扩展调用消息,并将响应发送回进行调用的扩展面板javascript 清单Google chrome extension 如何从调试选项卡获取DOM到扩展脚本,google-chrome-extension,Google Chrome Extension,我在这里寻求一些帮助,因为我所看到的示例只是从选项卡到扩展,而不是相反 我正在寻找一个页面/标签的源代码,我正在用一个定制的Chrome扩展调试它。我希望扩展调用消息,并将响应发送回进行调用的扩展面板javascript 清单 "permissions": [ "tabs", "<all_urls>", "debugger" ], "background": { "scripts": ["background.js"], "persistent": false }
"permissions": [
"tabs",
"<all_urls>",
"debugger"
],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"]
}
],
content.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;
});
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
if (request.data == "getHTML") {
sendResponse({data: document.getElementById('header').innerHTML});
}
});
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);});
}
chrome.browserAction.onClicked.addListener(function () {
version = "1.0";
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;
});
}
chrome.extension.onMessage.addListener(function (request, sender) {
console.log("Message recieved");
if (request.data == "getHTML") {
chrome.extension.sendMessage({
"data": "Some Stuff"
});
}
});
tabId = parseInt(window.location.search.substring(1));
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"
});
}
}
chrome.extension.onMessage.addListener(function (response, sender) {
console.log(response);
});
spy.html
<script src="spy.js" type="text/javascript"></script>
结果
"permissions": [
"tabs",
"<all_urls>",
"debugger"
],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"]
}
],
端口错误:无法建立连接。接收端不存在。杂项_绑定:235
chromeHidden.Port.dispatchOnDisconnect杂项_绑定:235
“未定义”的事件处理程序中出错:无法读取未定义类型的属性“数据”错误:无法读取未定义类型的属性“数据”在铬-extension://fpdkndicjblnkakkiiapbbdflkehjmgm/headers.js:132:91
在其他绑定中:279:11
在chrome.Event.dispatchToListener(事件绑定:387:21)
在chrome.Event.dispatch(事件绑定:373:27)
在chrome.Event.dispatch(事件绑定:393:17)
在Object.chromeHidden.Port.dispatchOnDisconnect(杂项绑定:238:27)
当我尝试运行它时,会出现此错误。我缺少什么?您如何捕获
chrome.tabs.sendMessage(tabId,
)的tabId
,如果您正在寻找将消息从chrome Extension
传递到调试选项卡的示例代码,请检查此项
工具书类
manifest.json
已注册弹出窗口
页面和内容脚本
{
"name": "Pass message from Chrome Extension to Debugging Tab",
"version": "1",
"description": "http://stackoverflow.com/questions/14205155/how-can-i-pass-a-message-from-my-chrome-extension-to-debugging-tab",
"browser_action": {
"default_title": "Selected Text",
"default_popup": "popup.html"
},
"permissions": [
"tabs",
"<all_urls>"
],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["selection.js"]
}
],
"manifest_version": 2
}
selection.js
添加了一个处理程序来捕获从弹出页面发送的消息
//Add a handler to handle message sent from popup.html
chrome.extension.onMessage.addListener(function(request, sender) {
console.log("Message "+request+" is recieved");
});
编辑:
在消除了一些不推荐使用的API()之后,我使您的代码正常工作,如sendResponse
background.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;
});
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
if (request.data == "getHTML") {
sendResponse({data: document.getElementById('header').innerHTML});
}
});
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);});
}
chrome.browserAction.onClicked.addListener(function () {
version = "1.0";
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;
});
}
chrome.extension.onMessage.addListener(function (request, sender) {
console.log("Message recieved");
if (request.data == "getHTML") {
chrome.extension.sendMessage({
"data": "Some Stuff"
});
}
});
tabId = parseInt(window.location.search.substring(1));
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"
});
}
}
chrome.extension.onMessage.addListener(function (response, sender) {
console.log(response);
});
content.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;
});
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
if (request.data == "getHTML") {
sendResponse({data: document.getElementById('header').innerHTML});
}
});
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);});
}
chrome.browserAction.onClicked.addListener(function () {
version = "1.0";
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;
});
}
chrome.extension.onMessage.addListener(function (request, sender) {
console.log("Message recieved");
if (request.data == "getHTML") {
chrome.extension.sendMessage({
"data": "Some Stuff"
});
}
});
tabId = parseInt(window.location.search.substring(1));
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"
});
}
}
chrome.extension.onMessage.addListener(function (response, sender) {
console.log(response);
});
spy.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;
});
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
if (request.data == "getHTML") {
sendResponse({data: document.getElementById('header').innerHTML});
}
});
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);});
}
chrome.browserAction.onClicked.addListener(function () {
version = "1.0";
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;
});
}
chrome.extension.onMessage.addListener(function (request, sender) {
console.log("Message recieved");
if (request.data == "getHTML") {
chrome.extension.sendMessage({
"data": "Some Stuff"
});
}
});
tabId = parseInt(window.location.search.substring(1));
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"
});
}
}
chrome.extension.onMessage.addListener(function (response, sender) {
console.log(response);
});
如何确保您在测试期间不手动触发开发人员工具。好的,我已经解决了。
因为我需要加载页面的DOM,所以我将使用后台页面中的chrome.tabs.onUpdate.addListener在加载页面时发送代码。这样我就不必依赖于选项卡和扩展之间的双向通信
清单
"permissions": [
"tabs",
"<all_urls>",
"debugger"
],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"]
}
],
删除content.js
background.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;
});
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
if (request.data == "getHTML") {
sendResponse({data: document.getElementById('header').innerHTML});
}
});
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);});
}
chrome.browserAction.onClicked.addListener(function () {
version = "1.0";
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;
});
}
chrome.extension.onMessage.addListener(function (request, sender) {
console.log("Message recieved");
if (request.data == "getHTML") {
chrome.extension.sendMessage({
"data": "Some Stuff"
});
}
});
tabId = parseInt(window.location.search.substring(1));
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"
});
}
}
chrome.extension.onMessage.addListener(function (response, sender) {
console.log(response);
});
添加了以下内容
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (tabId != _tabId) {return;}
if (changeInfo.status == "complete") {
chrome.tabs.executeScript(tabId, {code:"var x = document.documentElement.innerHTML;x"}, function (r) {
chrome.extension.sendMessage(null, {"data": r[0]});
});
}
});
chrome.extension.onMessage.addListener(function(request, sender) {
console.log("Request.data: " + request.data);
});
content.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;
});
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
if (request.data == "getHTML") {
sendResponse({data: document.getElementById('header').innerHTML});
}
});
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);});
}
chrome.browserAction.onClicked.addListener(function () {
version = "1.0";
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;
});
}
chrome.extension.onMessage.addListener(function (request, sender) {
console.log("Message recieved");
if (request.data == "getHTML") {
chrome.extension.sendMessage({
"data": "Some Stuff"
});
}
});
tabId = parseInt(window.location.search.substring(1));
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"
});
}
}
chrome.extension.onMessage.addListener(function (response, sender) {
console.log(response);
});
除去
spy.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;
});
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
if (request.data == "getHTML") {
sendResponse({data: document.getElementById('header').innerHTML});
}
});
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);});
}
chrome.browserAction.onClicked.addListener(function () {
version = "1.0";
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;
});
}
chrome.extension.onMessage.addListener(function (request, sender) {
console.log("Message recieved");
if (request.data == "getHTML") {
chrome.extension.sendMessage({
"data": "Some Stuff"
});
}
});
tabId = parseInt(window.location.search.substring(1));
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"
});
}
}
chrome.extension.onMessage.addListener(function (response, sender) {
console.log(response);
});
添加了以下内容
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (tabId != _tabId) {return;}
if (changeInfo.status == "complete") {
chrome.tabs.executeScript(tabId, {code:"var x = document.documentElement.innerHTML;x"}, function (r) {
chrome.extension.sendMessage(null, {"data": r[0]});
});
}
});
chrome.extension.onMessage.addListener(function(request, sender) {
console.log("Request.data: " + request.data);
});
background.js在哪里?我已经更新了最初的帖子。感谢您的关注。@ilya:windows.getCurrent
和选项卡。getSelected
已弃用,而chrome.debugger.attach
只有两个选项卡arguments@Sudarshan那么,我是否应该修改流程以匹配您的答案,即删除“背景”在清单中,将popup.html添加到浏览器操作中?我在background.js中有其他代码,主要是侦听器。1)chrome.windows.onRemoved->分离调试器2)chrome.debugger.onDetach->删除弹出窗口window@Sudarshan顺便说一句,您可以为attach指定回调-TabId由background.js文件设置,该文件正在附加调试器并打开弹出窗口。我将把代码添加到原始帖子中。我试图将清单更改为包含“”,但其余的似乎都是相同的。唯一的另一个区别是,您正在将弹出窗口作为浏览器操作加载,而我正在使用background.js加载弹出窗口。@ilya:您没有加载弹出窗口,您正在创建一个类型为panel的新窗口,创建一个正确的新panel后,您可以如何绑定调试器会话。自从我尝试了你的方法后,我已经更改了术语,我不想为扩展设置默认的弹出窗口,而是希望有一个新的框架来显示结果。我能够实现你更正的内容,但这并没有按预期工作。sendMessage(tabId,{“data”:“getHTML”});未在函数中调用。。。或者,如果真的发生了,就没有人在听了。当页面最终完成加载时,我可以通过控制台手动发送消息,只有这样我才能返回一些内容。你知道为什么这不会发生吗?当DOM完全加载时,是否有一个不同的tOt值来查找DOM。DocumentUpdate?@ilya:你能解释一下什么东西没有按照预期工作吗?我测试了这个,并且能够跟踪调试器事件;你想跟踪什么?更改标题以帮助他人