Skip to content

Instantly share code, notes, and snippets.

@mcwhittemore
Last active December 22, 2020 19:32
Show Gist options
  • Save mcwhittemore/4431121 to your computer and use it in GitHub Desktop.
Save mcwhittemore/4431121 to your computer and use it in GitHub Desktop.
Bookmarklet to let you post highlighted text to twitter. Slightly moded version of http://jsfiddle.net/dKaJ3/2/. Encoded via http://chris.zarate.org/bookmarkleter.

How to Install

Chrome

  1. Right click on bookmark bar (ctrl+shirt+b to disply if hidden)
  2. Click "Add Page..."
  3. Change Name to "Tweet Highlight"
  4. Change URL to the string of text found in TweetHighlight.bm
  5. Click Save
function getSelectionHtml() {
var html = "";
if (typeof window.getSelection != "undefined") {
var sel = window.getSelection();
if (sel.rangeCount) {
var container = document.createElement("div");
for (var i = 0, len = sel.rangeCount; i < len; ++i) {
container.appendChild(sel.getRangeAt(i).cloneContents());
}
html = container.innerHTML;
}
} else if (typeof document.selection != "undefined") {
if (document.selection.type == "Text") {
html = document.selection.createRange().htmlText;
}
}
var url='https://twitter.com/intent/tweet%3Fsource=highlight%26text='+html+' '+window.location.href;
window.open(url,'_blank');
}
getSelectionHtml();
javascript:(function(){function%20getSelectionHtml(){var%20html=%22%22;if(typeof%20window.getSelection%20!=%22undefined%22){var%20sel=window.getSelection();if(sel.rangeCount){var%20container=document.createElement(%22div%22);for(var%20i=0,len=sel.rangeCount;i%20%3C%20len;++i){container.appendChild(sel.getRangeAt(i).cloneContents());}html=container.innerHTML;}}else%20if(typeof%20document.selection%20!=%22undefined%22){if(document.selection.type==%22Text%22){html=document.selection.createRange().htmlText;}}var%20url='https://twitter.com/intent/tweet%3Fsource=highlight%26text='+html+'%20'+window.location.href;window.open(url,'_blank');}getSelectionHtml();})();
@spaceclottey
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment