突出显示文本时获得光标的位置

发布于 2025-02-09 00:24:40 字数 1109 浏览 1 评论 0 原文

我需要创建一个人们可以用来查找单词的 chrome扩展名。而且我发现我使用双击方法,例如当我想找到一个单词的含义时,我只需双击,然后出现通知。 (下图是我的chrome扩展。)

”在此处输入图像说明”

  • 但是,我发现用户很难找到Pharsal的含义 动词,搭配和句子。
  • 所以想出一个想法,我可以在 用户选择文本,以及本文的最后一封信,我得到 位置。

”在此处输入图像描述”

“在此处输入映像”

I need to create a Chrome Extension which people can use to look up words. And I have found that I use double-click method like when I want to find meaning of a word I just double click and then the notification will appear. (The image below is my Chrome Extension.)

enter image description here.

  • However, I find it is difficult for user to find meaning of pharsal
    verb, collocation and sentence.
  • So come up with an idea that I can get the position of cursor when
    user select text and to the last letter of this text I get the
    position.

enter image description here

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情深如许 2025-02-16 00:24:40

要获取选择的文本,您可以使用此功能,然后删除点,以及您不感兴趣的其他字符,然后按空格将所选文本拆分以获取最后一个单词。 (也不需要光标坐标,因此您可以删除该件。

  let hasSelection = false;
  function showCoordsForMouseUp(event) {
      const selection = window.getSelection().toString()
      hasSelection = selection.length;
      if (hasSelection) {
          const x = event.clientX;
          const y = event.clientY;
          const coords = `X coords: ${x} , Y coords: ${y}`;
          console.log(`${coords} - ${selection}`)
      } else {
          console.log('No text selected')
      }
  }

  document.onmouseup = showCoordsForMouseUp;

To get the text selected you could use this function, then remove the dots, and other characters you are not interested in and split the selected text by spaces to get the last word. (also you don't need the cursor coordinates for it, so you could remove this piece. of the code)

  let hasSelection = false;
  function showCoordsForMouseUp(event) {
      const selection = window.getSelection().toString()
      hasSelection = selection.length;
      if (hasSelection) {
          const x = event.clientX;
          const y = event.clientY;
          const coords = `X coords: ${x} , Y coords: ${y}`;
          console.log(`${coords} - ${selection}`)
      } else {
          console.log('No text selected')
      }
  }

  document.onmouseup = showCoordsForMouseUp;
梦魇绽荼蘼 2025-02-16 00:24:40

感谢您的贡献,我在Mouseup活动中找到了解决方案。

document.onmouseup = function F(e) {
  x = e.pageX;
  y = e.pageY;
  if (window.getSelection().toString() != "") {
    console.log("X: " + x + "Y: " + y);
  }
}
<p>
  Flower, flowers.
</p>

Thanks for your contribution, I have found the solution with onMouseUp Event.

document.onmouseup = function F(e) {
  x = e.pageX;
  y = e.pageY;
  if (window.getSelection().toString() != "") {
    console.log("X: " + x + "Y: " + y);
  }
}
<p>
  Flower, flowers.
</p>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文