getSelection不起作用

getSelection不起作用

本文介绍了如果从上下文菜单中调用,Chrome getSelection不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在开发Chrome扩展程序,但似乎无法通过前几行。 问题是,当我单击上下文菜单时,它无法从文档中获取选择内容。当我将它用作包含在HTML页面中的简单脚本时,它显然运行良好。 以下是代码: var id = chrome.contextMenus.create({title:Search,contexts:[selection],onclick:openUrl }); 函数openUrl(){ var sel = window.getSelection()。toString()。trim() alert(sel)} 此代码返回一个空的提示框。 在mouseup上抓取用户选择的单词并在字典中搜索该单词的脚本。这个脚本工作正常,我只需要在用户点击上下文菜单中的搜索时执行它。 所以我要找的是: 1)用户从文档中选择一个单词2)右键单击它并单击上下文菜单 3)脚本包含所有要执行的指令。 在问这个问题之前,我环顾四周,但我找不到任何东西,可能是因为我对这个真棒站点很新颖。如果我错过了任何问题,请随时将我重定向到其他类似的问题。 解决方案 function openUrl(info,tab){ alert( info.selectionText); } I'm working on a Chrome extension but I don't seem to be able to go past the first few lines.The problem is that when I click on the context menu it fails to grab the selection from the document. It works fine obviously when I use it as a simple script included in an HTML page.Here's the code:var id = chrome.contextMenus.create({ "title" : "Search", "contexts" : ["selection"], "onclick" : openUrl});function openUrl() { var sel = window.getSelection().toString().trim() alert(sel)}This code returns an empty alert box.I have a script that on mouseup grabs the word that the user has selected and searches for this word on a dictionary. This script works fine I just need to execute it when the user clicks "Search" in the context menu.So what I'm looking for is:1) User selects a word from the document2) Right clicks on it and clicks on the context menu3) The script containing all instruction to be executed on that click.I looked around before asking this question but I couldn't find anything probably because I'm pretty new to this awesome site. Please feel free to redirect me to other similiar question if I missed any. Thanks! 解决方案 function openUrl(info, tab) { alert(info.selectionText);} 这篇关于如果从上下文菜单中调用,Chrome getSelection不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-28 04:59