简体   繁体   English

Chrome扩展程序中脚本之间的通信

[英]Communication between scripts in Chrome Extension

I know there are many variations of this question already in existence here, but none of them seem to work for me. 我知道这个问题已经存在很多变种,但是似乎没有一个对我有用。

Details: 细节:

I'm writing an extension that pulls some email data from emails you send in gmail. 我正在编写一个扩展程序,该扩展程序可以从您以gmail发送的电子邮件中提取一些电子邮件数据。 In order to achieve this I am using this version of Gmailr https://github.com/joscha/gmailr . 为了实现此目的,我正在使用此版本的Gmailr https://github.com/joscha/gmailr

In effect, I have three content scripts: Gmailr.js and main.js (which are pretty much identical to those in the link above) allow me to pull out the information I'm looking for. 实际上,我有三个内容脚本:Gmailr.js和main.js(与上面的链接中的脚本几乎相同)允许我提取所需的信息。 Then content.js I use to send a message to the background page of the extension. 然后,我使用content.js将消息发送到扩展的后台页面。

The problem is that from gmailr.js and main.js I cannot use any of the Chrome APIs, and I'm not really sure why, so I can't send messages from these back to the background page. 问题是从gmailr.js和main.js中我无法使用任何Chrome API,而且我不确定原因,因此无法将这些消息发送回后台页面。

That is why I made content.js which can communicate with the background page. 这就是为什么我制作了可以与后台页面通信的content.js的原因。 However, it does not seem to be able to see anything the other content scripts do. 但是,它似乎看不到其他内容脚本可以执行的任何操作。 For example, main.js inserts a div at the top of the page. 例如,main.js在页面顶部插入一个div。 When I try to attach an event listener to a button in this div from content.js, I am told that no such element exists. 当我尝试将事件监听器附加到content.js的此div中的按钮上时,被告知不存在此类元素。

How can I get the data pulled out by main.js to be seen by content.js? 如何获取main.js提取的数据以供content.js查看? (I also tried to put the data in local storage, then trigger a custom event listener to tell content.js to read local storage, but no luck because they don't seem to be able to hear each other's event being triggered). (我还尝试将数据放入本地存储,然后触发一个自定义事件侦听器,以告诉content.js读取本地存储,但是没有运气,因为它们似乎无法听到彼此触发的事件)。

Any insight or alternatives are much appreciated. 任何见解或替代方案都将受到赞赏。

(I can post code if necessary, but it's fragmented and long) (如果需要,我可以发布代码,但是它是零散的并且很长)

My manifest file: 我的清单文件:

{
  "manifest_version": 2,
  "name": "Email extractor",
  "description": "Extracts data from emails",
  "version": "1.0",
  "background": {
     "script": "background.js"
  },
  "content_scripts": [
    {
      "matches": [
        "*://mail.google.com/*",
        "*://*/*"
      ],
      "js": [
        "lib/yepnope.js/yepnope.1.5.4-min.js",
        "lib/bootstrap.js",
        "main.js",
        "gmailr.js",
        "content.js"

      ],
      "css": [
        "main.css"
      ],
      "run_at": "document_end"
    }
  ],
  "permissions": [
    "tabs",
    "storage",
    "background",
    "*://mail.google.com/*",
    "*://*/*"
  ],

  "browser_action": {
    "default_icon": "img/icon.png",
    "default_popup": "popup.html"
  },

  "web_accessible_resources" : [
    "writeForm.js",
    "disp.js",
    "/calendar/jsDatePick.min.1.3.js",
    "/calendar/jsDatePick_ltr.min.css",
    "lib/gmailr.js",
    "lib/jquery-bbq/jquery.ba-bbq.min.js",
    "content.js",
    "main.js",
    "background.js"
  ]
} 

This is main.js: 这是main.js:

Gmailr.init(function(G) {
    sender = G.emailAddress();
    G.insertTop($("<div id='gmailr'><span></span> <span id='status'></span>)");
    el = document.getElementById("testid");
    el.addEventListener('click', mg, false);
    var status = function(msg) {
       G.$('#gmailr #status').html(msg); };    
       G.observe(Gmailr.EVENT_COMPOSE, function(details) {        
         ....
         status(" user: " + user);
         console.log('user:', user);
         //now try to send a message to the background page
         //this always returns the error that method sendMessage does not exist for     undefined
        chrome.runtime.sendMessage({greeting: "test from gmailr"}, function(response) {
        console.log("did it send?");
        });
    });


});

gmailr.js is quite long and is also not my own code but it can be seen here: http://pastebin.com/pK4EG9vh gmailr.js很长,也不是我自己的代码,但是可以在这里看到: http : //pastebin.com/pK4EG9vh

Hi perhaps 3 likely reason to your problem : 您好,可能是您遇到问题的3个原因:

  1. The way you send messages to bgp from main.js and gmailr.js are perhaps wrong because you must arrive to communicate from any content script to your bgp. 从main.js和gmailr.js向bgp发送消息的方式可能是错误的,因为您必须到达才能将任何内容脚本与bgp进行通信。 (in your manifest content script key the gmailr.js is missing). (在清单内容脚本密钥中,缺少gmailr.js)。 Show us your code it would help. 向我们展示您的代码会有所帮助。

  2. You seems to have a problem with the moment you search from content.js to access to the element created in main.js. 从content.js搜索以访问在main.js中创建的元素时,您似乎遇到了问题。 Do you try to access your element with the jQuery $("").on() method ? 您是否尝试使用jQuery $(“”)。on()方法访问元素? A simple test must be to declare a function in one cs and to use it in another. 一个简单的测试必须是在一个cs中声明一个函数,然后在另一个cs中使用它。 If it's not working it's a manifest problem. 如果它不起作用,那就是明显的问题。 The order you declare .js file in manifest content script key is important also. 在清单内容脚本密钥中声明.js文件的顺序也很重要。

  3. try to in the manifest content script array "run_at":"document_end" 尝试在清单内容脚本数组“ run_at”中:“ document_end”

Hope it help ! 希望对您有所帮助!

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

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