如何在包含 iframe 或仅包含框架的 HTML 文档中查找选择
如果文本可能位于 HTML 文档的某个框架(或 iframe)内,是否有办法在 HTML 文档中查找所选文本?
如果文档没有框架,那就很简单:
var text;
if (document.getSelection) {
// Firefox and friends
text = document.getSelection();
} else if (document.selection) {
// IE
text = document.selection.createRange();
}
if (text == undefined || text == '') {
// Iterate over all textarea elements and see if one of them has selection
var areas = document.getElementsByTagName('textarea');
for(var i = 0; i = areas.length; i++) {
if(areas[i].selectionStart != undefined &&
areas[i].selectionStart != areas[i].selectionEnd){
text = areas[i].value.substring(areas[i].selectionStart, a[i].selectionEnd);
break;
}
}
}
// Now if document has selected text, it's in text
所以这可以跨浏览器工作(尽管不是很漂亮)。
问题是当文档包含框架或 iframe 时。 框架有自己的文档,因此仅使用上面的代码是不够的。 人们可能会迭代框架树并在其中之一中搜索选定的文本,但是一般来说框架可以包含来自不同域的内容,因此即使我要迭代搜索中根文档的所有框架和所有子框架等所选文本的我将无权访问其 HTML,对吧? 所以我无法获得他们选择的文本。
即使页面包含框架,是否有一种(简单)可靠的方法可以在网页上查找所选文本?
谢谢
Is there a way to find the selected text in an HTML document if the text may be within one of its frames (or iframes)?
If the document has no frames it's simple:
var text;
if (document.getSelection) {
// Firefox and friends
text = document.getSelection();
} else if (document.selection) {
// IE
text = document.selection.createRange();
}
if (text == undefined || text == '') {
// Iterate over all textarea elements and see if one of them has selection
var areas = document.getElementsByTagName('textarea');
for(var i = 0; i = areas.length; i++) {
if(areas[i].selectionStart != undefined &&
areas[i].selectionStart != areas[i].selectionEnd){
text = areas[i].value.substring(areas[i].selectionStart, a[i].selectionEnd);
break;
}
}
}
// Now if document has selected text, it's in text
So this works cross-browser (although not very pretty).
The problem is when the document contains frames or iframes.
Frames have their own document, so just using the code above is not enough.
One could potentially iterate over the frames tree and search for selected text in one of them, however in general frames can have content from different domains, so even if I were to iterate over all frames and over all subframes etc of the root document in search of selected text I would not have had the permission to access their HTML, right? So I wouldn't be able to get their selected text.
Is there a (simple) reliable way of finding the selected text on a web page even if the page contains frames?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
经过更多调查后回答我自己的问题:
因此,如果框架属于不同的域,那么您无能为力,因为您没有访问它们的 dom 的权限。 然而,在所有框架都位于同一域(例如 gmail)的常见情况下,只需像树一样迭代主题即可。 以下是完成此操作的代码:
下面的代码用于计算所选文本的字符和单词数的书签:
To answer my own question, after a bit more investigation:
So, if frames are of different domains then there's nothing you can do about it since you have no permission accessing their dom. However, in the common case where all frames are on the same domain (e.g. gmail) just iterate theme all like a tree. Here's the code that accomplishes that:
The code below is for a bookmarklet that counts chars and words of the selected text:
@Ran 对你自己的问题的回答很好。 但是,如果 iframe 文档未定义,则该函数将失败。 我添加了一个条件来检查这一点,现在它适用于我尝试过的每个网站,包括 Gmail。
if ((!t || t == '') && d)
再次感谢您提供的精彩代码。@Ran Excellent answer to your own question. However, if the iframe document is undefined then the function fails. I added a conditional to check for this, now it works on every site I've tried including Gmail.
if ((!t || t == '') && d)
Thanks again for the great code.