如何从firefox插件获取显示页面的iframe内容?

时间:2014-03-01 01:26:51

标签: javascript firefox iframe firefox-addon

例如,我在firefox中打开了以下页面:

<html>
   <head></head>
   <body> text
       <iframe id="wikipedia" src="http://en.wikipedia.org"></iframe>
   </body>
</html>

如何从我的插件中获取iframe中显示的维基百科文本?

...
Welcome to Wikipedia,
the free encyclopedia that anyone can edit.
4,462,480 articles in English
...

2 个答案:

答案 0 :(得分:0)

此代码在此处打开最新导航器中的新标签:浏览器窗口,在加载时会查找iframe并获取其内容/

这个解决方案更好,因为如果iframe正在加载,我测试是否存在iframe.contentDocument是否存在,这不是最准确的测试方法,我现在想不出另一个。 如果其他人读到此内容请分享一个更好的方法来查看iframe是否已加载

但如果iframe正在加载,则会等待iframe加载,而不是将其登录到浏览器控制台的iframe的html

复制粘贴到暂存器,确保环境设置为“浏览器”,这是经过测试和工作

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);
    }
}

答案 1 :(得分:0)

最新导航器中当前加载的标签的解决方案:浏览器和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);
    }
}