Skip to content

Instantly share code, notes, and snippets.

@japboy

japboy/socials.js

Last active Dec 17, 2015
Embed
What would you like to do?
SNS ボタン JavaScript コードまとめ。
/**
* Social button BFFs (modified)
*
* http://www.phpied.com/social-button-bffs/
* http://t32k.me/mol/log/social-button-bffs/
* http://www.aaronpeters.nl/blog/why-loading-third-party-scripts-async-is-not-good-enough
*
* Load this script before `</body>` like:
*
* <script src="./socials.js"></script>
*
*
* Facebook:
* https://developers.facebook.com/docs/reference/javascript/
*
* <div class="fb-like" data-send="false" data-layout="button_count" data-width="280"></div>
*
*
* Google +1:
* https://developers.google.com/+/web/+1button/
*
* <div class="g-plusone" data-size="small"></div>
*
*
* Twitter Buttons:
* https://twitter.com/about/resources/buttons
*
* <a href="https://twitter.com/share" class="twitter-share-button" data-lang="ja">Tweet</a>
*
*/
(function (w, d, s) {
'use strict';
function go () {
var js, fjs = d.getElementsByTagName(s)[0], load = function (url, id) {
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.src = url; js.id = id;
fjs.parentNode.insertBefore(js, fjs);
};
// Facebook
w.fbAsyncInit = function() {
// init the FB JS SDK
w.FB.init({
appId: process.env.FACEBOOK_APP_ID,
channelUrl: process.env.FACEBOOK_CHANNEL_URL,
xfbml: true,
version: 'v2.2'
});
// Additional initialization code such as adding Event Listeners goes here
// Facebook customised Share dialog
// https://developers.facebook.com/docs/sharing/reference/share-dialog
w.fbShareDialog = function (options, callback) {
w.FB.ui({
method: 'feed',
name: options.name,
caption: options.caption,
description: options.description,
link: options.link,
picture: options.picture
}, function (resp) {
if (resp && resp.post_id) return callback(null, resp.post_id);
var err = new Error('No posting to Facebook.');
callback(err);
});
};
// Twitter customised Share dialog
// https://dev.twitter.com/web/intents
w.twShareDialog = function (options) {
w.open(
'https://twitter.com/intent/tweet?' +
'lang=' + process.env.LANG +
'&text=' + w.encodeURIComponent(options.text) +
'&url=' + w.encodeURIComponent(options.url) +
'&hashtags=' + options.hashtags +
'&via=' + options.via +
'&related=' + w.encodeURIComponent(options.related) +
'&in-reply-to=' + options.inReplyTo,
'',
'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=420,width=550'
);
};
};
// Google +1
w.___gcfg = {lang: process.env.LANG};
load('//connect.facebook.net/' + process.env.LANG + '_' + process.env.REGION + '/sdk.js', 'fbjssdk');
load('https://apis.google.com/js/plusone.js', 'gplus1js');
load('//platform.twitter.com/widgets.js', 'tweetjs');
}
if (w.addEventListener) { w.addEventListener('load', go, false); }
else if (w.attachEvent) { w.attachEvent('onload', go); }
})(window, document, 'script');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment