Chrome extension: detect when popup window closes

前端 未结 2 992
陌清茗
陌清茗 2020-12-06 16:11

For my chrome extension, I want to perform an action when the browserAction popup window closes. I understand that there no built-in events are triggered when this happens.

相关标签:
2条回答
  • 2020-12-06 16:18

    For reference, here's the working version of the background.js script:

    chrome.runtime.onConnect.addListener(function (externalPort) {
      externalPort.onDisconnect.addListener(function () {
        console.log("onDisconnect")
        // Do stuff that should happen when popup window closes here
      })
    
      console.log("onConnect")
    })
    
    0 讨论(0)
  • 2020-12-06 16:25

    onDisconnect is not an assignable property.
    It's an object that provides addListener method to register a callback:

    externalPort.onDisconnect.addListener(function() {
        var ignoreError = chrome.runtime.lastError;
        console.log("onDisconnect");
    });
    
    0 讨论(0)
提交回复
热议问题