简体   繁体   English

在Chrome扩展程序弹出窗口中获取DOM?

[英]Getting DOM in Chrome extension popup?

I'm trying to create a Chrome extension that displays the current page's DOM in a popup. 我正在尝试创建一个Chrome扩展程序,以在弹出窗口中显示当前页面的DOM。

As a warmup, I tried putting a string in getBackgroundPage().dummy , and this is visible to the popup.js script. 作为热身,我试图把一个字符串getBackgroundPage().dummy ,这可见popup.js脚本。 But, when I try saving the DOM in getBackgroundPage().domContent , popup.js just sees this as undefined . 但是,当我尝试将DOM保存在getBackgroundPage().domContent ,popup.js只是将其视为undefined

Any idea what might be going wrong here? 知道这里可能出什么问题吗?

I looked at this related post , but I couldn't quite figure out how to use the lessons from that post for my code. 我看了这篇相关的文章 ,但是我不太清楚如何在我的代码中使用那篇文章中的课程。


Code: 码:

background.js background.js

chrome.extension.getBackgroundPage().dummy = "yo dummy"; 

function doStuffWithDOM(domContent) {
    //console.log("I received the following DOM content:\n" + domContent);
    alert("I received the following DOM content:\n" + domContent);
    //theDomContent = domContent;
    chrome.extension.getBackgroundPage().domContent = domContent;
}

chrome.tabs.onUpdated.addListener(function (tab) {
    //communicate with content.js, get the DOM back...
    chrome.tabs.sendMessage(tab.id, { text: "report_back" }, doStuffWithDOM); //FIXME (doesnt seem to get into doStuffWithDOM)
});

content.js content.js

/* Listen for messages */
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 */

        //alert("hi from content script"); //DOESN'T WORK ... do we ever get in here?
        sendResponse(document.all[0].outerHTML);
    }
});

popup.js popup.js

document.write(chrome.extension.getBackgroundPage().dummy); //WORKS.
document.write(chrome.extension.getBackgroundPage().domContent); //FIXME (shows "undefined")

popup.html popup.html

<!doctype html>
<html>
  <head>
    <title>My popup that should display the DOM</title>       
    <script src="popup.js"></script>
  </head>
</html>

manifest.json manifest.json

{
"manifest_version": 2,
"name":    "Get HTML example w/ popup",
"version": "0.0",

"background": {
    "persistent": false,
    "scripts": ["background.js"]
},
"content_scripts": [{
    "matches": ["<all_urls>"],
    "js":      ["content.js"]
}],
"browser_action": {
    "default_title": "Get HTML example",
    "default_popup": "popup.html"
},

"permissions": ["tabs"]
}

You got the syntax of chrome.tabs.onUpdated wrong. 您的chrome.tabs.onUpdated语法错误。

In background.js 在background.js中

chrome.tabs.onUpdated.addListener(function(id,changeInfo,tab){
    if(changeInfo.status=='complete'){ //To send message after the webpage has loaded
        chrome.tabs.sendMessage(tab.id, { text: "report_back" },function(response){
           doStuffWithDOM(response);
        });
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM