2014-12-06 65 views
0

所以我只是在每次點擊擴展按鈕時都想得到響應。因此,與類似的AdBlock如何歸結每次點擊擴展按鈕時都無法播放響應

enter image description here

但是,相反,我只是試圖做一個console.log()每次單擊該按鈕沒有任何明顯的彈出窗口時間。

我已經試過這到目前爲止

chrome.extension.onMessage.addListener(
    function(request, sender, sendResponse) { 
     switch (request.directive) { 
     case "popup-click": 
      // execute the content script 
      chrome.tabs.executeScript(null, { // defaults to the current tab 
       file: "real.js", // script to inject into page and run in sandbox 
       allFrames: true // This injects script into iframes in the page and doesn't work before 4.0.266.0. 
      }); 
      sendResponse({}); // sending back empty response to sender 
      break; 
     default: 
      // helps debug when request directive doesn't match 
      alert("Unmatched request of '" + request + "' from script to background.js from " + sender); 
     } 
    } 
); 

然後我real.js

console.log("Yo"); 

但遺憾的是我只能得到一個Yo當它啓動。有任何想法嗎?

+0

發送此消息您期望什麼? – Xan 2014-12-06 22:46:07

+0

它不是'功能(請求,發件人,sendResponse){'? @Xan – Idris 2014-12-09 01:16:28

+0

這是聽衆。 – Xan 2014-12-09 01:17:26

回答

1

如果沒有彈出(沒有任何顯示當您單擊該按鈕),再有就是將觸發按鈕被點擊時的事件:

chrome.browserAction.onClicked

解僱當瀏覽器操作圖標被點擊時。如果瀏覽器操作彈出窗口,此事件不會觸發。

要使用:

// In your background script 
chrome.browserAction.onClicked.addListener(function() { 
    // Do stuff 
}); 

但是,如果你確實有一個彈出,那麼,作爲文檔提到,此事件將不會觸發。然後你的代碼更合適:你只需要從彈出窗口發送一條消息,並在後臺腳本中打開它。請參閱this answer中的完整示例。