本文介绍了Firefox扩展:获取选定的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在研究一个简单的Firefox扩展,我想获取选定的文本。我试过这个:
你的问题是 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扩展:获取选定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!