Skip to content

Instantly share code, notes, and snippets.

@keelii
Created August 28, 2013 07:01
Show Gist options
  • Save keelii/6362930 to your computer and use it in GitHub Desktop.
Save keelii/6362930 to your computer and use it in GitHub Desktop.
Jshare
(function ($) {
/**
* Jshare插件
*/
var Jshare = function (that, options, callback) {
this.opts = $.extend({
title: '我在 @京东 发现了一个非常不错的商品',
url: location.href,
pic: 'http://misc.360buyimg.com/lib/img/e/logo-201305.png',
idPerfix: 'Jshare-',
item: [
{
title: '新浪微博',
id: 'weibo'
},{
title: '腾讯QQ',
id: 'qq'
},{
title: '腾讯微博',
id: 'tx_weibo'
},{
title: '人人网',
id: 'renren'
},{
title: '开心网',
id: 'kaixin'
},{
title: '豆瓣',
id: 'douban'
}
]
}, options);
this.$o = $(that);
this.callback = callback || function() {};
this.init();
};
Jshare.prototype = {
init: function() {
this.renderHTML();
},
renderHTML: function() {
var ul = $('<ul></ul>'),
perfix = this.opts.idPerfix;
$.each(this.opts.item, function(i, o, x) {
ul.append('<li><a id="'+ perfix + o.id +'" href="javascript:;">'+ o.title +'</a></li>');
});
this.$o.html(ul);
this.bindEvent();
},
bindEvent: function() {
var _this = this;
var perfix = this.opts.idPerfix;
this.$o.find('a').unbind('click').bind('click', function(e) {
var id = $(this).attr('id');
if ( new RegExp(perfix).test(id) ) {
_this.show( id.replace(perfix, '') );
}
});
},
show: function(id) {
console.log(id);
var url ='';
var param = {
title: this.opts.title,
text : this.opts.title,
url: this.opts.url,
pic: this.opts.pic
};
if ( id === 'weibo' ) {
param.appkey = 2445336821;
url = 'http://service.weibo.com/share/share.php?';
}
if ( id === 'tx_weibo' ) {
param = {
c: 'share',
a: 'index',
title: this.opts.title,
url: this.opts.url
};
url = 'http://share.v.t.qq.com/index.php?';
}
if ( id=== 'qq_zone') {
url = 'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?';
}
if ( id === 'qq' ) {
param.desc = this.opts.title;
url = 'http://connect.qq.com/widget/shareqq/index.html?';
}
if ( id == 'kaixin' ) {
// delete param.title;
// delete param.pic;
// param.rcontent = this.opts.title;
// param.rtitle;
url = 'http://www.kaixin001.com/repaste/share.php?';
}
if ( id === 'douban' ) {
url = 'http://shuo.douban.com/!service/share?';
}
window.open(url + $.param(param), 'height=500, width=600');
}
};
$.fn.Jshare = function (options, callback) {
return this.each(function () {
// 实例化插件对象
var plugin = new Jshare(this, options, callback);
$(this).data('Jshare', plugin);
});
};
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment