JavaScript文本选择事件 [英] javascript text selection events

查看:126
本文介绍了JavaScript文本选择事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不仅适用于Google Chrome扩展,也适用于JavaScript 我正在写一个Chrome扩展,其中突出显示文本并显示一个上下文菜单,我在上下文菜单中显示我的项目,当点击时应该处理选定的文本



在调出上下文菜单并选择我的选项后,对象与所有值零和空值

所以我想实现一些机制,这将缓冲文本选择,只要用户在选择文本后释放鼠标,以便事件可以触发,如果有任何事情发生,我可以在全局变量中创建选定文本的副本,并可以稍后处理。

window.getSelected()正常工作,当我使用单独的测试代码进行测试,但在使用我的扩展名的情况下,弹出上下文菜单,我无法获得实际选定的文本。



所选文本如我在文档中看到的将是文字和html



建议请...




在这里,我粘贴了我正在做的事情。当我点击保存字提醒我收到一个空字符串




这里是代码的其余部分






 < script> 
chrome.extension.onRequest.addListener(function(request,sender,sendResponse){


switch(request.message)
{
case' getSelection':
sendResponse({data:window.getSelection()。toString()});
break;

case'createMenu':
seecon() ;
break;

默认值:
sendResponse({data:'无效参数'});
break;
}
}) ;

函数conOnClick(info,tab)
{
$ b $ * / $
chrome.extension.sendRequest(tab.id,{method:'getSelection' },function(response){
alert(response.data);
});
* /
}


//函数seecon()
{
var contexts = [selection];
for(var i = 0; i< contexts.length; i ++){
var context = contexts [i];
var title =保存到Word提醒;
var id = chrome.contextMenus.create({title:title,contexts:[context],
onclick:conOnClick});

}

}



< / script>


解决方案

我会简单地在文档上设置一个mouseUp事件,然后检查是否有任何选择(如果是,则选择是否与先前的选择不同)。


This is not just for google chrome extension but also for javascript

I am writing a chrome extension in which when a text is highlighted and a context menu is shown i display my item in the context menu which when clicked should process the selected text

after bringing up the context menu and selecting my option i get empty object with all values zeros and nulls

so i want to implement some mechanism which will buffer the text selection as soon as the user releases the mouse after selecting the text so that an event can fire and if any thing fires i can make a copy of the selected text in a global variable and can process later

window.getSelected() is working fine when i tested with a separate testing code but while using with my extension where i bring up the context menu i could not get the actual selected text

the selected text as i see in documentations would be of text and html

suggestions please...


Here i have pasted what i am doing. when i click save to word reminder i get an empty string

and here is the rest of the code


<script>
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {


    switch(request.message)
    {
        case 'getSelection':
            sendResponse({data: window.getSelection().toString()});
        break;

        case 'createMenu':
            seecon();
            break;

        default:
            sendResponse({data: 'Invalid arguments'});
        break;
    }
});

function conOnClick(info,tab)
{

/*
    chrome.extension.sendRequest(tab.id, {method: 'getSelection'}, function(response){
        alert(response.data);
    });
*/  
}


//function seecon()
{
var contexts = ["selection"];
for (var i = 0; i < contexts.length; i++) {
  var context = contexts[i];
  var title = "Save to Word Reminder";
  var id = chrome.contextMenus.create({"title": title, "contexts":[context],
                                       "onclick": conOnClick});

}

}



</script>

解决方案

I would simply set a mouseUp event on the document, and then check if there is any selection (and if yes, whether the selection is different from the previous selection).

这篇关于JavaScript文本选择事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆