Reputation: 93
I am listening to page loads using sdk/tabs:
require('sdk/tabs').on('ready', function(tab) { console.log(tab.url); } );
It works as expected during normal navigation, but on some pages with script navigation the "ready" event doesn't fire. For example, load a Flickr photo page:
https://www.flickr.com/photos/42559357@N04/14696401803
and navigate to other photos using GUI navigation (arrows at the sides or icons at the bottom of the photo). The tab URL changes, but the listener is never called.
Is this normal? Is there any way to track such page loads?
ETA: I noticed that sdk/context-menu does not suffer from the same problem, which gave me an idea for a workaround:
var cm = require("sdk/context-menu");
cm.Item({
label: "dummy",
contentScript: 'self.on("context", function (node) {' +
' self.postMessage(document.URL);' +
' return false;' +
'});',
onMessage: function (pageUrl) {
console.log(pageUrl);
}
});
The function under onMessage will be triggered whenever the context menu is opened on a page, and tricky page changes like the ones described above will not confuse the script: it will accurately log the current URL.
As it happens, a context menu customized to a page URL is what I am aiming at, so this will probably work for me (I'll add URL filtering and a way to make sure that the payload is only delivered once per page). But I would still like an answer to the original question.
ETA2: page-mod has the same problem as tabs: the following will listen to new page openings on Flickr, but not when new pages are loaded via Flickr UI:
var pageMod = require("sdk/page-mod");
pageMod.PageMod({
include: '*.flickr.com',
contentScriptWhen: 'start',
contentScript: 'self.port.emit("pageOpened", document.URL);',
onAttach: function(worker) {
worker.port.on("pageOpened", function(pageUrl) {
console.log(pageUrl);
});
}
});
Upvotes: 3
Views: 1190
Reputation: 93
So two years later Firefox is ending support for its older addon platforms and forcing everyone to switch to WebExtensions. But for my Flickr addon this turned out to be a blessing, because now I can do what I want just by listening for tab URL changes from the background script:
chrome.tabs.onUpdated.addListener(onTabChange)
function onTabChange(tabId, changeInfo) {
if (changeInfo.url) {
// do stuff
}
}
This does actually work with Flickr's sneaky page updates.
Upvotes: 0
Reputation: 89
I had this issue. It was because Firefox "pre-loaded" the page so it was loaded with state="ready". Hence there was no page state change and the event wasn't fired.
In order to overcome this issue, I listen to the "open" event (tabs.on("open", ...)
) and check whether the page is in "ready" state:
tabs.on("open", function (tab) {
tab.on("pageshow", doSomething);
if (tab.readyState == "complete") {
doSomething(tab);
}
});
Upvotes: 2
Reputation: 5830
You should consider listening for the pageshow
event instead:
https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/tabs#pageshow
This api was specifically added to work around at least some of these types of issues. If the UI changes you need to hook are purely from JS though, you may need to add listeners for framework-specific events.
Upvotes: 1