sns_share.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. var SnsShare = {
  2. buttons : {},
  3. protocols : {
  4. tsina: 'http://v.t.sina.com.cn/share/share.php?title=%s&url=%s&pic=%s&source=bookmark&appkey=%s&ralateUid=%s',
  5. tqq : 'http://share.v.t.qq.com/index.php?c=share&a=index&title=%s&url=%s&pic=%s&appkey=%s&site=lolbox.duowan.com',
  6. qzone : 'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?title=%s&url=%s&pics=%s&summary=%s'
  7. },
  8. init : function() {
  9. for (var k in SnsShare.protocols) {
  10. var buttons = SnsShare.getElementsByClassName(null, 'a', 'button_' + k);
  11. if (buttons.length > 0) {
  12. var buttonsToAdd = [];
  13. for (var i = 0, j = buttons.length; i < j; i++) {
  14. if (buttons[i].onclick) continue;
  15. buttons[i].onclick = function() {
  16. SnsShare.share(this);
  17. return false;
  18. };
  19. buttons[i].style.cursor = 'pointer';
  20. buttonsToAdd.push(buttons[i]);
  21. };
  22. SnsShare.buttons[k] = buttonsToAdd;
  23. }
  24. }
  25. },
  26. share : function(button) {
  27. var key = button.className.substr(7);
  28. if (!SnsShare[key]) return;
  29. var appkey = jiathis_config.appkey[key];
  30. var protocol = SnsShare.protocols[key];
  31. var url = SnsShare[key].call(null, appkey, protocol);
  32. window.open(url);
  33. },
  34. tsina : function(appkey, protocol) {
  35. var url = jiathis_config.url || window.location.href;
  36. var pic = jiathis_config.pic || '';
  37. return SnsShare.sprintf(protocol, encodeURIComponent(jiathis_config.title), encodeURIComponent(url),
  38. encodeURIComponent(pic), appkey, jiathis_config.ralateuid['tsina']);
  39. },
  40. tqq : function(appkey, protocol) {
  41. var url = jiathis_config.url || window.location.href;
  42. var pic = jiathis_config.pic || '';
  43. return SnsShare.sprintf(protocol, encodeURIComponent(jiathis_config.title), encodeURIComponent(url),
  44. encodeURIComponent(pic), appkey);
  45. },
  46. qzone : function(appkey, protocol) {
  47. var url = jiathis_config.url || window.location.href;
  48. var pic = jiathis_config.pic || '';
  49. return SnsShare.sprintf(protocol, encodeURIComponent(jiathis_config.title), encodeURIComponent(url),
  50. encodeURIComponent(pic), encodeURIComponent(jiathis_config.summary));
  51. },
  52. getElementsByClassName : function(fatherId, tagName, className) {
  53. var node = fatherId && document.getElementById(fatherId) || document;
  54. tagName = tagName || "*";
  55. className = className.split(" ");
  56. var classNameLength = className.length;
  57. for (var i = 0, j = classNameLength; i < j; i++) {
  58. //创建匹配类名的正则
  59. className[i] = new RegExp("(^|\\s)" + className[i].replace(/\-/g, "\\-") + "(\\s|$)");
  60. }
  61. var elements = node.getElementsByTagName(tagName);
  62. var result = [];
  63. for (var i = 0, j = elements.length, k = 0; i < j; i++) { //缓存length属性
  64. var element = elements[i];
  65. while (className[k++].test(element.className)) { //优化循环
  66. if (k === classNameLength) {
  67. result[result.length] = element;
  68. break;
  69. }
  70. }
  71. k = 0;
  72. }
  73. return result;
  74. },
  75. sprintf : function() {
  76. var i = 0,
  77. a, f = arguments[i++],
  78. o = [],
  79. m, p, c, x, s = '';
  80. while (f) {
  81. if (m = /^[^%]+/.exec(f)) {
  82. o.push(m[0]);
  83. } else if (m = /^%{2}/.exec(f)) {
  84. o.push('%');
  85. } else if (m = /^%(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) {
  86. if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) {
  87. throw ('Too few arguments.');
  88. }
  89. if (/[^s]/.test(m[7]) && (typeof(a) != 'number')) {
  90. throw ('Expecting number but found ' + typeof(a));
  91. }
  92. switch (m[7]) {
  93. case 'b':
  94. a = a.toString(2);
  95. break;
  96. case 'c':
  97. a = String.fromCharCode(a);
  98. break;
  99. case 'd':
  100. a = parseInt(a);
  101. break;
  102. case 'e':
  103. a = m[6] ? a.toExponential(m[6]) : a.toExponential();
  104. break;
  105. case 'f':
  106. a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a);
  107. break;
  108. case 'o':
  109. a = a.toString(8);
  110. break;
  111. case 's':
  112. a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a);
  113. break;
  114. case 'u':
  115. a = Math.abs(a);
  116. break;
  117. case 'x':
  118. a = a.toString(16);
  119. break;
  120. case 'X':
  121. a = a.toString(16).toUpperCase();
  122. break;
  123. }
  124. a = (/[def]/.test(m[7]) && m[2] && a >= 0 ? '+' + a : a);
  125. c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' ';
  126. x = m[5] - String(a).length - s.length;
  127. p = m[5] ? str_repeat(c, x) : '';
  128. o.push(s + (m[4] ? a + p : p + a));
  129. } else {
  130. throw ('Huh ?!');
  131. }
  132. f = f.substring(m[0].length);
  133. }
  134. return o.join('');
  135. }
  136. };