I am very much new to this FF XUL based add-on development. While exploring, I found that FF will have multiprocess windows in coming releases and web content will no longer be accessed by Chrome process directly. So, I have written a frame script and chrome process script will asynchronously access the web content via message manager. Now, for my add-on I have used the Browser message manager, which works for only one for browser tab.
Now, this is the problem, I am facing: “How to load framescript only once per browser?”.
So, to detect that whether a framscript has already been loaded or not, I keep a “weakMap()” and put the browser object in it and check if the framescript is not loaded already for that browser. The code snippet looks like this:
var browserWeakMap = new WeakMap(); // A week browser map, which has already loaded the framescript..
function loadFrameScript() {
var BrowserObj = gBrowser.selectedBrowser;
var BrowserMessageManager = BrowserObj.messageManager;
if (!browserWeakMap.has(BrowserObj) || !browserWeakMap.get(BrowserObj)) {
BrowserMessageManager.loadFrameScript("chrome://extension/content/extn_framescript.js", true);
browserWeakMap.set(BrowserObj, true);
}
BrowserMessageManager.addMessageListener("extension:MessageFromContent", handleContentScriptMessage);
BrowserMessageManager.sendAsyncMessage("extension:MessageFromChrome", {
FunctionName: "InitWithDocTitle" /* Unique functionality command */
});
}
This code works fine when I am working with one single Firefox window. But problem occurs for this scenario : “A framescript is already been loaded in a browser and browserWeakMap has already this browser object in it. But if I drag this browser tab to create a new window, a new browserWeakMap has been created. So, for new window, same browser tab loads the framescript again.”
So, what should I do to keep track of browsers where the framescript has already been loaded or not keeping an eye on browser windows. We can create a new instance of the browser window by drag drop and we can merge the tabs from two windows to a single one.