Hi,
I’m developing an addon for real-time synchronization of open tabs between browsers.
I’ve discovered an inconsistency in the ordering of method executions between Firefox on desktop and Firefox on Android which is a bit problematic for me.
In my addon I need to be notified when the user opens a new tab, so I’m subscribing to browser.tabs.oncreated
event to tell the server to create a tab on another browser. This results in creating the tab in the other browser with the usage of browser.tabs.create()
call. To prevent a loop I need to detect whether the tab was created by user or programmatically by the addon (and not call the server in this case).
To achieve this, I do something like this:
var tabsCreatedByAddon = {}
browser.tabs.onCreated.addListener(function(createdTab) {
if (!tabsCreatedBySynchronizer.hasOwnProperty(createdTab.id)) {
// Call the server if the tab has not been created by us.
}
});
function someCallbackCalledByServer(newTabProperties) {
browser.tabs.create(newTabProperties).then(function(tab) {
tabsCreatedBySynchronizer[tab.id] = true;
});
};
On desktop browser it works fine but on Android it doesn’t because onCreated is executed before the Promise then
method is executed.
For example
browser.tabs.onCreated.addListener(function(createdTab) {
console.log("OnCreated:");
console.log(createdTab);
});
var newTabProperties = {
index: 1,
url: "http://www.google.com"
};
browser.tabs.create(newTabProperties).then(function(tab) {
console.log("result from create: ");
console.log(tab);
});
On desktop it logs:
result from create:
Object { id: 1, index: 1, windowId: 0, selected: true, highlighted: true, active: true, pinned: false, status: “loading”, incognito: false, width: 1280, 5 more… }
OnCreated:
Object { id: 1, index: 1, windowId: 0, selected: true, highlighted: true, active: true, pinned: false, status: “loading”, incognito: false, width: 1280, 5 more… }
While on Android it writes:
OnCreated:
Object { id: 16, index: 1, windowId: 1, selected: true, highlighted: true, active: true, pinned: false, status: “loading”, incognito: false, width: 1024, 4 more… }
result from create:
Object { id: 16, index: 1, windowId: 1, selected: true, highlighted: true, active: true, pinned: false, status: “loading”, incognito: false, width: 1024, 4 more… }
Mozilla desktop: 52.0
Mozilla Android: 54.0b2
Is it expected behavior? If so, what’s the preferred solution to my problem? Is it possible to retrieve a tab id before onCreated event()? For now I will do a workaround in which I save a url to the dictionary before a call to create() and remove it after retrieving result.
Edit: Seems like the url is not passed to onCreated and then()… Great…
Thanks.