Chrome插件消息傳遞實例


首先吐槽“360極速瀏覽器應用開發平台”的開發文檔,在消息傳遞(http://open.chrome.360.cn/extension_dev/messaging.html)一節中,翻譯極其低劣:

Sending a request from the extension to a content script looks very similar, except that you need to specify which tab to send it to. This example demonstrates sending a message to the content script in the selected tab.

傳遞一個請求到擴展很容易,你需要指定哪個標簽發起這個請求。下面這個例子展示了如何指定標簽發起一個請求。

similar翻譯成容易,發送到tab翻譯成標簽發起。

代碼更新也不及時,chrome.tabs.getSelected和chrome.extension.sendRequest這兩個函數在新版Chrome已經廢棄(參考資料[1])。

學習過程中偷懶不看英文,結果反而被繞個大圈子。

1、開發文檔

Chrome的消息傳遞,可以在Web(通過content script)和擴展之間進行,任意一方都可發送或接收消息。

Web(通過content script發送消息如下:


  
  
 
 
         
  1. chrome.runtime.sendMessage({greeting: “hello”}, function(response) {
  2. console.log(response.farewell);
  3. });
通過chrome.runtime.sendMessage函數發送消息,其中{greeting: “hello”}是消息對象(大括號的用法見參考資料[2]);function(response) {…}是回調函數,處理接收方發回的消息。
插件發送消息需要確定接收的Tab,如下:


  
  
 
 
         
  1. chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  2. chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
  3. console.log(response.farewell);
  4. });
  5. });
以上代碼指定當前tab為消息的接收者。
接受端需要通過runtime.onMessage事件處理消息:

  
  
 
 
         
  1. chrome.runtime.onMessage.addListener(
  2. function(request, sender, sendResponse) {
  3. console.log(sender.tab ?
  4. "from a content script:" + sender.tab.url :
  5. "from the extension");
  6. if (request.greeting == "hello")
  7. sendResponse({farewell: "goodbye"});
  8. });

事件處理通過function(request, sender, sendResponse){…}完成,以上代碼處理消息時在控制台記錄發送者的消息,回復“goodbye”消息。

2、完整示例
示例完成content script和插件的消息傳遞,具體包括:
1、content script發送消息,backgroud接收消息;
2、popup發送消息,content script接收消息。

manifest.json


  
  
 
 
         
  1. {
  2. "manifest_version": 2,
  3. "name": "Say Hello",
  4. "description": "This extension say hello to you.",
  5. "version": "1.0",
  6. "permissions": ["tabs", " <all_urls>"],
  7. "browser_action": {
  8. "default_icon": "icon.png",
  9. "default_popup": "popup.html"
  10. },
  11. "background": {
  12. "page": "background.html"
  13. },
  14. "content_scripts": [{
  15. "matches": [" <all_urls>"],
  16. "js": ["contentscript.js"]
  17. }]
  18. }

contentscript.js

  
  
 
 
         
  1. chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
  2. console.log(response.farewell);
  3. });
  4. chrome.runtime.onMessage.addListener(
  5. function(request, sender, sendResponse) {
  6. console.log(sender.tab ?
  7. "from a content script:" + sender.tab.url :
  8. "from the extension");
  9. if (request.greeting == "hello"){
  10. sendResponse({farewell: "I'm contentscript,goodbye!"});
  11. }
  12. });

background.html


  
  
 
 
         
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>
  5. Getting Started Extension's Popup
  6. </title>
  7. <script src="background.js">
  8. </script>
  9. </head>
  10. <body>
  11. <p>
  12. Hello!
  13. </p>
  14. <div id="r">
  15. </div>
  16. </body>
  17. </html>

background.js


  
  
 
 
         
  1. chrome.runtime.onMessage.addListener(
  2. function(request, sender, sendResponse) {
  3. console.log(sender.tab ?
  4. "from a content script:" + sender.tab.url :
  5. "from the extension");
  6. if (request.greeting == "hello")
  7. sendResponse({farewell: "I'm backgroud,goodbye!"});
  8. });

popup.html

  
  
 
 
         
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>
  5. Getting Started Extension's Popup
  6. </title>
  7. <script type="text/javascript" src="jquery.js">
  8. </script>
  9. <script src="popup.js">
  10. </script>
  11. </head>
  12. <body>
  13. <p>
  14. Hello!
  15. </p>
  16. <div id="r">
  17. </div>
  18. </body>
  19. </html>

popup.js


  
  
 
 
         
  1. chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  2. chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
  3. console.log(response.farewell);
  4. });
  5. });

參考資料
[1]Content腳本與擴展的其他頁面腳本的消息傳遞http://blog.csdn.net/greatpresident/article/details/9052801
[2]Javascript中大括號“{}”的多義性http://www.cnblogs.com/snandy/archive/2011/02/28/1966894.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM