/**
* Share.js
*
* @author overtrue
* @license MIT
*
* @example
*
* $('.share-components').share();
*
* // or
*
* $('.share-bar').share({
* sites: ['qzone', 'qq', 'weibo','wechat'],
* // ...
* });
*
*/
;
(function ($) {
/**
* Initialize a share bar.
*
* @param {Object} $options globals (optional).
*
* @return {Void}
*/
$.fn.share = function ($options) {
var $head = $(document.head);
var $defaults = {
url: location.href,
site_url: location.origin,
source: $head.find('[name=site], [name=Site]').attr('content') || document.title,
title: $head.find('[name=title], [name=Title]').attr('content') || document.title,
description: $head.find('[name=description], [name=Description]').attr('content') || '',
image: $('img:first').prop('src') || '',
wechatQrcodeTitle: $("html").hasClass("LANG-cn") ? '使用微信扫描二维码' : "Use Wechat scan QR code",
wechatQrcodeHelper: $("html").hasClass("LANG-cn") ? '使用微信扫描二维码' : "Use Wechat scan QR code",
// wechatQrcodeTitle: '微信扫一扫:分享',
// wechatQrcodeHelper: '
微信里点“发现”,扫一下
二维码便可将本文分享至朋友圈。
', mobileSites: [], sites: ['favorite', 'weibo', 'qq', 'wechat', 'tencent', 'douban', 'qzone', 'linkedin', 'diandian', 'facebook', 'twitter', 'google'], disabled: [], initialized: false, email: "information@huawei.com" }; var $globals = $.extend({}, $defaults, $options); var $templates = { favorite: "javascript:void(0);", qzone: 'https://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url={{URL}}&title={{TITLE}}&desc={{DESCRIPTION}}&summary={{SUMMARY}}&site={{SOURCE}}', qq: 'https://connect.qq.com/widget/shareqq/index.html?url={{URL}}&title={{TITLE}}&source={{SOURCE}}&desc={{DESCRIPTION}}', tencent: 'https://share.v.t.qq.com/index.php?c=share&a=index&title={{TITLE}}&url={{URL}}&pic={{IMAGE}}', weibo: 'https://service.weibo.com/share/share.php?url={{URL}}&title={{TITLE}}', wechat: 'javascript:;', douban: 'https://shuo.douban.com/!service/share?href={{URL}}&name={{TITLE}}&text={{DESCRIPTION}}&image={{IMAGE}}&starid=0&aid=0&style=11', diandian: 'https://www.diandian.com/share?lo={{URL}}&ti={{TITLE}}&type=link', linkedin: 'https://www.linkedin.com/shareArticle?mini=true&ro=true&title={{TITLE}}&url={{URL}}&summary={{SUMMARY}}&source={{SOURCE}}&armin=armin', facebook: 'https://www.facebook.com/sharer/sharer.php?u={{URL}}', twitter: 'https://twitter.com/intent/tweet?text={{TITLE}}&url={{URL}}&via={{SITE_URL}}', google: 'https://plus.google.com/share?url={{URL}}', email: 'mailto:{{EMAIL}}?subject={{TITLE}}&body={{URL}}' + encodeURIComponent('\n') + '{{DESCRIPTION}}', print: 'javascript:window.print();void(0)' }; this.each(function () { if ($(this).data('initialized')) { return true; } var $data = $.extend({}, $globals, $(this).data()); var $container = $(this).addClass('share-component social-share'); createIcons($container, $data); createWechat($container, $data); $container.find(".social-share-icon.icon-favorite").off("click").on("click", function () { _addFavorite(); }) if (isMobileScreen()) { $container.find(".social-share-icon.icon-wechat").off("click").on("click", function () { $container.find(".wechat-qrcode").toggle(); return false; }) } //添加utag事件 $container.off("click", ".social-share-icon").on("click", ".social-share-icon", function () { var cmd = $(this).data("cmd"); //console.log(site); if ( typeof(utShare) != "undefined" && typeof (utShare) == "function") { utShare(cmd); } }); $(this).trigger("initialized").data('initialized', true); }); function _addFavorite() { var url = window.location; var title = document.title; var ua = navigator.userAgent.toLowerCase(); if (ua.indexOf("360se") > -1) { alert("由于360浏览器功能限制,请按 Ctrl+D 手动收藏!"); } else if (window.external && typeof window.external.AddToFavoritesBar == "function") { window.external.AddToFavoritesBar(url, title); //IE8 } else if (window.external && typeof window.external.addFavorite == "function") { //IE类浏览器 try { window.external.addFavorite(url, title); } catch (e) { alert('您的浏览器不支持,请按 Ctrl+D 手动收藏!'); } } else if (window.sidebar && typeof window.sidebar.addPanel == "function") { //firfox等浏览器; window.sidebar.addPanel(title, url, ""); } else { alert('您的浏览器不支持,请按 Ctrl+D 手动收藏!'); } } /** * Create site icons * * @param {Object|String} $container * @param {Object} $data */ function createIcons($container, $data) { var $sites = getSites($data); $.each($data.mode == 'prepend' ? $sites.reverse() : $sites, function (i, $name) { var $url = makeUrl($name, $data); var dataName = $name.slice(0, 1).toUpperCase() + $name.slice(1) var $target = ' target="_blank"'; if ($url.indexOf("javascript:") >= 0 || $url.indexOf("mailto:") >= 0) $target = ''; if ($container.data("show-text")) { dataName = $container.data($name + "-text") || dataName; var $link = $data.initialized ? $container.find('.icon-' + $name) : $('' + dataName + ''); } else { var $link = $data.initialized ? $container.find('.icon-' + $name) : $(''); } if (!$link.length) { return true; } $link.prop('href', $url); if (!$data.initialized) { $data.mode == 'prepend' ? $container.prepend($link) : $container.append($link); } }); } /** * Create the wechat icon and QRCode. * * @param {Object|String} $container * @param {Object} $data */ function createWechat($container, $data) { var $wechat = $container.find('a.icon-wechat'); $wechat.append('