从上下文菜单中识别info.selectionText上的多行 [英] recognize multple lines on info.selectionText from Context Menu

查看:185
本文介绍了从上下文菜单中识别info.selectionText上的多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当用户在页面上选择一些文本时,我的扩展程序都会添加一个上下文菜单.

My extension adds a context menu whenever a user selects some text on the page.

然后,使用info.selectionText在用户从上下文菜单中选择一项时执行的功能上使用选定的文本. (来自 http://code.google.com/chrome/extensions/contextMenus.html)

Then, using info.selectionText, I use the selected text on a function executed whenever the user selects one of the items from my context menu. (from http://code.google.com/chrome/extensions/contextMenus.html)

到目前为止,一切正常.

So far, all works ok.

现在,我从一位扩展用户那里得到了这个很酷的请求,即每行所选文本执行一次相同的功能. 用户将选择3行文本,而我的函数将被调用3次,每行一次,并带有相应的文本行.

Now, I got this cool request from one of the extension users, to execute that same function once per line of the selected text. A user would select, for example, 3 lines of text, and my function would be called 3 times, once per line, with the corresponding line of text.

到目前为止,我还无法拆分info.selectionText以便识别每一行... info.selectionText返回一行文本,并且找不到拆分文本的方法.

I haven't been able to split the info.selectionText so far, in order to recognize each line... info.selectionText returns a single line of text, and could not find a way to split it.

有人知道是否有办法吗?有没有用于拆分的隐藏"字符?

Anyone knows if there's a way to do so? is there any "hidden" character to use for the split?

在此先感谢...如果您有兴趣,这是扩展名的链接

Thanks in advance... in case you're interested, here's the link to the extension

https://chrome.google.com/webstore/detail/aagminaekdpcfimcbhknlgjmpnnnmooo

推荐答案

好吧,因为OnClickData的selectionText永远都是文本,因此您将永远无法使用这种方法来做到这一点.

Ok, as OnClickData's selectionText is only ever going to be text you'll never be able to do it using this approach.

然后我要做的是将内容脚本注入到每个页面中,并使用类似于以下示例的内容(受此SO帖子的启发-

What I would do then is inject a content script into each page and use something similar to the below example (as inspired by reading this SO post - get selected text's html in div)

您仍然可以像现在一样使用上下文菜单OnClickData挂钩,但是当您收到它而不是读取selectionText时,可以使用事件通知来触发上下文脚本,而使用x.Selector.getSelected()来读取选择.那应该给你你想要的.使用上下文菜单后,文本将保留在扩展名中,因此阅读所选文本应该没有问题.

You could still use the context menu OnClickData hook like you do now but when you receive it instead of reading selectionText you use the event notification to then trigger your context script to read the selection using x.Selector.getSelected() instead. That should give you what you want. The text stays selected in your extension after using the context menu so you should have no problem reading the selected text.

if (!window.x) {
    x = {};
}

// https://stackoverflow.com/questions/5669448/get-selected-texts-html-in-div
x.Selector = {};
x.Selector.getSelected = function() {
    var html = "";
    if (typeof window.getSelection != "undefined") {
        var sel = window.getSelection();
        if (sel.rangeCount) {
            var container = document.createElement("div");
            for (var i = 0, len = sel.rangeCount; i < len; ++i) {
                container.appendChild(sel.getRangeAt(i).cloneContents());
            }
            html = container.innerHTML;
        }
    } else if (typeof document.selection != "undefined") {
        if (document.selection.type == "Text") {
            html = document.selection.createRange().htmlText;
        }
    }
    return html;
}

$(document).ready(function() {
    $(document).bind("mouseup", function() {
        var mytext = x.Selector.getSelected();
        alert(mytext);
        console.log(mytext);
    });
});​

http://jsfiddle.net/richhollis/vfBGJ/4/

另请参阅: Chrome扩展程序:如何捕获所选文本并发送到网络服务

http://jsfiddle.net/richhollis/vfBGJ/4/

See also: Chrome Extension: how to capture selected text and send to a web service

这篇关于从上下文菜单中识别info.selectionText上的多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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