Skip to content

Instantly share code, notes, and snippets.

@nishinoshake
Last active June 1, 2016 06:10
Show Gist options
  • Save nishinoshake/7be8631e24e698c631d8 to your computer and use it in GitHub Desktop.
Save nishinoshake/7be8631e24e698c631d8 to your computer and use it in GitHub Desktop.
jQueryでFacebook,Twitter,LINEのシェア設定
/**
* == SNSボタンでシェア ==
* @constructor
*/
Index.Social = function() {
var elm = {
$twitter : $('.twitter a'),
$facebook: $('.facebook a'),
$line : $('.line a'),
$hatena : $('.hatena a')
},
url = {
'share' : getShareUrl(), // or root url
'twitter' : 'https://twitter.com/intent/tweet?url=',
'facebook' : 'http://www.facebook.com/sharer.php?u=',
'line' : 'http://line.me/R/msg/text/',
'hatena' : 'http://b.hatena.ne.jp/add?mode=confirm&url='
},
txt = '',
hashtags = 'xxx,yyy',
title = '',
size = {
'width' : 520,
'height' : 350
},
center = {
top : (screen.height - size.height) / 2,
left: (screen.width - size.width) / 2
};
var twitter = (function() {
var href = url.twitter;
href += encodeURIComponent(url.share);
href += '&text=' + encodeURIComponent(txt);
href += '&hashtags=' + encodeURIComponent(hashtags);
console.log(href);
elm.$twitter.attr('href', href).on('click', function() {
openExternal(href, 'twitterShare');
return false;
});
})();
var facebook = (function() {
var href = url.facebook + encodeURIComponent(url.share);
elm.$facebook.attr('href', href).on('click', function() {
openExternal(href, 'facebookShare');
return false;
});
})();
var line = (function() {
var href = url.line + encodeURIComponent(url.share);
elm.$line.attr('href', href);
})();
var hatena = (function() {
var href = url.hatena + encodeURIComponent(url.share);
href += '&title=' + title;
elm.$hatena.attr('href', href).on('click', function() {
openExternal(this.href, 'hatenaBookmark');
return false;
});
})();
function getShareUrl() {
var url = location.href;
$('meta').each(function() {
if ( $(this).attr('property') === 'og:url' && $(this).attr('content') ) {
url = $(this).attr('content');
}
});
return url;
}
function openExternal(href, title) {
window.open(
href, title,
'top=' + center.top + ',left=' + center.left + ',toolbar=0,menubar=0,status=0,width=' + size.width + ',height=' + size.height
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment