2009-04-30 38 views
4

有没有办法找到一个HTML文档中选定的文本,如果文本可能是其帧(或iFrame)中的一个内HTML文档中选择?如何找到包含iframe或只是框架

如果文档没有框架很简单:

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 

所以此工程跨浏览器(虽然不是很漂亮)。

问题是当文档包含框架或iframes。 框架有自己的文档,所以只使用上面的代码是不够的。 可能会对帧树进行迭代,并在其中一个帧中搜索选定的文本,但是一般帧可以包含来自不同域的内容,因此即使我要遍历根文档中的所有帧和所有子帧等,搜索选定的文本我不会有权限访问他们的HTML,对吧?所以我无法获得他们选定的文本。

有没有发现,即使页面包含框架的网页上所选文本的(简单的)可靠的方式?

感谢

回答

3

要回答我的问题,多一点调查后: 所以,如果帧不同域的再没有什么可以做,因为你没有权限访问他们的DOM。但是,在所有框架都在同一个域上(例如gmail)的通常情况下,只是像树一样迭代主题。下面是实现的代码:

下面的代码是用于计数所选文本的字符和字的书签:

javascript:(function(){ 
    // Function: finds selected text on document d. 
    // @return the selected text or null 
    function f(d){ 
    var t; 
    if (d.getSelection) t = d.getSelection(); 
    else if(d.selection) t = d.selection.createRange(); 
    if (t.text != undefined) t = t.text; 
    if (!t || t=='') { 
     var a = d.getElementsByTagName('textarea'); 
     for (var i = 0; i < a.length; ++i) { 
     if (a[i].selectionStart != undefined && a[i].selectionStart != a[i].selectionEnd) { 
      t = a[i].value.substring(a[i].selectionStart, a[i].selectionEnd); 
      break; 
     } 
     } 
    } 
    return t; 
    }; 
    // Function: finds selected text in document d and frames and subframes of d 
    // @return the selected text or null 
    function g(d){ 
    var t; 
    try{t = f(d);}catch(e){}; 
    if (!t || t == '') { 
     var fs = d.getElementsByTagName('frame'); 
     for (var i = 0; i < fs.length; ++i){ 
     t = g(fs[i].contentDocument); 
     if(t && t.toString() != '') break; 
     } 
     if (!t || t.toString() == '') { 
     fs = d.getElementsByTagName('iframe'); 
     for (var i = 0; i < fs.length; ++i){ 
      t = g(fs[i].contentDocument); 
      if(t && t.toString() != '') break; 
     } 
     } 
    } 
    return t; 
    }; 
    var t= g(document); 
    if (!t || t == '') alert('please select some text'); 
    else alert('Chars: '+t.toString().length+'\nWords: '+t.toString().match(/(\S+)/g).length); 
})() 
0

@Ran优秀的答案,你自己的问题。但是,如果iframe文档未定义,则该功能失败。我添加了一个条件来检查这一点,现在它适用于我尝试过的每个网站,包括Gmail。 if ((!t || t == '') && d)再次感谢伟大的代码。

var getSelectedText = function(){ 
    // Function: finds selected text on document d. 
    // @return the selected text or null 
    function f(d){ 
    var t; 
    if (d.getSelection) t = d.getSelection(); 
    else if(d.selection) t = d.selection.createRange(); 
    if (t.text != undefined) t = t.text; 
    if (!t || t=='') { 
     var a = d.getElementsByTagName('textarea'); 
     for (var i = 0; i < a.length; ++i) { 
     if (a[i].selectionStart != undefined && a[i].selectionStart != a[i].selectionEnd) { 
      t = a[i].value.substring(a[i].selectionStart, a[i].selectionEnd); 
      break; 
     } 
     } 
    } 
    return t; 
    }; 
    // Function: finds selected text in document d and frames and subframes of d 
    // @return the selected text or null 
    function g(d){ 
    var t; 
    try{t = f(d);}catch(e){console.log('ERROR: ',e);}; 
    if ((!t || t == '') && d){ 
     var fs = d.getElementsByTagName('frame'); 
     for (var i = 0; i < fs.length; ++i){ 
     t = g(fs[i].contentDocument); 
     if(t && t.toString() != '') break; 
     } 
     if (!t || t.toString() == '') { 
     fs = d.getElementsByTagName('iframe'); 
     for (var i = 0; i < fs.length; ++i){ 
      t = g(fs[i].contentDocument); 
      if(t && t.toString() != '') break; 
     } 
     } 
    } 
    return t; 
    }; 
    var t= g(document); 
    if (!t || t == '') ; 
    else return t.toString(); 
}