gpt4 book ai didi

javascript - 在文本高亮事件中?

转载 作者:IT王子 更新时间:2023-10-29 02:55:51 25 4
gpt4 key购买 nike

我很好奇是否有人知道如果/一旦用户完成选择网页上的文本我将如何触发函数运行?我希望用户能够选择文本,并在短暂的延迟后(或立即,此时无关紧要)一个覆盖按钮出现在用户可以单击的文本附近,然后我返回并运行更多基于选择的代码。这是一个 Firefox 扩展。

我能想到的一个类似的例子是在 IE 中,您可以在其中选择文本,然后它会显示“网络加速器”。我 99% 确定我知道如何实际覆盖按钮,并获取所选文本的位置,但我不知道如何检查是否有任何选择,而不做某种无限循环,这这似乎是个糟糕的主意。

编辑:

//In my overlay.js with the rest of my sidebar code
isTextSelected: function () {
var myText = cqsearch.getSelectedText();
var sidebar = document.getElementById("sidebar");
var sidebarDoc = sidebar.contentDocument || document;

var curHighlightedDiv = sidebarDoc.getElementById("testDiv");
curHighlightedDiv.innerHTML = "Current text selection:" + myText;
}
};

//In my on firefox load function I added this
document.onmouseup = cqsearch.isTextSelected;

这就是我根据 Robert 的建议得出的结果,我花了一些时间才将所有内容都放在正确的位置,但效果很好!现在定位我的按钮。

最佳答案

没有任何 onhighlightext 或类似的东西,但解决方案是绑定(bind) onmouseup 以检查是否选择了任何文本(如果它不在输入/文本区域

编辑

这是为您提供的实现示例。我只在 Chrome/Firefox/IE7 中测试过这个。这也适用于输入。

http://jsfiddle.net/qY7gE/

代码来自 JSFiddle :

var t = '';
function gText(e) {
t = (document.all) ? document.selection.createRange().text : document.getSelection();

document.getElementById('input').value = t;
}

document.onmouseup = gText;
if (!document.all) document.captureEvents(Event.MOUSEUP);
<input type='text' id='input' />
In software, a stack overflow occurs when too much memory is used on the call stack. The call stack contains a limited amount of memory, often determined at the start of the program. The size of the call stack depends on many factors, including the programming language, machine architecture, multi-threading, and amount of available memory. When too much memory is used on the call stack the stack is said to overflow, typically resulting in a program crash.[1] This class of software bug is usually caused by one of two types of programming errors.[2]

关于javascript - 在文本高亮事件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3731328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com