Access within iframes
I'm using a short snippet of linkify derived code to access all the text inodes within a webpage from a Firefox extension. This looks like this, so nothing particularly interesting:
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);
Does anyone have a suggestion on the best way to grab the text nodes from any iframes present as well pl开发者_如何学编程ease? Specifically, is using
iframe.window.content.document
likely to get me anywhere (Doesn't seem to), or am I am I barking up the wrong tree?
Cheers :)
Edit 2: This is the complete updated function (again)
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]);
}
}
}
}
Edit 3: A slightly better function?
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');
}
}
}
You want iframe.contentDocument
(or iframe.contentWindow.document
, but the former is simpler).
精彩评论