buttons.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. require(['gitbook', 'jquery'], function(gitbook, $) {
  2. function site(label, icon, link) {
  3. return {
  4. label: label,
  5. icon: 'fa fa-' + icon,
  6. onClick: function (e) {
  7. e.preventDefault();
  8. window.open(link);
  9. }
  10. };
  11. }
  12. var url = encodeURIComponent(location.href);
  13. var title = encodeURIComponent(document.title);
  14. var SITES = {
  15. douban: site('豆瓣', 'share', 'http://shuo.douban.com/!service/share?href=' + url + '&name=' + title),
  16. facebook: site('Facebook', 'facebook', 'http://www.facebook.com/sharer/sharer.php?s=100&p[url]=' + url),
  17. google: site('Google+', 'google-plus', 'https://plus.google.com/share?url=' + url),
  18. hatenaBookmark: site('はてなブックマーク', 'bold', 'http://b.hatena.ne.jp/entry/' + url),
  19. instapaper: site('instapaper', 'instapaper', 'http://www.instapaper.com/text?u=' + url),
  20. line: site('LINE', 'comment', 'http://line.me/R/msg/text/?' + title + ' ' + url),
  21. linkedin: site('Linkedin', 'linkedin', 'https://www.linkedin.com/shareArticle?mini=true&url=' + url),
  22. messenger: site('Facebook Messenger', 'commenting', 'fb-messenger://share?link=' + url),
  23. pocket: site('Pocket', 'get-pocket', 'https://getpocket.com/save?url=' + url + '&title=' + title),
  24. qq: site('QQ', 'qq', 'http://connect.qq.com/widget/shareqq/index.html?url=' + url + '&title=' + title),
  25. qzone: site('QQ空间', 'star', 'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=' + url + '&title=' + title),
  26. stumbleupon: site('StumbleUpon', 'stumbleupon', 'http://www.stumbleupon.com/submit?url=' + url + '&title=' + title),
  27. twitter: site('Twitter', 'twitter', 'https://twitter.com/intent/tweet?url=' + title + '&text=' + title),
  28. viber: site('Viber', 'volume-control-phone', 'viber://forward?text='+ url + ' ' + title),
  29. vk: site('VK', 'vk', 'http://vkontakte.ru/share.php?url=' + url),
  30. weibo: site('新浪微博', 'weibo', 'http://service.weibo.com/share/share.php?content=utf-8&url=' + url + '&title=' + title),
  31. whatsapp: site('WhatsApp', 'whatsapp', 'whatsapp://send?text='+ url + ' ' + title),
  32. };
  33. gitbook.events.bind('start', function(e, config) {
  34. var opts = config.sharing;
  35. // Create dropdown menu
  36. var menu = $.map(opts.all, function(id) {
  37. var site = SITES[id];
  38. return {
  39. text: site.label,
  40. onClick: site.onClick
  41. };
  42. });
  43. // Create main button with dropdown
  44. if (menu.length > 0) {
  45. gitbook.toolbar.createButton({
  46. icon: 'fa fa-share-alt',
  47. label: 'Share',
  48. position: 'right',
  49. dropdown: [menu]
  50. });
  51. }
  52. // Direct actions to share
  53. $.each(SITES, function(sideId, site) {
  54. if (!opts[sideId]) return;
  55. gitbook.toolbar.createButton({
  56. icon: site.icon,
  57. label: site.text,
  58. position: 'right',
  59. onClick: site.onClick
  60. });
  61. });
  62. });
  63. });