在最近的導航只是當前加載的標籤解決方案:瀏覽器和iframe已經加載
Components.utils.import('resource://gre/modules/Services.jsm')
var aDOMWindow = Services.wm.getMostRecentWindow('navigator:browser');
var iframes = aDOMWindow.gBrowser.contentDocument.querySelectorAll('iframe');
Services.appShell.hiddenDOMWindow.console.log('number of iframes in this tab = ' + iframes.length);
[].forEach.call(iframes, function (frm, i) {
var iframeInnerHtml = frm.contentDocument.documentElement.innerHTML;
});
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);
}
}
這裏該解決方案是當前選項卡,但如果iframe中加載不信任:
Components.utils.import('resource://gre/modules/Services.jsm')
var aDOMWindow = Services.wm.getMostRecentWindow('navigator:browser');
var iframes = aDOMWindow.gBrowser.contentDocument.querySelectorAll('iframe');
Services.appShell.hiddenDOMWindow.console.log('number of iframes in this tab = ' + iframes.length);
[].forEach.call(iframes, function (frm, i) {
logIframeHtml(frm, i);
});
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);
}
}
我不知道爲什麼人們失望投你這一點。生病給你下面的解決方案 – Noitidart
我不在乎投票 - 很需要解決。 –