在chrome扩展UI内单击按钮获取网站html

问题描述

一旦用户单击我的popup.html中的按钮,我希望能够从网页中抓取html。 当我尝试获取html时遇到了问题,它获取了我的chrome扩展名html,而不是网页。我尝试在网页中执行脚本,以便将dom更改为网页,但这对我也不起作用。

当前,我有以下与我所遇到的问题有关的代码...此时,我只是试图编写脚本来操纵background.js文件页面的html。

manifest.json
{
    "name": "xxxx","version": "1.0","description": "xxxxx","permissions": [
        "identity","tabs","http://*/*","https://*/*"
    ],"background":{
        "persistent": false,"scripts": ["background.js"]
    },"content_scripts": [{
        "matches": ["*://*.stackoverflow.com/*"],"js": ["content.js"]
    }],"externally_connectable": {
        "matches": [
            "*://*.nike.com/*","*://*.amazon.com/*"
        ]
    },"browser_action": {
        "default_title": "xxxxx","default_popup": "popup.html","default_icon": {
            "16": "images/logo_16.png","32": "images/logo_32.png","48": "images/logo_48.png","128": "images/logo_128.png","800":"images/logo_800.png"
        }
    },"icons": {
        "16": "images/logo_16.png","800":"images/logo_800.png"
    },"manifest_version": 2
}

background.js

let tabID = "";

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

chrome.extension.onConnect.addListener(function(port) {
  console.log("Connected .....");
  port.onMessage.addListener(function(msg) {
       console.log("message recieved" + msg);
       chrome.tabs.executeScript({code: 'alert(Getting cart)'});
       port.postMessage(chrome.tabs.sendMessage(tabID,{text: 'report_back'},doStuffWithDom));
  });
})

chrome.tabs.onActivated.addListener(function (activeInfo) {
  tabID = activeInfo.tabId;
  console.log(tabID);
});

script.js

if (document.getElementById("scrapeBtn")){
    document.getElementById("scrapeBtn").addEventListener("click",Scrape);
}else {
    document.getElementById("scrapeBtn").removeEventListener("click",Scrape);
}

function Scrape() {
    var port = chrome.extension.connect({
        name: "Sample Communication"
   });
   port.postMessage("Hi BackGround");
   port.onMessage.addListener(function(msg) {
        console.log("message recieved" + msg);
   });
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)