Kevin Powell - CommunityKP-C
Kevin Powell - Community2y ago
15 replies
empty

get Text and link (if exists) when users highlight text on screen.

Hi guys, I wonder that how can I get text and the link when users highlight it on screen? I got the text by this func but do not know how to get the link. Can anyone help me?
 function getSelectionText() {
        var text = "";
        if (window.getSelection) {
          text = window.getSelection().toString();
        } else if (
          document.selection &&
          document.selection.type !== "Control"
        ) {
          text = document.selection.createRange().text;
        }
        if (text) console.log("🚀 ~ getSelectionText ~ text:", text);
        return text;
      }
      document.onmouseup = function () {
        getSelectionText();
      };
image.png
Was this page helpful?