Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

google chrome extension - can I execute code inside a popup when it is being closed?

in my chrome extension I want the popup whenever it is being closed to send a message to the background script. This message should contain the current content of a text area on the popup as well as the url of the tab the popup was opened on.

window.onblur = function(){
    let text = document.getElementById("txtarea").value;
    chrome.runtime.sendMessage({msg: text});
}

With just the content of the text area everything works fine. However when i want to add the url of the current tab like this:

window.onblur = function(){
    let text = document.getElementById("txtarea").value;
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
        let currentTab = tabs[0].url;
        chrome.runtime.sendMessage({msg: text; url: currentTab});
    });
}

nothing is received by the background script before the popup is closed. I'm guessing this is just because it takes too long right? Is there a way I could make this work? It is important that I really get the url of the tab the popup was opened on. When I for example move the query to the background script and close the popup by switching tabs the url of the new tab is being printed out instead of the one I want.

Thank you :)

question from:https://stackoverflow.com/questions/65888890/can-i-execute-code-inside-a-popup-when-it-is-being-closed

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...