qshare.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. [Discuz!] (C)2001-2009 Comsenz Inc.
  3. This is NOT a freeware, use is subject to license terms
  4. $Id$
  5. */
  6. var _share_tencent_weibo = function() {
  7. var share_btn = function(_arr) {
  8. if (_arr[0]) {
  9. return _arr[0];
  10. }
  11. else {
  12. var o = document.createElement("a"),
  13. _ostyle = "width:92px;height:22px;background:url(http://open.t.qq.com/apps/qshare/images/icon.gif) no-repeat #f00;position:absolute;display:none;";
  14. o.setAttribute("style", _ostyle);
  15. o.style.cssText = _ostyle;
  16. o.setAttribute("href", "javascript:;");
  17. document.body.insertBefore(o, document.body.childNodes[0]);
  18. return o;
  19. }
  20. } (arguments);
  21. var share_area = function(_arr) {
  22. if (_arr[1]) {
  23. if ((typeof _arr[1] == "object" && _arr[1].length) || (_arr[1].constructor == Array)) {
  24. return _arr[1];
  25. } else {
  26. return [_arr[1]];
  27. }
  28. }
  29. else {
  30. return [document.body];
  31. }
  32. } (arguments);
  33. var current_area = share_area[0];
  34. var _site = arguments[2] ? arguments[2] : "";
  35. var _appkey = encodeURI(arguments[3] ? arguments[3] : "");
  36. var _web = {
  37. "name": arguments[4] || "",
  38. "href": location.href,
  39. "hash": location.hash
  40. };
  41. var _pic = function(area) {
  42. var _imgarr = area.getElementsByTagName("img");
  43. var _srcarr = [];
  44. for (var i = 0; i < _imgarr.length; i++) {
  45. _srcarr.push(_imgarr[i].src);
  46. }
  47. return _srcarr.join("|");
  48. };
  49. var _u = 'http://v.t.qq.com/share/share.php?url=$url$&appkey=' + _appkey + '&site=' + _site + '&title=$title$&pic=$pic$';
  50. var _select = function() {
  51. return (document.selection ? document.selection.createRange().text: document.getSelection()).toString().replace(/[\s\n]+/g, " ");
  52. };
  53. if ( !! window.find) {
  54. HTMLElement.prototype.contains = function(B) {
  55. return this.compareDocumentPosition(B) - 19 > 0
  56. }
  57. }
  58. String.prototype.elength = function() {
  59. return this.replace(/[^\u0000-\u00ff]/g, "aa").length;
  60. }
  61. document.onmouseup = function(e) {
  62. e = e || window.event;
  63. var o = e.target || e.srcElement;
  64. for (var i = 0; i < share_area.length; i++) {
  65. if (share_area[i].contains(o) || share_area[i] == o) {
  66. var _e = {
  67. "x": e.clientX,
  68. "y": e.clientY
  69. };
  70. var _o = {
  71. "w": share_btn.clientWidth,
  72. "h": share_btn.clientHeight
  73. };
  74. var _d = window.pageYOffset || (document.documentElement || document.body).scrollTop || 0;
  75. var x = (_e.x - _o.w < 0) ? _e.x + _o.w: _e.x - _o.w,
  76. y = (_e.y - _o.h < 0) ? _e.y + _d - _o.h: _e.y + _d - _o.h + ( - [1, ] ? 10 : 0);
  77. if (_select() && _select().length >= 10) {
  78. with(share_btn.style) {
  79. display = "inline-block";
  80. left = (x - 5) + "px";
  81. top = y + "px";
  82. position = "absolute";
  83. zIndex = "999999";
  84. }
  85. current_area = share_area[i];
  86. break;
  87. } else {
  88. share_btn.style.display = "none";
  89. }
  90. } else {
  91. share_btn.style.display = "none";
  92. }
  93. }
  94. };
  95. share_btn.onclick = function() {
  96. var _str = _select();
  97. var _strmaxlen = 280 - ("\u6211\u6765\u81EA\u4E8E\u817E\u8BAF\u5FAE\u535A\u5F00\u653E\u5E73\u53F0" + " " + _web.name).elength();
  98. var _resultstr = "";
  99. if (_str.elength() > _strmaxlen) {
  100. _strmaxlen = _strmaxlen - 3;
  101. for (var i = _strmaxlen >> 1; i <= _strmaxlen; i++) {
  102. if ((_str.slice(0, i)).elength() > _strmaxlen) {
  103. break;
  104. }
  105. else {
  106. _resultstr = _str.slice(0, i);
  107. }
  108. }
  109. _resultstr += "...";
  110. } else {
  111. _resultstr = _str;
  112. }
  113. if (_str) {
  114. var url = _u.replace("$title$", encodeURIComponent(_resultstr + " " + _web.name)).replace("$pic$", _pic(current_area));
  115. url = url.replace("$url$", encodeURIComponent(_web.href.replace(_web.hash, "") + "#" + (current_area["name"] || current_area["id"] || "")));
  116. if (!- [1, ]) {
  117. url = url.substr(0, 2048);
  118. }
  119. window.open(url, 'null', 'width=700,height=680,top=0,left=0,toolbar=no,menubar=no,scrollbars=no,location=yes,resizable=no,status=no');
  120. }
  121. };
  122. };