在iframe中访问

时间:2011-04-24 13:13:18

标签: javascript firefox linkify

我正在使用一小段linkify派生代码来访问Firefox扩展程序中网页中的所有文本inode。这看起来像这样,所以没什么特别有趣的:

var notInTags=[
    'a', 'head', 'noscript', 'option', 'script', 'style', 'title', 'textarea'
    ];

    var xpath = ".//text()[not(ancestor::"+notInTags.join(') and not(ancestor::')+")]";

    var candidates = window.content.document.evaluate(xpath, window.content.document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

有没有人有关于从任何iframe中获取文本节点的最佳方法的建议呢? 具体来说,正在使用

iframe.window.content.document

可能会把我带到任何地方(似乎没有),或者我是在咆哮错误的树?

干杯:)

编辑2:这是完整的更新功能(再次)

    rsfindmod.searchiframes= function(candidates){
//This fixes cases where a redirecting page uses frames (Primarily search engines etc)
    const urlRegex = /\b(https?:\/\/[^\s+\"\<\>]+)/ig;
    var framesets = window.content.document.getElementsByTagName('frame','iframe','frameset');

    for (var i = 0; i < framesets.length; i++) {
    if (urlRegex.test(framesets[i])) {
    alert('test');
    var document2 = framesets[i].contentDocument;
    var notInTags=[
    'a', 'head', 'noscript', 'option', 'script', 'style', 'title', 'textarea'
    ];

    var xpath = ".//text()[not(ancestor::"+notInTags.join(') and not(ancestor::')+")]";

    var textnodes = document2.evaluate(xpath, document2, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
    for(var i = 0; i < textnodes.length; i++) {
        candidates.push(textnodes[i]);
    }
}   
}
}

编辑3:功能稍好一点?

rsfindmod.searchiframes= function(candidates, frame, documentList){
//This fixes cases where a redirecting page uses frames (Primarily search engines etc)
    const framesets = frame.frames;

    for (var i = 0; i < framesets.length; i++) {
    var document2 = framesets[i].contentWindow.document;
    var notInTags=[
    'a', 'head', 'noscript', 'option', 'script', 'style', 'title', 'textarea'
    ];
    alert('test');
    var xpath = ".//text()[not(ancestor::"+notInTags.join(') and not(ancestor::')+")]";

    var textnodes = document2.evaluate(xpath, document2, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
    for(var i = 0; i < textnodes.length; i++) {
        candidates.push(textnodes[i]);
        alert('test1'); 
    }

}
}

1 个答案:

答案 0 :(得分:1)

您想要iframe.contentDocument(或iframe.contentWindow.document,但前者更简单。)