1/** 2 * social-share.js 3 * 4 * @author 52cik <[email protected]> 5 * @license MIT 6 * 7 * @example 8 * <pre> 9 * socialShare('.share-components'); 10 * 11 * // or 12 * 13 * socialShare('.share-bar', { 14 * sites: ['qzone', 'qq', 'weibo','wechat'], 15 * // ... 16 * }); 17 * </pre> 18 */ 19function copyText(copyText) { 20 try { 21 navigator.clipboard.writeText(copyText).then(function() { 22 layer.msg('ææ¬å·²å¤å¶å°åªè´´æ¿'); 23 }, function(err) { 24 var tempElement = $('<textarea>'); // åå»ºä¸´æ¶ææ¬åºåå¹¶å°è¯å¤å¶ 25 $('body').append(tempElement); // å°ä¸´æ¶ææ¬åºåæ·»å å°bodyä¸ 26 tempElement.val(copyText).select(); // è®¾ç½®ææ¬åºåçå¼å¹¶éä¸å® 27 document.execCommand('copy'); // æ§è¡å¤å¶å½ä»¤ 28 tempElement.remove(); // ç§»é¤ä¸´æ¶ææ¬åºå 29 layer.msg('ææ¬å·²å¤å¶å°åªè´´æ¿'); 30 }); 31 } catch (err) { 32 var tempElement = $('<textarea>'); // åå»ºä¸´æ¶ææ¬åºåå¹¶å°è¯å¤å¶ 33 $('body').append(tempElement); // å°ä¸´æ¶ææ¬åºåæ·»å å°bodyä¸ 34 tempElement.val(copyText).select(); // è®¾ç½®ææ¬åºåçå¼å¹¶éä¸å® 35 document.execCommand('copy'); // æ§è¡å¤å¶å½ä»¤ 36 tempElement.remove(); // ç§»é¤ä¸´æ¶ææ¬åºå 37 layer.msg('ææ¬å·²å¤å¶å°åªè´´æ¿'); 38 } 39 return false; 40} 41;(function (window, document, undefined) { 42 43 // Initialize a variables. 44 45 var Array$indexOf = Array.prototype.indexOf; 46 var Object$assign = Object.assign; 47 48 var runningInWeChat = /MicroMessenger/i.test(navigator.userAgent); 49 var isMobileScreen = document.documentElement.clientWidth <= 768; 50 51 var image = (document.images[0] || 0).src || ''; 52 var site = getMetaContentByName('site') || getMetaContentByName('Site') || document.title; 53 var title = getMetaContentByName('title') || getMetaContentByName('Title') || document.title; 54 var description = getMetaContentByName('description') || getMetaContentByName('Description') || ''; 55 56 var defaults = { 57 url: location.href, 58 origin: location.origin, 59 source: site, 60 title: title, 61 description: description, 62 image: image, 63 imageSelector: undefined, 64 65 weiboKey: '', 66 67 wechatQrcodeTitle: '微信æ«ä¸æ«ï¼å享', 68 wechatQrcodeHelper: '<p>微信éç¹âåç°âï¼æ«ä¸ä¸</p><p>äºç»´ç 便å¯å°æ¬æåäº«è³æååã</p>', 69 wechatQrcodeSize: 100, 70 71 sites: ['weibo2', 'qq2', 'wechat', 'douban', 'qzone', 'linkedin', 'facebook', 'twitter', 'google', 'link'], 72 mobileSites: [], 73 disabled: [], 74 initialized: false 75 }; 76 77 var templates = { 78 qzone: 'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url={{URL}}&title={{TITLE}}&desc={{DESCRIPTION}}&summary={{SUMMARY}}&site={{SOURCE}}&pics={{IMAGE}}', 79 qq2: 'http://connect.qq.com/widget/shareqq/index.html?url={{URL}}&title={{TITLE}}&source={{SOURCE}}&desc={{DESCRIPTION}}&pics={{IMAGE}}&summary="{{SUMMARY}}"', 80 weibo2: 'https://service.weibo.com/share/share.php?url={{URL}}&title={{TITLE}}&pic={{IMAGE}}&appkey={{WEIBOKEY}}', 81 wechat: 'javascript:', 82 douban: 'http://shuo.douban.com/!service/share?href={{URL}}&name={{TITLE}}&text={{DESCRIPTION}}&image={{IMAGE}}&starid=0&aid=0&style=11', 83 linkedin: 'http://www.linkedin.com/shareArticle?mini=true&ro=true&title={{TITLE}}&url={{URL}}&summary={{SUMMARY}}&source={{SOURCE}}&armin=armin', 84 facebook: 'https://www.facebook.com/sharer/sharer.php?u={{URL}}', 85 twitter: 'https://twitter.com/intent/tweet?text={{TITLE}}&url={{URL}}&via={{ORIGIN}}', 86 google: 'https://plus.google.com/share?url={{URL}}', 87 link: `javascript:copyText('{{URL}}#{{TITLE}}');` 88 }; 89 90 91 /** 92 * Expose API to the global 93 * 94 * @param {String|Element} elem 95 * @param {Object} options 96 */ 97 window.socialShare = function (elem, options) { 98 elem = typeof elem === 'string' ? querySelectorAlls(elem) : elem; 99 100 if (elem.length === undefined) { 101 elem = [elem]; 102 } 103 104 each(elem, function (el) { 105 if (!el.initialized) { 106 share(el, options); 107 } 108 }); 109 }; 110 111 // Domready after initialization 112 alReady(function () { 113 socialShare('.social-share, .share-component'); 114 }); 115 116 117 /** 118 * Initialize a share bar. 119 * 120 * @param {Object} $options globals (optional). 121 * 122 * @return {Void} 123 */ 124 function share(elem, options) { 125 var data = mixin({}, defaults, options || {}, dataset(elem)); 126 127 if (data.imageSelector) { 128 data.image = querySelectorAlls(data.imageSelector).map(function (item) { 129 return item.src; 130 }).join('||'); 131 } 132 133 addClass(elem, 'share-component social-share'); 134 createIcons(elem, data); 135 createWechat(elem, data); 136 137 elem.initialized = true; 138 } 139 140 141 /** 142 * Create site icons 143 * 144 * @param {Element} elem 145 * @param {Object} data 146 */ 147 function createIcons(elem, data) { 148 var sites = getSites(data); 149 var isPrepend = data.mode == 'prepend'; 150 151 each(isPrepend ? sites.reverse() : sites, function (name) { 152 var url = makeUrl(name, data); 153 var link = data.initialized ? getElementsByClassName(elem, 'icon-' + name) : createElementByString('
153<a class="social-share-icon icon-' + name + '"></a>'); 154 155 if (!link.length) { 156 return true; 157 } 158 159 link[0].href = url; 160 161 if (name === 'wechat') { 162 link[0].tabindex = -1; 163 } else { 164 if (name !== 'link') { 165 link[0].target = '_blank'; 166 } 167 } 168 169 if (!data.initialized) { 170 isPrepend ? elem.insertBefore(link[0], elem.firstChild) : elem.appendChild(link[0]); 171 } 172 }); 173 } 174 175 176 /** 177 * Create the wechat icon and QRCode. 178 * 179 * @param {Element} elem 180 * @param {Object} data 181 */ 182 function createWechat(elem, data) { 183 var wechat = getElementsByClassName(elem, 'icon-wechat', 'a'); 184 185 if (wechat.length === 0) { 186 return false; 187 } 188 189 var elems = createElementByString('<div class="wechat-qrcode"><h4>' + data.wechatQrcodeTitle + '</h4><div class="qrcode"></div><div class="help">' + data.wechatQrcodeHelper + '</div></div>'); 190 var qrcode = getElementsByClassName(elems[0], 'qrcode', 'div'); 191 192 new QRCode(qrcode[0], {text: data.url, width: data.wechatQrcodeSize, height: data.wechatQrcodeSize}); 193 wechat[0].appendChild(elems[0]); 194 } 195 196 197 /** 198 * Get available site lists. 199 * 200 * @param {Object} data 201 * 202 * @returns {Array} 203 */ 204 function getSites(data) { 205 if (!data['mobileSites'].length) { 206 data['mobileSites'] = data['sites']; 207 } 208 209 var sites = (isMobileScreen ? data['mobileSites'] : data['sites']).slice(0); 210 var disabled = data['disabled']; 211 212 if (typeof sites == 'string') { 213 sites = sites.split(/\s*,\s*/); 214 } 215 if (typeof disabled == 'string') { 216 disabled = disabled.split(/\s*,\s*/); 217 } 218 219 if (runningInWeChat) { 220 disabled.push('wechat'); 221 } 222 223 // Remove elements 224 disabled.length && each(disabled, function (it) { 225 sites.splice(inArray(it, sites), 1); 226 }); 227 228 return sites; 229 } 230 231 232 /** 233 * Build the url of icon. 234 * 235 * @param {String} name 236 * @param {Object} data 237 * 238 * @returns {String} 239 */ 240 function makeUrl(name, data) { 241 242 if (!data['summary']) { 243 data['summary'] = data['description']; 244 } 245 246 return templates[name].replace(/\{\{(\w)(\w*)\}\}/g, function (m, fix, key) { 247 var nameKey = name + fix + key.toLowerCase(); 248 key = (fix + key).toLowerCase(); 249 250 return encodeURIComponent((data[nameKey] === undefined ? data[key] : data[nameKey]) || ''); 251 }); 252 } 253 254 255 /** 256 * Supports querySelectorAll, jQuery, Zepto and simple selector. 257 * 258 * @param str 259 * 260 * @returns {*} 261 */ 262 function querySelectorAlls(str) { 263 return (document.querySelectorAll || window.jQuery || window.Zepto || selector).call(document, str); 264 } 265 266 267 /** 268 * Simple selector. 269 * 270 * @param {String} str #ID or .CLASS 271 * 272 * @returns {Array} 273 */ 274 function selector(str) { 275 var elems = []; 276 277 each(str.split(/\s*,\s*/), function (s) { 278 var m = s.match(/([#.])(\w+)/); 279 if (m === null) { 280 throw Error('Supports only simple single #ID or .CLASS selector.'); 281 } 282 283 if (m[1]) { 284 var elem = document.getElementById(m[2]); 285 286 if (elem) { 287 elems.push(elem); 288 } 289 } 290 291 elems = elems.concat(getElementsByClassName(str)); 292 }); 293 294 return elems; 295 } 296 297 298 /** 299 * Add the classNames for element. 300 * 301 * @param {Element} elem 302 * @param {String} value 303 */ 304 function addClass(elem, value) { 305 if (value && typeof value === "string") { 306 var classNames = (elem.className + ' ' + value).split(/\s+/); 307 var setClass = ' '; 308 309 each(classNames, function (className) { 310 if (setClass.indexOf(' ' + className + ' ') < 0) { 311 setClass += className + ' '; 312 } 313 }); 314 315 elem.className = setClass.slice(1, -1); 316 } 317 } 318 319 320 /** 321 * Get meta element content value 322 * 323 * @param {String} name 324 * 325 * @returns {String|*} 326 */ 327 function getMetaContentByName(name) { 328 return (document.getElementsByName(name)[0] || 0).content; 329 } 330 331 332 /** 333 * Get elements By className for IE8- 334 * 335 * @param {Element} elem element 336 * @param {String} name className 337 * @param {String} tag tagName 338 * 339 * @returns {HTMLCollection|Array} 340 */ 341 function getElementsByClassName(elem, name, tag) { 342 if (elem.getElementsByClassName) { 343 return elem.getElementsByClassName(name); 344 } 345 346 var elements = []; 347 var elems = elem.getElementsByTagName(tag || '*'); 348 name = ' ' + name + ' '; 349 350 each(elems, function (elem) { 351 if ((' ' + (elem.className || '') + ' ').indexOf(name) >= 0) { 352 elements.push(elem); 353 } 354 }); 355 356 return elements; 357 } 358 359 360 /** 361 * Create element by string. 362 * 363 * @param {String} str 364 * 365 * @returns {NodeList} 366 */ 367 function createElementByString(str) { 368 var div = document.createElement('div'); 369 div.innerHTML = str; 370 371 return div.childNodes; 372 } 373 374 375 /** 376 * Merge objects. 377 * 378 * @returns {Object} 379 */ 380 function mixin() { 381 var args = arguments; 382 383 if (Object$assign) {
384 return Object$assign.apply(null, args); 385 } 386 387 var target = {}; 388 389 each(args, function (it) { 390 each(it, function (v, k) { 391 target[k] = v; 392 }); 393 }); 394 395 return args[0] = target; 396 } 397 398 399 /** 400 * Get dataset object. 401 * 402 * @param {Element} elem 403 * 404 * @returns {Object} 405 */ 406 function dataset(elem) { 407 if (elem.dataset) { 408 return JSON.parse(JSON.stringify(elem.dataset)); 409 } 410 411 var target = {}; 412 413 if (elem.hasAttributes()) { 414 each(elem.attributes, function (attr) { 415 var name = attr.name; 416 if (name.indexOf('data-') !== 0) { 417 return true; 418 } 419 420 name = name.replace(/^data-/i, '') 421 .replace(/-(\w)/g, function (all, letter) { 422 return letter.toUpperCase(); 423 }); 424 425 target[name] = attr.value; 426 }); 427 428 return target; 429 } 430 431 return {}; 432 } 433 434 435 /** 436 * found element in the array. 437 * 438 * @param {Array|Object} elem 439 * @param {Array} arr 440 * @param {Number} i 441 * 442 * @returns {Number} 443 */ 444 function inArray(elem, arr, i) { 445 var len; 446 447 if (arr) { 448 if (Array$indexOf) { 449 return Array$indexOf.call(arr, elem, i); 450 } 451 452 len = arr.length; 453 i = i ? i < 0 ? Math.max(0, len + i) : i : 0; 454 455 for (; i < len; i++) { 456 // Skip accessing in sparse arrays 457 if (i in arr && arr[i] === elem) { 458 return i; 459 } 460 } 461 } 462 463 return -1; 464 } 465 466 467 /** 468 * Simple each. 469 * 470 * @param {Array|Object} obj 471 * @param {Function} callback 472 * 473 * @returns {*} 474 */ 475 function each(obj, callback) { 476 var length = obj.length; 477 478 if (length === undefined) { 479 for (var name in obj) { 480 if (obj.hasOwnProperty(name)) { 481 if (callback.call(obj[name], obj[name], name) === false) { 482 break; 483 } 484 } 485 } 486 } else { 487 for (var i = 0; i < length; i++) { 488 if (callback.call(obj[i], obj[i], i) === false) { 489 break; 490 } 491 } 492 } 493 } 494 495 496 /** 497 * Dom ready. 498 * 499 * @param {Function} fn 500 * 501 * @link https://github.com/jed/alReady.js 502 */ 503 function alReady(fn) { 504 var add = 'addEventListener'; 505 var pre = document[add] ? '' : 'on'; 506 507 ~document.readyState.indexOf('m') ? fn() : 508 'load DOMContentLoaded readystatechange'.replace(/\w+/g, function (type, i) { 509 (i ? document : window) 510 [pre ? 'attachEvent' : add] 511 ( 512 pre + type, 513 function () { 514 if (fn) if (i < 6 || ~document.readyState.indexOf('m')) fn(), fn = 0 515 }, 516 !1 517 ) 518 }) 519 } 520})(window, document);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.