2014-12-06 62 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中的完整示例。