Skip to content

Instantly share code, notes, and snippets.

@toctan
Created June 22, 2014 01:13
Show Gist options
  • Save toctan/c464373d6068dd7573ad to your computer and use it in GitHub Desktop.
Save toctan/c464373d6068dd7573ad to your computer and use it in GitHub Desktop.
$.fn.share = (opts = {}) ->
@each ->
$this = $(@)
$this.on 'click', (event) ->
event.preventDefault()
$offsetParent = $this.offsetParent()
url = encodeURIComponent(
if opts.url
opts.url
else if $offsetParent.attr('id')
"#{window.location.href}##{$offsetParent.attr('id')}"
else
window.location.href
)
text = encodeURIComponent(opts.text || $offsetParent.find($this.
data('text-selector')).text()).slice(0, 140)
paths =
twitter: "http://twitter.com/intent/tweet?url=#{url}&text=#{text}"
weibo: "http://service.weibo.com/share/share.php?url=#{url}&title=#{text}"
link = paths[$this.data('network')]
window.open link, 'targetWindow', 'toolbar=no,location=no,status=no,
menubar=no,scrollbars=yes,resizable=yes,width=500,height=350'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment