简体   繁体   English

Chrome扩展程序-更改弹出图标onClick / onHover

[英]Chrome extension - changing the popup icon onClick / onHover

I am trying to find a way to change the extension's icon once the popup is either open or hovered. 我试图找到一种方法,可以在弹出窗口打开或悬停时更改扩展程序的图标。 I found a way to do it when opening the popup, using: 我找到了一种在打开弹出窗口时使用的方法:

chrome.browserAction.onClicked.addListener(updateIcon);

but,couldn't find a way to catch the popup close event. 但是,找不到捕捉弹出窗口关闭事件的方法。 i tried to use this from the popup code: 我试图从弹出代码中使用它:

 var port = chrome.runtime.connect();
  port.onDisconnect.addListener(function()
  {
     alert('closed');
  });

But was unsuccessful. 但是没有成功。

Any ideas? 有任何想法吗?

Thanks. 谢谢。

Assuming the popup page is connecting to a background script, try moving the Port.onDisconnect code to the background script: 假设弹出页面正在连接到后台脚本,请尝试将Port.onDisconnect代码移动到后台脚本:

  port.onDisconnect.addListener(function()
  {
     alert('closed');
  });

I'm assuming that your way wasn't working because after you close the popup, it's dead and cannot detect the Port.onDisconnect event. 我假设您的方法行不通,因为关闭弹出窗口后,它已经死了,无法检测到Port.onDisconnect事件。 If you move that to your background script, it should still be alive, would be notified when the Port was disconnected, and could change the button's icon itself! 如果将其移至后台脚本,则该脚本仍应处于活动状态,并在断开端口连接时得到通知,并且可以更改按钮的图标本身!

As for changing the icon when the extension is hovered, it would depend on if you mean to do it when the button on the toolbar is hovered or to do it when the popup page is hovered. 至于悬停扩展时更改图标,取决于您是要悬停工具栏上的按钮时还是悬停弹出页面时执行。 If you mean the former, I do not think this is possible. 如果您指的是前者,我认为这是不可能的。 If you mean the latter, the popup page should be able to do it through an event listener and your "updateIcon" function. 如果您指的是后者,则弹出页面应该能够通过事件侦听器和您的“ updateIcon”函数来执行此操作。

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

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