如何从chrome扩展中检索DOM数据?

时间:2014-11-04 12:54:46

标签: google-chrome-extension

我有以下代码来开发chrome扩展程序。

的manifest.json

{
  "manifest_version": 2,
  "name": "maskMe",
  "description": "This extension mask your profile section",
  "version": "1.0",
  "icons": {
    "24": "foto/icon_24.png",
  },
  "browser_action": {
    "default_icon": "foto/icon_32.png",
    "default_popup": "popup.html",
  },
  "background": {
    "persistent": false,
    "scripts": ["js/background.js"]
  },
  "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
  "permissions": [
        "tabs",
        "activeTab",
        "http://*/*",
        "https://*/*"
  ],
  "content_scripts": [{
    "matches": ["<all_urls>"],
    "js": ["js/script.js","js/jquery.min.js"]
  }]
}

background.js

debugger;
chrome.browserAction.setBadgeText({text: "maskkkk"});

function doStuffWithDOM(domContent) {
    console.log("I received the following DOM content:\n" + domContent);
}

chrome.browserAction.onClicked.addListener(function(tab) {
    console.log('will this run bg scrpt blow');
    chrome.tabs.sendMessage(tab.id, { text: "report_back" },doStuffWithDOM);
});

的script.js

debugger;
/* Listen for messages */
console.log('content script');
chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
    /* If the received message has the expected format... */
    if (msg.text && (msg.text == "report_back")) {
        /* Call the specified callback, passing
           the web-pages DOM content as argument */
        sendResponse(document.all[0].outerHTML);
    }
});

当我点击浏览器上的chrome图标时,我没有看到任何类型的活动。从打开的标签中获取DOM内容并进行操作的正确过程是什么。

0 个答案:

没有答案