|
(function($) { |
|
var supportedServices = { |
|
'weibo': 'http://service.weibo.com/share/share.php?title={{title}}&url={{url}}&pic={{pic}}', |
|
'douban': 'http://shuo.douban.com/!service/share?name={{title}}&href={{url}}&image={{pic}}', |
|
'kaixin': 'http://www.kaixin001.com/repaste/bshare.php?rtitle={{title}}&rurl={{url}}', |
|
'netease': 'http://t.163.com/article/user/checkLogin.do?info={{title}}', |
|
'qq_t': 'http://v.t.qq.com/share/share.php?title={{title}}&url={{url}}&pic={{pic}}', |
|
'qq_zone': 'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?title={{title}}&url={{url}}&pics={{pic}}', |
|
'renren': 'http://share.renren.com/share/buttonshare.do?title={{title}}&link={{url}}', |
|
'sohu': 'http://t.sohu.com/third/post.jsp?title={{title}}&url={{url}}&content=utf-8', |
|
|
|
'facebook': 'http://www.facebook.com/sharer.php?t={{title}}&u={{url}}', |
|
'twitter': 'https://twitter.com/intent/tweet?text={{title}}&url={{url}}' |
|
}; |
|
|
|
var uniformParams = ['service', 'title', 'url', 'pic']; |
|
|
|
function getData(el) { |
|
var data = {}; |
|
$.each(uniformParams,function(i, key){ |
|
var value = el.data(key); |
|
if (value){ |
|
data[key] = value; |
|
} |
|
}); |
|
return data; |
|
} |
|
|
|
function buildShareUrl(serviceName, option) { |
|
var service = supportedServices[serviceName]; |
|
if (!serviceName) { |
|
throw new Error('Should specify serviceName, such as weibo'); |
|
} |
|
if (!service) { |
|
throw new Error('"' + serviceName + '" do not exist'); |
|
} |
|
|
|
return service.replace(/{{(.*?)}}/g, function(a, m) { |
|
return option[m] ? encodeURIComponent(option[m]) : ''; |
|
}); |
|
} |
|
|
|
$.fn.share = function(options) { |
|
this.each(function(i,el){ |
|
el = $(el); |
|
var data = $.extend({}, (options || {}), getData(el)); |
|
el.click(function(){ |
|
window.open(buildShareUrl(data.service, data)); |
|
}); |
|
}); |
|
}; |
|
})($); |