I am building a page action chrome extension wherein I need to send a message from content script to background page. For example, if the content script is injected on youtube.com, I first send a message from content.js
to background.js
to display the icon. Now, I need to send another message to background page when this tab is closed.
For content.js
, I have written code similar to following:
chrome.runtime.sendMessage({todo: "showPageAction",message: someotherinfo});
window.addEventListener("beforeunload",function(e){
chrome.runtime.sendMessage({todo: "removeTab",message: someotherinfo});
});
Although this works fine (most of the times), I get the error Uncaught Error: Extension context invalidated
. I am new to this and any clarification/suggestion would be helpful.