Firefox扩展:获取选定的文本 [英] Firefox Extension: Get selected text
本文介绍了Firefox扩展:获取选定的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在研究一个简单的Firefox扩展,我想获取选定的文本。我试过这个:
var WordCount = {
/ * ... * /
changeSelected:function (){
var selectedText = this.getSelection();
var words = this.countWords(selectedText);
this.changeStatus(单词,选);
// alert(selectedText);
},
getSelection:function(e){
var focused_window = document.commandDispatcher.focusedWindow;
var sel_text = focused_window.getSelection();
返回sel_text.toString();
$ b window.addEventListener(select,function(e){WordCount.changeSelected();},false);
问题是,我没有用 document.commandDispatcher.focusedWindow.getSelection (),我不知道为什么:($ / b>
解决方案
你的问题是 document.commandDispatcher.focusedWindow
将会指向一个chrome窗口,我怀疑你真的想要一个内容窗口,试着用 content.getSelection()来替换它。 c $ c>
I am working on a simple Firefox Extension and I want to get the selected text. I tried this:
var WordCount = {
/* ... */
changeSelected: function() {
var selectedText = this.getSelection();
var words = this.countWords(selectedText);
this.changeStatus(words, " selected");
//alert(selectedText);
},
getSelection: function(e) {
var focused_window = document.commandDispatcher.focusedWindow;
var sel_text = focused_window.getSelection();
return sel_text.toString();
}
}
window.addEventListener("select", function(e) { WordCount.changeSelected(); }, false);
The Problem is, that I dont get the selection with document.commandDispatcher.focusedWindow.getSelection() and I don't know why :(
解决方案
Your problem is that document.commandDispatcher.focusedWindow
is going to be pointing to a chrome window, where I suspect you actually want a content window. Try replacing that with content.getSelection()
这篇关于Firefox扩展:获取选定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文