English 中文(简体)
Does onbeforeunload event trigger for popup.html in a google chrome extension?
原标题:

I m writing a google chrome extension with a popup and a background page. The popup subscribes to certain events that the background generates, and I would like to unsubscribe from those events when the popup goes away. However, I don t see either of onbeforeunload or onunload events being generated from the popup. Are these events fired? If not, any ideas on how to capture popup close?

问题回答

"beforeunload" is not fired for browser action popups. Believe me, I tried. "unload" should be fired correctly, however, so listen for that.

Actually, I have a similar system in place, that allows you to attach events using addEventListener, and they are automagically cleaned up when the unload event fires.

Here s a little tip: obviously you can t use console.log, because by the time the unload event fires, it s already too late. But, you can do this:

var background = chrome.extension.getBackgroundPage();

addEventListener("unload", function (event) {
    background.console.log(event.type);
}, true);

By using the above code, you can open up the background page s console, which should show that the unload event is fired correctly.

I ve had a kind of similar issue. In my case, I ve set the focus on an element which was in the popup page. So when the popup is closed, the element lose the focus and I can catch the blur event.

If you ve switched to using event pages instead of background pages in your extension, the recommended way of getting access to it is calling chrome.runtime.getBackgroundPage() with a function that gets called back with the background page reference.

However, if you try to get the background page in an onbeforeunload handler, like this:

document.addEventListener("beforeunload", function() {
    chrome.runtime.getBackgroundPage(function(backgroundPage) {
        backgroundPage.log("unloading");
    });
});

The callback doesn t seem to fire before the page unloads, so that log() call is never made.

In a comment above, Pauan suggested using chrome.extension.getBackgroundPage() to get the page. The only way I could get that to work was not to create the handler with addEventListener(). Instead I had to go old school and set the handler directly on window:

window.onbeforeunload = function() {
    chrome.extension.getBackgroundPage().log("unloading");
};

That finally got through to the background page.

Use the messaging APIs to set up a Port between the popup and background page. You then should get a onDisconnect event in the background page when the popup goes away.

// popup.js
chrome.runtime.connect({ name: "popup" });
// background.js
chrome.runtime.onConnect.addListener(function (port) {
    if (port.name === "popup") {
        port.onDisconnect.addListener(function () {
        console.log("popup has been closed");
    });
  }
});




相关问题
Redirect to cache URL in Chrome browser

self.location = cache: + self.location I want to redirect from "[URL]" to "cache:[URL]". I just want this code to work in Chrome browser.

我如何更新/重载 Chrome延?

I m在4号 Chrome(目前为4.0.249.0)中开发一个延伸点,显示用户在地位酒吧中的形象。 Ive设计了一个供用户使用的备选办法网页......

Setting Opacity in CSS For Chrome

I ve tried the following: (this is actually for fancybox, as the overlay does not show in chrome/safari: $("#fancy_overlay").css({<br /> background-color : opts....

Chrome Blocking Javascript, Google Wave, Google Gadgets

Project: Developing a gadget template for Google Wave which will allow my Flash movies to interact with the Wave api. I had to adapt the existing Flex application so that it would work with ...

image height using jquery in chrome problem

$( img ).height() returns 0 in chrome, but it returns the actual height in IE and firefox. Whats the actual method to get the height of the image in chrome?

jQuery block moving

I wanna move a div block to the top, so I coded like this: CSS part: .movingPart{ margin-top:80px; } jQuery part: $(document).ready(function() { $( #btn ).click(function() { $( .movingPart )....

Table cells bad rendering with Google Chrome/Webkit

On the following code, COL1A and COL3A are not 50% height with Chrome or Webkit. It works fine with IE7 and Firefox. <table border="1"> <tr> <td height="100%">COL 1A</td>...

热门标签