我在Stack Overflow上发现了几个相当不错的投票问题,尽管我实际上找不到工作解决方案。努力在Chrome扩展中获取DOM数据
我一直在试图按照这里的答案:Chrome Extension - Get DOM content, 但我仍然得到一个完全空白的控制台(尽管重新加载扩展名)!
manifest.json的:
{
"manifest_version": 2,
"name": "Test Extension",
"version": "0.0",
"background": {
"persistent": false,
"scripts": ["background.js"]
},
"content_scripts": [{
"matches": ["https://*/*"],
"js": ["content.js"]
}],
"browser_action": {
"default_title": "Test Extension",
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": ["activeTab"]
}
background.js:
// A function to use as callback
function logDOM(domContent) {
console.log('I received the following DOM content:\n' + domContent);
}
// When the browser-action button is clicked...
chrome.browserAction.onClicked.addListener(function (tab) {
console.log(tab.url);
chrome.tabs.sendMessage(tab.id, {text: 'report_back'}, logDOM);
});
content.js:
// Listen for messages
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
// If the received message has the expected format...
if (msg.text === 'report_back') {
// Call the specified callback, passing
// the web-page's DOM content as argument
sendResponse(document.all[0].outerHTML);
}
});
我做索姆错了吗?
您从镀铬背景页控制台://扩展? –