简体   繁体   English

检测何时打开Chrome扩展程序弹出窗口

[英]Detecting when a Chrome Extension popup is opened

I am trying to wire some analytics into the onStartup event of my Chrome Extension per the docs . 我正在尝试根据文档将一些分析数据onStartup到我的Chrome扩展程序的onStartup事件中。 However, the event never seems to fire when the extension is opened via the icon in the browser. 但是,通过浏览器中的图标打开扩展程序时,似乎从未触发该事件。

Note that the onInstalled event in the below code fires as expected when the extension is installed, reloaded, etc. 请注意,安装,重新加载扩展等时,以下代码中的onInstalled事件会按预期触发。

chrome.runtime.onInstalled.addListener(function(details) {
  console.log('Extension installed: ' + details.reason);
});
chrome.runtime.onStartup.addListener(function() {
  console.log('Extension started');
});

Note I'm running Chrome v37 - the onStartup event has been available since v23. 请注意,我正在运行Chrome v37-自v23起,就可以使用onStartup事件。

You are trying to invoke code when a popup is opened. 您试图在打开弹出窗口时调用代码。 This is not the same as "starting" the extension - the chrome.runtime.onStartup event normally fires once per browser launch. 这与“启动”扩展名不同chrome.runtime.onStartup事件通常在每次浏览器启动时触发一次。

chrome.browserAction.onClicked event will not fire when a popup page is set; 设置弹出页面时, chrome.browserAction.onClicked事件不会触发; instead, you need to execute some code in the popup page itself (and that code will be executed each time the popup is opened). 相反,您需要在弹出页面本身中执行一些代码(并且该代码将在每次打开弹出窗口时执行)。

You can simply send your analytics event from the popup page itself. 您只需从弹出页面本身发送分析事件即可。 Or, if you prefer sending it from the background page, you just need to message it . 或者,如果您希望从后台页面发送它,则只需向发送消息

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

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