简体   繁体   English

Chrome扩展程序弹出窗口在错误的时间触发

[英]Chrome extension popup close fired at wrong time

I have a chrome extension which is currently consists of a background page and a popup page. 我有一个chrome扩展程序,该扩展程序当前由一个背景页面和一个弹出页面组成。 There are some initialization happens when the popup is opened. 打开弹出窗口时会发生一些初始化。 I am using DOM event 我正在使用DOM事件

doc.addEventListener('DOMContentLoaded', function() { ... }

And the behaviour is correct. 并且行为是正确的。 The issue is when closing the popup. 问题是关闭弹出窗口时。 Since chrome popup does not throw unload event I am using what was suggested here . 由于chrome弹出窗口不会引发unload事件,因此我使用此处建议的内容。 Here is my code 这是我的代码

popup.js popup.js

bgPage = chrome.extension.getBackgroundPage();
bgPageManager = bgPage.manager(); // This is the exposed api from bg page
bgPageManager.init(chrome.runtime.connect({
    name: 'P1'
}));

Here connecting to the runtime and sending the port to background page so that it can listen to onDisconnect event. 在这里,连接到运行时并将端口发送到后台页面,以便它可以侦听onDisconnect事件。

Background.js Background.js

function init(port) {
    port.onDisconnect.addListener(function() {
        // Clean up happens here
        stateManager.unregister();
    });
}

This works as well. 这也可以。

But the issue is, this onDisconnect getting fired when the popup is getting opened, not when it is getting closed 但是问题是, 打开弹出窗口时(而不是关闭窗口时)会触发onDisconnect

The documentation for onDisconnect event is onDisconnect事件的文档

An object which allows the addition and removal of listeners for a Chrome event. 一个对象,允许添加和删除Chrome事件的侦听器。

Which is not every helpful ;) 并不是每个都有帮助;)

So is there anything wrong I am doing, or any way when I can detect the popup close? 那么,我在做任何错误的事情,还是可以检测到弹出窗口关闭的任何方式?

Unless there's something listening to chrome.runtime.onConnect in the popup page (from your comment, doesn't seem so), chrome.runtime.connect() will return a Port that immediately closes (as no-one was willing to listen). 除非弹出窗口中有人监听chrome.runtime.onConnect (根据您的评论,似乎并非如此), chrome.runtime.connect()将返回一个立即关闭的Port(因为没人愿意听) 。

You're definitely making it more difficult than it should be with involving getBackgroundPage though. 但是,与使用getBackgroundPage相比,您肯定比原来要困难的多。 The popup can initiate the port itself. 弹出窗口可以启动端口本身。 All you need to do is: 您需要做的只是:

// popup code
var bgPort = chrome.runtime.connect({name: "P1"});

// background code
var popupPort;
chrome.runtime.onConnect.addListener(function(port) {
  if(port.name == "P1") {
    popupPort = port;
    popupPort.onDisconnect.addListener(function() {
      /* Clean up happens here */
    });
  }
});

In case you want to preserve what you already have, the minimal code to put into the popup is this: 如果您想保留已有的内容,则在弹出窗口中放入的最少代码是:

var bgPort;
chrome.runtime.onConnect.addListener(function(port) {
  if(port.name == "P1") {
    bgPort = port;
  }
});

Note that in all cases you need to keep a reference to the Port object on both sides. 请注意,在所有情况下,都需要在两侧都保留对Port对象的引用。 If the garbage collector collects it, the port will be disconnected. 如果垃圾收集器对其进行收集,则端口将断开连接。

Finally, a port name is optional; 最后,端口名称是可选的。 if it's the only port you use, you can drop the code that sets/checks the name. 如果这是您使用的唯一端口,则可以删除设置/检查名称的代码。

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

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