mercredi 30 septembre 2020

Why doesn't this window open after closing it (chrome extension)?

So I'm building a simple chrome extension and I deal with popup windows. When the user installes or activates the extension, a popup should appear with instructions, then a user can click one of three buttons: "Done", "Skip", "Finish".

I have a problem with the "Skip" button: when a user clicks on it, it should "tell" the background script that Skip was clicked, then close itself (the window where the buttons are), then the background script will open a NEW window which is the same as the before one but with different data/information on it. Now the user can again choose between the three buttons and the cycle begins. The problem: if I click on the Skip button a few (2-3-4x) times, the new window will not open, sometimes even if I click only once, the window closes and doesn't open a new one.

The relevant code: background.js (this initializes the popup window and recieves datas from it):

window.open("notification.html", currentEx, 
"width=300,height=400,status=no,scrollbars=yes,resizable=no");

chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
    console.log("received " + msg.button);

    if(msg.button == "done"){
        console.log("done");
    }else if(msg.button == "skip"){
        randEx = Math.floor((Math.random() * 5));
        currentEx = something[randEx];
        console.log(currentEx);
        // currentEx is the data I want to access from the popup window that's why it is the name of the window

        var newWindow = window.open("notification.html", currentEx, 
        "width=300,height=400,status=no,scrollbars=yes,resizable=no");
    }else if(msg.button == "finish"){
        console.log("finish");
    }
});

notification.js (the popup window which is opened when the extension is activated or the user clicks on the skip):

var btnSkip = document.createElement("button");   
btnSkip.innerHTML = "Skip";  
btnSkip.onclick = function(){
    chrome.runtime.sendMessage({
        button: 'skip'
    }, function(){
        window.close();
    });
};                  
document.body.appendChild(btnSkip);  

I just can't see why the popup window won't open after 2 "Skip" or sometimes more click. It should close and open no matter how many times I do it, right? I can't figure out where is the problem, maybe the listener is wrong?




Aucun commentaire:

Enregistrer un commentaire