我在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);
}
});
我做索姆錯了嗎?
您從鍍鉻背景頁控制檯://擴展? –