Skip to content

Instantly share code, notes, and snippets.

@AnweshGangula
Last active July 21, 2021 12:40
Show Gist options
  • Save AnweshGangula/998376def9e30fd0233cae695b4bc258 to your computer and use it in GitHub Desktop.
Save AnweshGangula/998376def9e30fd0233cae695b4bc258 to your computer and use it in GitHub Desktop.
Bookmarklet to share URL of selected text in webpage
javascript:(selection=document.getSelection().toString().replaceAll("\n","%0A").replaceAll(" ","%20"),thisURL=window.location.href,newURL=`${thisURL}#:~:text=${selection}`,window.location.href=newURL;)
selection=document.getSelection().toString().replaceAll("\n","%0A").replaceAll(" ","%20"),thisURL=window.location.href,newURL=`${thisURL}#:~:text=${selection}`,window.location.href=newURL;
// 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