首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Chrome扩展如何将数据从内容脚本发送到popup.html

Chrome扩展如何将数据从内容脚本发送到popup.html
EN

Stack Overflow用户
提问于 2013-11-16 23:10:11
回答 1查看 96.7K关注 0票数 105

我知道这一点在很多帖子里都被问到了,但老实说我不明白。我对JavaScript、Chrome扩展和所有的东西都是新手,我有这个类的分配。所以我需要做一个插件,使用跨域请求来计算任何给定页面上的DOM对象。到目前为止,我已经使用Chrome扩展API实现了这一点,现在的问题是我需要在popup.html页面上显示来自contentScript.js文件的数据。我不知道该怎么做,我试着阅读了文档,但我就是不明白该怎么做。

以下是目前为止的代码。

manifest.json

代码语言:javascript
复制
{
"manifest_version":2,

"name":"Dom Reader",
"description":"Counts Dom Objects",
"version":"1.0",

"page_action": {
    "default_icon":"icon.png",
    "default_title":"Dom Reader",
    "default_popup":"popup.html"
},

"background":{
    "scripts":["eventPage.js"],
    "persistent":false
},

"content_scripts":[
    {
        "matches":["http://pluralsight.com/training/Courses/*", "http://pluralsight.com/training/Authors/Details/*",                                          "https://www.youtube.com/user/*", "https://sites.google.com/site/*", "http://127.0.0.1:3667/popup.html"],
        "js":["domReader_cs.js","jquery-1.10.2.js"]
        //"css":["pluralsight_cs.css"]
    }
],

"permissions":[
    "tabs",
    "http://pluralsight.com/*",
    "http://youtube.com/*",
    "https://sites.google.com/*",
    "http://127.0.0.1:3667/*"
]

popup.html

代码语言:javascript
复制
<!doctype html>
<html>

    <title> Dom Reader </title>    
    <script src="jquery-1.10.2.js" type="text/javascript"></script>
    <script src="popup.js" type="text/javascript"></script>

<body>
    <H1> Dom Reader </H1>
    <input type="submit" id="readDom" value="Read DOM Objects" />

   <div id="domInfo">

    </div>
</body>
</html>

eventPage.js

代码语言:javascript
复制
var value1,value2,value3;

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.action == "show") {
    chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
        chrome.pageAction.show(tabs[0].id);
    });
}

value1 = request.tElements;
});

popup.js

代码语言:javascript
复制
$(function (){
$('#readDom').click(function(){
chrome.tabs.query({active: true, currentWindow: true}, function (tabs){
    chrome.tabs.sendMessage(tabs[0].id, {action: "readDom"});

 });
});
});

contentScript

代码语言:javascript
复制
var totalElements;
var inputFields;
var buttonElement;

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse){
if(request.action == "readDom"){

    totalElements = $("*").length;
    inputFields = $("input").length;
    buttonElement = $("button").length;


}
})

chrome.runtime.sendMessage({ 
action: "show", 
tElements: totalElements, 
Ifields: inputFields, 
bElements: buttonElement 

});

任何帮助都将不胜感激,请避免我做的任何无关紧要的事情:)

EN

回答 1

Stack Overflow用户

发布于 2013-11-17 01:59:42

为此,您可以使用localStorage。您可以在浏览器内存中以哈希表格式存储任何数据,然后随时访问它。我不确定我们是否可以从内容脚本访问localStorage (它之前被阻止了),试着自己去做。下面是如何通过你的后台页面(我首先将数据从内容脚本传递到后台页面,然后将其保存在localStorage中):

在contentScript.js中:

代码语言:javascript
复制
chrome.runtime.sendMessage({
  total_elements: totalElements // or whatever you want to send
});

在eventPage.js (您的背景页面)中:

代码语言:javascript
复制
chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse){
       localStorage["total_elements"] = request.total_elements;
    }
);

然后,您可以使用localStorage"total_elements“在popup.js中访问该变量。

也许您可以在现代浏览器中直接从内容脚本访问localStorage。这样你就不需要在后台页面中传递数据了。

关于localStorage的很好的阅读:http://diveintohtml5.info/storage.html

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20019958

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档