Skip to content
This repository has been archived by the owner on May 13, 2018. It is now read-only.

Commit

Permalink
Replaces dependency on the tab-browser module
Browse files Browse the repository at this point in the history
This fixes #2 in the very simplest way – the tab-browser module wasn't very extensively used so the few use cases there was has been replaced with basically the same code that the tab-browser itself used to do what it did.
  • Loading branch information
voxpelli committed Feb 27, 2014
1 parent 08cf8f7 commit 1748a3f
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions lib/showforpage.js
Expand Up @@ -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) {
Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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);
};
Expand All @@ -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");
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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
};
Expand Down

0 comments on commit 1748a3f

Please sign in to comment.