Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Noitidart/9299123 to your computer and use it in GitHub Desktop.

Select an option

Save Noitidart/9299123 to your computer and use it in GitHub Desktop.
_ff-addon-snippet-OpenNewTabGetIframeHtml - This snippet must run in privelaged scope. It opens a new tab and messages you when it loads, and it looks for all iframes in that new tab and messages you when iframes contentDocument is ready and populated. I do need a better way to test if iframe is loading/loaded on initial run of the getIframeHtml…
Components.utils.import('resource://gre/modules/Services.jsm')
var aDOMWindow = Services.wm.getMostRecentWindow('navigator:browser');
var newTabBrowser = aDOMWindow.gBrowser.getBrowserForTab(aDOMWindow.gBrowser.loadOneTab('http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_tolocaletimestring', {
inBackground: false
}));
newTabBrowser.addEventListener('load', function onloadFunc() {
Services.appShell.hiddenDOMWindow.console.log('tab loaded');
newTabBrowser.removeEventListener('load', arguments.callee, true);
var doc = newTabBrowser.contentDocument;
var win = doc.defaultView;
var iframes = doc.querySelectorAll('iframe');
Services.appShell.hiddenDOMWindow.console.log('number of iframes in this tab = ' + iframes.length);
[].forEach.call(iframes, function (frm, i) {
logIframeHtml(frm, i);
});
}, true);
function logIframeHtml(iframe, i) {
iframe.removeEventListener('DOMContentLoaded', logIframeHtml, true); //this line removes the event listneer if it had one
var iframeDoc = iframe.contentDocument;
if (!iframeDoc) {
Services.appShell.hiddenDOMWindow.console.log('iframe ' + i + ' is not fully loaded yet as it its contentDocument does not exist');
iframe.addEventListener('DOMContentLoaded', logIframeHtml, true);
} else {
var iframeInnerHtml = iframeDoc.documentElement.innerHTML;
Services.appShell.hiddenDOMWindow.console.log('iframe ' + i + ' innerHTML = ' + iframeInnerHtml);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment