本文介绍了请解释与Google Chrome扩展程序的背景沟通的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读并重新阅读此页面,并运行样本:



但我似乎没有把握如何在background.html,popup.html和content.js之间进行背景沟通。我想发送消息来触发功能,获取响应并处理这些响应。地图样本很接近帮助我,但我只需要一些超级简单的东西,而不需要所有的地图东西。 (注意,我知道jQuery以及Javascript,所以如果你愿意的话,可以随意混合一些jQuery。)

所有的扩展页面(背景页面,弹出窗口,信息栏,页面动作都在同一个扩展模块中运行,把它看作是一个域的网页,这个域名就是你的扩展ID,每个扩展页面就像一个普通页面(类似当你开发一个网站时)。



所有的扩展页面(上面提到的)都可以很容易地相互通信,你有多种方式可以这样做:




  1. 你直接做!我会尽可能使用这种方法。 >

      var bkg = chrome.extension.getBackgroundPage();`
    bkg.ping();`




  2. 如下所示,您可以使用也可以传递信息。当我希望它是面向事件的时候,我使用这种方法。我很少在扩展页面中使用它。



    popup.html
    $ b

      chrome.extension.sendRequest({method:'ping'},function(response){
    // response.result
    });

    background_page.html

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


现在,扩展页面和。请仔细阅读该文件以了解它。内容脚本不是扩展页面,你不能做什么扩展页面。您无法直接与上述任何页面进行沟通。内容脚本是在网页上下文中运行的JavaScript文件,而不是扩展页面。这是一个重要的区别。为了在扩展页面和内容脚本之间进行通信,您需要使用。该页面有很多信息,我强烈建议您阅读它。这与我们如何使用消息传递非常相似(上述第2步),但唯一不同的是您如何发送请求。您需要使用,因为您需要从您的扩展页面(背景,弹出窗口,页面等)向内容脚本发送单个请求。您需要知道标签的ID才能做到这一点。请查看获取更多信息。



如果您的扩展通常与您的内容脚本进行通信,您可以使用长连接,这在我上面喜欢的消息部分中有很好的解释。

对于类似的问题,我也回答了很多问题和其他人。由于你在JavaScript方面经验丰富,我强烈建议你阅读文档,它有你需要的一切。阅读API,我希望我现在了解内容脚本和扩展页面之间的区别,并且它们之间的通信是通过扩展消息传递。


I have read and re-read this page, as well as run the samples:

http://code.google.com/chrome/extensions/background_pages.html

But I don't seem to grasp how to do the background communication between the background.html, popup.html, and content.js. I want to send messages to trigger functions, get responses, and process those responses. The map sample was sort of close to helping me, but I just need something super simple and not need all that map stuff. (Note, I know jQuery as well as Javascript, so feel free to mix in a bit of jQuery if you want.)

解决方案

All extension pages (background page, popup, infobar, page action all run inside the same extension. Think of it as a webpage with one domain. And that domain is your extension ID. Each one of those extension pages is like a regular page (similar when you develop a website).

All extension pages (mentioned above) can communicate with each other easily, you have multiple ways doing so:

  1. chrome.extension.getBackgroundPage()

    You do it directly! I use this approach whenever I can. Its cleaner in my opinion.

    var bkg = chrome.extension.getBackgroundPage();`  
    bkg.ping();`
    

  2. chrome.extension.onRequest.addListener and chrome.extension.sendRequest

    As shown below, you can use extension messaging to pass information as well. I use this approach when I want it to be event oriented. I rarely use this within extension pages.

    popup.html

    chrome.extension.sendRequest({method: 'ping'}, function(response) {
       // response.result
    });
    

    background_page.html

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

Now there is a difference between "Extension Pages" and "Content Scripts". Please read that document carefully to understand it. A content script is not a extension page, you cannot do what extension pages do. You cannot directly communicate to any of those pages mentioned above. Content Scripts are JavaScript files that run in the context of web pages, not extension pages. Thats an important distinction to recognize.

So in order to communicate between your extension pages and content script, you need to use Messaging. That page has a lot of information, I highly recommend you to read it. It is very similar to how we used messaging (step 2 above), but the only different is how you send a request. You would need to use chrome.tabs.sendRequest, because you need to send a single request to the content script from your extension page (background, popup, page, etc). You would need to know the ID of your tab in order to do that. Please look at the Tab API for more info.

If your extension communicates with your content script very often, you can use Long Lived connections, which is explained pretty well in the Messaging section I liked above.

I have answered many questions and other people too regarding similar questions. Since your experienced in JavaScript, I highly recommend you to read the documentation, it has everything you need. Read the API, and I hope I you now understand the difference between Content Script and Extension Pages, and the communication between them is through Extension Messaging.

这篇关于请解释与Google Chrome扩展程序的背景沟通的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 23:04