Last active
July 21, 2021 12:40
-
-
Save AnweshGangula/998376def9e30fd0233cae695b4bc258 to your computer and use it in GitHub Desktop.
Bookmarklet to share URL of selected text in webpage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
selection=document.getSelection().toString().replaceAll("\n","%0A").replaceAll(" ","%20"),thisURL=window.location.href,newURL=`${thisURL}#:~:text=${selection}`,window.location.href=newURL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Human Readable Function | |
(function () { | |
function getSelectedText() { | |
var txt = ''; | |
if (window.getSelection) { | |
txt = window.getSelection(); | |
} | |
else if (document.getSelection) { | |
txt = document.getSelection(); | |
} | |
else if (document.selection) { | |
txt = document.selection.createRange().text; | |
} | |
else return; | |
return txt; | |
} | |
selection = getSelectedText().toString().replaceAll("\n","%0A").replaceAll(" ", "%20"); | |
thisURL = window.location.href; | |
newURL = `${thisURL}#:~:text=${selection}`; | |
// console.log(newURL); | |
window.location.href = newURL | |
}()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment