share-selected-text
Version:
share selected text on twitter, buffer, stumbleupon, digg, tumblr & reddit. Inspired by medium.com
12 lines • 5.08 kB
JavaScript
/*!
* Share Selected Text
* version: 1.1.5
* license: MIT
* url: https://github.com/VincentLoy/share-selected-text
* author: Vincent Loy <vincent.loy1@gmail.com>
* contributors:
* - Wendy Beth <wendybeth010@gmail.com>
* - Dmitry Motorin <dmitry.mot@gmail.com>
* - Dustin Armstrong
*/
!function(t){var e=function(){return document.querySelector('meta[property="og:url"]')&&document.querySelector('meta[property="og:url"]').getAttribute("content")?document.querySelector('meta[property="og:url"]').getAttribute("content"):encodeURIComponent(window.location.href)},n=50,o=1.33,r=140,i=24,a=2,c=3,l=250,s={popup:"popup",page:"page"},u=r-i-a-c,d={twitter:"twitter",buffer:"buffer",digg:"digg",linkedin:"linkedin",stumbleupon:"stumbleupon",reddit:"reddit",tumblr:"tumblr",facebook:"facebook"},p=/[ .,!?\/\\\+\-=*£$€:~§%^µ)(|@"{}&#><_]/g,f=/[ ,\/\\\+\-=*£$€:~§%^µ)(|@"{}&#><_]/g,m=void 0,h=void 0,b={},g=function(t){t=t||{};for(var e=1;e<arguments.length;e+=1)if(arguments[e])for(var n in arguments[e])arguments[e].hasOwnProperty(n)&&(t[n]=arguments[e][n]);return t},w=function(){m.classList.remove("active")},y=function(){m.classList.add("active")},v=function(t){for(;t.length&&t[0].match(p);)t=t.substring(1,t.length);for(;t.length&&t[t.length-1].match(f);)t=t.substring(0,t.length-1);return t},x=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"",n="",o=u;return t?(h.twitterUsername&&e===d.twitter&&(n=" via @"+h.twitterUsername,o=u-n.length),t.length>u?(t=t.substring(0,o),t=t.substring(0,t.lastIndexOf(" "))+"..."):t=t.substring(0,o+c),v(t)):""},S=function(t,n){n=h.sanitize?x(n,t):v(n);var o="https://twitter.com/intent/tweet?url="+e()+'&text="'+n+'"';h.twitterUsername&&h.twitterUsername.length&&(o+="&via="+h.twitterUsername);var r="https://facebook.com/dialog/share?display="+h.facebookDisplayMode+"&href="+e()+""e="+n;if(document.querySelector('meta[property="fb:app_id"]')&&document.querySelector('meta[property="fb:app_id"]').getAttribute("content")){var i=document.querySelector('meta[property="fb:app_id"]');r+="&app_id="+i}else if(h.facebookAppID&&h.facebookAppID.length)r+="&app_id="+h.facebookAppID;else{var a=h.buttons.indexOf("facebook");a>-1&&h.buttons.splice(a,1)}var c={twitter:o,buffer:'https://buffer.com/add?text="'+n+'"&url='+e(),digg:"http://digg.com/submit?url="+e()+"&title="+n,linkedin:"https://www.linkedin.com/shareArticle?url="+e()+"&title="+n,stumbleupon:"http://www.stumbleupon.com/submit?url="+e()+"&title="+n,reddit:"https://reddit.com/submit?url="+e()+"&title="+n,tumblr:"https://www.tumblr.com/widgets/share/tool?canonicalUrl="+e()+"&caption="+n,facebook:r};return c.hasOwnProperty(t)?c[t]:""},k=function(t){var e=document.documentElement.scrollTop||document.body.scrollTop,r=document.querySelector("body");m.style.top=e+t.top-n*o+"px",m.style.left=t.left+t.width/2-r.getBoundingClientRect().width/2+"px",Array.prototype.forEach.call(h.buttons,function(t){m.querySelector(".share-selected-text-btn-"+t).href=S(t,b.text)}),window.setTimeout(function(){y()},h.tooltipTimeout)},A=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:null,n=document.createElement("A"),o=document.createElement("i");return h.anchorsClass?["share-selected-text-btn","share-selected-text-btn-"+t,""+h.anchorsClass].map(function(t){return n.classList.add(t)}):["share-selected-text-btn","share-selected-text-btn-"+t].map(function(t){return n.classList.add(t)}),e?o.classList.add(""+e):["icon-sst-"+t,"fa","fa-"+t].map(function(t){return o.classList.add(t)}),o.style.pointerEvents="none",n.addEventListener("click",function(t){t.preventDefault();var e="status=no,menubar=no,location=no,scrollbars=no,width=720,height=540",n=t.target.href;window.open(n,"Share this post",e)}),n.href=S(t,b.text?b.text:""),n.appendChild(o),n},C=function(){var t=document.querySelector("body"),e=document.createElement("DIV"),o=document.createElement("DIV");return e.classList.add("share-selected-text-main-container"),o.classList.add("share-selected-text-inner"),h.tooltipClass&&o.classList.add(h.tooltipClass),e.style.height=n+"px",e.style.top=0,e.style.left=0,Array.prototype.forEach.call(h.buttons,function(t){var e=A(t);o.appendChild(e)}),e.appendChild(o),t.appendChild(e),e},q=function(){var t="",e=void 0;return window.getSelection?(e=window.getSelection(),t=e.toString()):document.selection&&"Control"!==document.selection.type&&(e=document.selection.createRange(),t=e.text),{selection:e,text:t}},E=function(){if(b=q(),b.text.length){var t=b.selection.getRangeAt(0),e=t.getBoundingClientRect();k(e)}else w()};t.shareSelectedText=function(t,e){var n=document.querySelectorAll(t);h=g({tooltipClass:"",sanitize:!0,buttons:[d.twitter,d.buffer],anchorsClass:"",twitterUsername:"",facebookAppID:"",facebookDisplayMode:s.popup,tooltipTimeout:l},e),m=C(),Array.prototype.forEach.call(n,function(t){t.addEventListener("mouseup",function(){E()})})}}(window),window.jQuery&&!function(t,e){var n=function(t,n){e(t,n)};t.fn.shareSelectedText=function(t){return n(this.selector,t)}}(jQuery,shareSelectedText);
;