Search

8/04/2009

getSelectedText

http://www.bloglines.com/js/r200702160/prototype/link_search.js


_getSelectedText: function(limit) {
var q = '';
var pos = [0, 0];
var limit = limit || 200;
var selectionRefNode = null;
try {
if(window.getSelection) {
var sel = window.getSelection();
if (sel) {
var r = null;
q = sel.toString();
if (!q) {
return {text:'', pos:null};
} else {
r = sel.getRangeAt(0).cloneRange();
}
r.collapse(false);
selectionRefNode = sel.anchorNode;
var newNode = document.createElement('span');
newNode.style.position = 'absolute';
newNode.style.display = 'inline';
newNode.style.visibility = 'hidden';
r.insertNode(newNode);
pos = Position.cumulativeOffset(newNode);
Element.extend(newNode).remove();
r.detach();
}
} else if(document.selection) {
var r = document.selection.createRange();
q = r.text;
if (q) {
selectionRefNode = r.parentElement();
pos = Position.cumulativeOffset(r);
pos[0] += document.documentElement.scrollLeft;
pos[1] += document.documentElement.scrollTop;
pos[0] += r.boundingWidth;
// adjust popup mark position
pos[1] -= 02;
}
} else {
q = '';
}
q = q.replace(/((^\s*)|([\n\t\r]+)|(\s*$))/g,'');
if(q.replace(/[^\x00-\xff]/g,"**").length > limit){
q = '';
}
} catch(e) {
alert(e);
q = '';
}
var blogIds = null;
if (selectionRefNode) {
blogIds = this._getBlogIdsBySelectionNode(selectionRefNode);
}
// adjust popup mark position
pos[0] -= 19;
pos[1] -= 20;
return {text:q, pos:pos, ids:blogIds};
},

沒有留言: