diff --git a/lib/showforpage.js b/lib/showforpage.js index 8fcacde..cb95f7f 100644 --- a/lib/showforpage.js +++ b/lib/showforpage.js @@ -5,7 +5,7 @@ var Ci = require("chrome").Ci, Cr = require("chrome").Cr, winUtils = require("sdk/deprecated/window-utils"), winUtilsNew = require("sdk/window/utils"), - tabBrowser = require("sdk/deprecated/tab-browser"), + tabsUtils = require("sdk/tabs/utils"), ShowForPage; ShowForPage = function (options) { @@ -17,7 +17,7 @@ ShowForPage = function (options) { var windowTracker, tabTracker, // Methods used internally - getContentDocument, windowPageShowEvent, windowLinkEvent, tabProgressListener, + getContentDocument, activeTab, windowPageShowEvent, windowLinkEvent, tabProgressListener, // Methods exposed externally remove; @@ -49,6 +49,10 @@ ShowForPage = function (options) { } }; + activeTab = function () { + return tabsUtils.getSelectedTab(winUtilsNew.getMostRecentBrowserWindow()); + }; + windowPageShowEvent = function (event) { var doc = event.originalTarget, href = doc.location.href; @@ -57,7 +61,7 @@ ShowForPage = function (options) { return; } - options.onPageShow.call(doc, href, getContentDocument(tabBrowser.activeTab).location.href !== href); + options.onPageShow.call(doc, href, getContentDocument(activeTab()).location.href !== href); }; windowLinkEvent = function (event) { @@ -81,7 +85,7 @@ ShowForPage = function (options) { rels[relStrings[i]] = true; } - inBackground = (getContentDocument(tabBrowser.activeTab).location.href !== href); + inBackground = (getContentDocument(activeTab()).location.href !== href); options.onLink.call(link, href, { rels : rels, href: link.href, title: link.title }, inBackground); }; @@ -101,7 +105,7 @@ ShowForPage = function (options) { } }; - if (options.onPageShow || options.onLink) { + if (options.onLocationChange || options.onPageShow || options.onLink) { windowTracker = new winUtils.WindowTracker({ onTrack: function (window) { var appcontent = window.document.getElementById("appcontent"); @@ -110,6 +114,20 @@ ShowForPage = function (options) { return; } + if (options.onLocationChange || options.onPageShow) { + Array.prototype.forEach.call(window.document.querySelectorAll("tabbrowser"), function (tabbrowser) { + var doc, domReady; + + if (options.onLocationChange) { + tabbrowser.addProgressListener(tabProgressListener); + + doc = tabbrowser.contentDocument; + domReady = (doc.readyState === 'complete' || doc.readyState === 'interactive'); + + options.onLocationChange.call(doc, doc.location.href, domReady); + } + }); + } if (options.onPageShow) { appcontent.addEventListener('pageshow', windowPageShowEvent, true); } @@ -124,6 +142,11 @@ ShowForPage = function (options) { return; } + if (options.onLocationChange) { + Array.prototype.forEach.call(window.document.querySelectorAll("tabbrowser"), function (tabbrowser) { + tabbrowser.removeProgressListener(tabProgressListener); + }); + } if (options.onPageShow) { appcontent.removeEventListener('pageshow', windowPageShowEvent, true); } @@ -134,28 +157,6 @@ ShowForPage = function (options) { }); } - if (options.onLocationChange || options.onPageShow) { - tabTracker = new tabBrowser.Tracker({ - onTrack: function (tabbrowser) { - var doc, domReady; - - if (options.onLocationChange) { - tabbrowser.addProgressListener(tabProgressListener); - - doc = tabbrowser.contentDocument; - domReady = (doc.readyState === 'complete' || doc.readyState === 'interactive'); - - options.onLocationChange.call(doc, doc.location.href, domReady); - } - }, - onUntrack: function (tabbrowser) { - if (options.onLocationChange) { - tabbrowser.removeProgressListener(tabProgressListener); - } - } - }); - } - return { remove : remove };