browser.browserAction.onClicked.addListener is detached from debug

24 Views Asked by At

update: I cleared all the Uncaught (in promise) Error: An unexpected error occurred that I had in my code, this is no longer the cause for browser.browserAction.onClicked.addListener going unresponsive, I found the single action that makes my extension unresponsive to debug and this prevents me from further debugging the app, it looks like the "freeze" also prevents the app from reading the storage.

The below code is what I can run once before the app turns unresponsive and detatched from the debug:

    browser.browserAction.onClicked.addListener(function (tab) {        
        // Check some condition to determine whether to open a page or show the default popup
        console.log(workstationData);
        if (workstationData.activated == false){
            browser.tabs.create({ url: browser.runtime.getURL("../pages/setup.html") });
        } else {

            if (workstationData.password && !workstationData.unlocked){

                if (popupId > -1) {
                    chrome.windows.update(popupId, { focused: true });
                } else {                                
                    const screenWidth = window.screen.width;
                    const screenHeight = window.screen.height;                    
                    const topOffset = Math.floor((screenHeight - 340) / 2);
                    const leftOffset = Math.floor((screenWidth - 416) / 2);                    

                    //browser.tabs.create({ url: browser.runtime.getURL("../pages/snip-password.html") });
                    browser.windows.create({
                      url: "../pages/snip-password.html",
                      type: "popup", // Type "popup" for always on top behavior (limited control)
                      width: 416,
                      height: 340,
                      top: topOffset, // Use top and left to position the window
                      left: leftOffset
                    })
                    .then(function (window){
                        popupId=window.id;
                    });      
                }          
            } else {
                // Set the default popup
                browser.browserAction.setPopup({ popup: "../popup/popup.html" });        
                // Open the default popup
                browser.browserAction.openPopup();
            }           
      }
    });

Considering the at that time state of workstationData we can remove the code paths not use and make the code more simple to read. the relevant action it performs is as follow:

                // Set the default popup
                browser.browserAction.setPopup({ popup: "../popup/popup.html" });        
                // Open the default popup
                browser.browserAction.openPopup();

After executing these lines the extension debugger is detatched and I'm no longer able to add breakpoints, no errors of sort, just I can no longer debug the app. The app is still wroking, it has other bugs to fix but if I'm unable to put breakpoints and check I'm stuck with other bugs happening past this point.

to note, if I replace the above two lines with the following one:

browser.tabs.create({ url: browser.runtime.getURL("../popup/popup.html") });

the app is not stuck and it also resolve the supposed thereafter bugs, it looks like that setting and then opening a popup cause some troubles.

For completness, this is the code in popup.js bound to the popup:

    var $sso;

    var $workstationData;

    document.addEventListener("DOMContentLoaded", function () {      

      // Send a message to background.js
      browser.runtime.sendMessage({ action: "get-workstation-data" }, response => {      

          $workstationData = response.data;

          if (($workstationData.account || $workstationData.password) && !$workstationData.unlocked ) {
              //data is locked, user needs to enter the password first
              document.getElementById("workstation-serial").innerText = "<workstation data is locked>";
          } else {
              document.getElementById("workstation-serial").innerText = $workstationData.serial;
          }
      });

    });  

    document.getElementById('settings').addEventListener('click', function() {
        browser.runtime.sendMessage({ action: "openTab", url:"settings" }, response => {

        });          
    });  

    document.getElementById('overview').addEventListener('click', function() {
        browser.runtime.sendMessage({ action: "openTab", url:"overview" }, response => {

        });          
    });      

    document.getElementById('referral').addEventListener('click', function() {
        browser.runtime.sendMessage({ action: "openTab", url:"referral" }, response => {

        });          
    });     

    document.getElementById('upgrade').addEventListener('click', function() {
        browser.runtime.sendMessage({ action: "openTab", url:"upgrade" }, response => {

        });          
    });                   
0

There are 0 best solutions below