【问题标题】:listen to selected text drag event in chrome extension在 chrome 扩展中收听选定的文本拖动事件
【发布时间】:2014-06-23 05:29:07
【问题描述】:

我希望我的扩展在选择(突出显示)然后拖动某些文本时监听事件。 就像通过将 url 拖动到选项卡框来打开新选项卡一样。我已经看到了这个答案this 答案,但是当单击图标时它会突出显示文本,但是我希望我的某些函数 foo() 在选择并拖动文本时自动触发。谁能帮帮我。

【问题讨论】:

    标签: javascript google-chrome google-chrome-extension


    【解决方案1】:

    首先,您需要创建处理函数:

    function highlightHandler(e) {
        // get the highlighted text
        var text = document.getSelection();
        // check if anything is actually highlighted
        if(text !== '') {
            // we've got a highlight, now do your stuff here
            doStuff(text);
        }
    }
    

    然后,您需要将其绑定到您的文档:

    document.onmouseup = highlightHandler;
    

    最后,编写你的 doStuff 函数来做你想做的事:

    function doStuff(text) {
        // do something cool
    }
    

    【讨论】:

    • 简洁的代码和很好的注释,正是我想要的。
    猜你喜欢
    • 2012-01-12
    • 1970-01-01
    • 2013-11-30
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-10
    • 2020-11-05
    相关资源
    最近更新 更多