如何获得从后台页面数据在谷歌浏览器扩展的内容脚本 [英] How do I get data from a background page to the content script in google chrome extensions

查看:117
本文介绍了如何获得从后台页面数据在谷歌浏览器扩展的内容脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图从我的背景页面数据发送到内容脚本在我的Chrome扩展程序。我似乎无法得到它的工作。我读过一些职位在线,但他们不是非常清晰,而且显得相当高的水平。我得设法让OAuth的使用上的Chrome样品中的Oauth接触的例子工作。认证工作,我可以得到的数据与打开一个新标签在HTML页面中显示它。

I've been trying to send data from my background page to a content script in my chrome extension. i can't seem to get it to work. I've read a few posts online but they're not really clear and seem quite high level. I've got managed to get the oauth working using the Oauth contacts example on the Chrome samples. The authentication works, i can get the data and display it in an html page by opening a new tab.

我想这个数据发送到内容脚本。

I want to send this data to a content script.

我遇到了很多麻烦这点,真的AP preciate,如果有人能够勾勒出你需要遵循从BG页面数据发送到内容脚本,甚至更好一些$ C $的明确的步骤C。任何考生?

i'm having a lot of trouble with this and would really appreciate if someone could outline the explicit steps you need to follow to send data from a bg page to a content script or even better some code. Any takers?

在code为我的背景页面如下(我已经排除了OAuth的paramaeters等)

the code for my background page is below (i've excluded the oauth paramaeters and other )

` function onContacts(text, xhr) {
    contacts = [];
    var data = JSON.parse(text);
    var realdata = data.contacts;
    for (var i = 0, person; person = realdata.person[i]; i++) {
      var contact = {
        'name' : person['name'],
        'emails' : person['email']
      };


      contacts.push(contact); //this array "contacts" is read by the 
 contacts.html page when opened in a new tab

    }

    chrome.tabs.create({ 'url' : 'contacts.html'}); sending data to new tab
    //chrome.tabs.executeScript(null,{file: "contentscript.js"});
    may be this may work?

  };

  function getContacts() {
    oauth.authorize(function() {
      console.log("on authorize");
      setIcon();
      var url = "http://mydataurl/";
      oauth.sendSignedRequest(url, onContacts);

    });
  };

  chrome.browserAction.onClicked.addListener(getContacts);`

因为我不太清楚如何获取数据到内容的脚本,我不会打扰张贴我失败的内容脚本的多个版本。如果我能刚上如何从我的内容脚本要求的人脉数组样本,以及如何从BG页面发送数据,这将是伟大的!

As i'm not quite sure how to get the data into the content script i wont bother posting the multiple versions of my failed content scripts. if I could just get a sample on how to request the "contacts" array from my content script, and how to send the data from the bg page, that would be great!

推荐答案

您有两种选择获取数据到内容脚本:

You have two options getting the data into the content script:


  1. 使用Tab键API:
    <一href=\"http://$c$c.google.com/chrome/extensions/tabs.html#method-executeScript\">http://$c$c.google.com/chrome/extensions/tabs.html#method-executeScript

  2. 使用消息:
    <一href=\"http://$c$c.google.com/chrome/extensions/messaging.html\">http://$c$c.google.com/chrome/extensions/messaging.html

我通常使用时,我的扩展将只是偶尔使用,例如,设置图像,因为这种方法我的桌面壁纸。人们不设置每一秒墙纸,或每分钟。他们通常做一个星期,甚至每天一次。所以,我只是注入一个脚本内容到该页面。这是pretty容易做到,你可以通过文件或code做到这一点,如文档中解释说:

Using Tab API

I usually use this approach when my extension will just be used once in a while, for example, setting the image as my desktop wallpaper. People don't set a wallpaper every second, or every minute. They usually do it once a week or even day. So I just inject a content script to that page. It is pretty easy to do so, you can either do it by file or code as explained in the documentation:

chrome.tabs.executeScript(tab.id, {file: 'inject_this.js'}, function() {
  console.log('Successfully injected script into the page');
});

使用邮件

如果你经常需要从你的网站上的信息,这将是更好地使用短信。有两种类型的邮件,长寿命和单请求。您的内容脚本(您在清单定义)可以监听延期请求:

Using Messaging

If you are constantly need information from your websites, it would be better to use messaging. There are two types of messaging, Long-lived and Single-requests. Your content script (that you define in the manifest) can listen for extension requests:

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
  if (request.method == 'ping')
    sendResponse({ data: 'pong' });
  else 
    sendResponse({});
});

和你的背景页面可以通过短信发送消息到内容脚本。如下图所示,它会得到当前选定的选项卡,然后发送到页面的请求。

And your background page could send a message to that content script through messaging. As shown below, it will get the currently selected tab and send a request to that page.

chrome.tabs.getSelected(null, function(tab) {
  chrome.tabs.sendRequest(tab.id, {method: 'ping'}, function(response) {
    console.log(response.data);
  });
});

取决于您的扩展,它的方法来使用。我都用了。为将每秒,每时间等中使用的延伸,我使用消息(长寿命)。对于不会使用,每次延期,那么你并不需要的内容脚本在每一个页面,你可以使用Tab API executeScript,因为它只是一个注入内容脚本,只要你需要。

Depends on your extension which method to use. I have used both. For an extension that will be used like every second, every time, I use Messaging (Long-Lived). For an extension that will not be used every time, then you don't need the content script in every single page, you can just use the Tab API executeScript because it will just inject a content script whenever you need to.

希望帮助!做#2搜索,有很多答案的内容脚本和背景的网页。

Hope that helps! Do a search on Stackoverflow, there are many answers to content scripts and background pages.

这篇关于如何获得从后台页面数据在谷歌浏览器扩展的内容脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆