jandan comment refresh

1、添加评论页内刷新按钮;2、显示图片原始大小。

  1. // ==UserScript==
  2. // @name jandan comment refresh
  3. // @namespace mtdwss@gmail.com
  4. // @version 0.2.3
  5. // @description 1、添加评论页内刷新按钮;2、显示图片原始大小。
  6. // @author mtdwss@gmail.com
  7. // @match http*://jandan.net/*
  8. // @grant GM_xmlhttpRequest
  9. // ==/UserScript==
  10.  
  11. (function () {
  12. 'use strict';
  13.  
  14. if (window.top != window.self) return;
  15.  
  16. if (window.location.href.match(/^http(s?)\:\/\/jandan\.net\/20\S+\.html/g)) {
  17. var wc = wilsonScore();
  18. var cl = $('ol.commentlist>li');
  19. cl.sort(function ($1, $2) {
  20. $1 = $($1);
  21. $2 = $($2);
  22. var nag1, nag2, pos1, pos2;
  23. var score1, score2;
  24. if ($1.attr('score')) {
  25. score1 = $1.attr('score');
  26. } else {
  27. nag1 = $1.find('.tucao-unlike-container').children('span').eq(0).text().trim();
  28. pos1 = $1.find('.tucao-like-container').children('span').eq(0).text().trim();
  29. score1 = wc(pos1, nag1);
  30. if (isNaN(score1)) score1 = 0;
  31. $1.attr('score', score1);
  32. console.log(nag1, pos1, score1);
  33. }
  34.  
  35. if ($2.attr('score')) {
  36. score2 = $2.attr('score');
  37. } else {
  38. nag2 = $2.find('.tucao-unlike-container').children('span').eq(0).text();
  39. pos2 = $2.find('.tucao-like-container').children('span').eq(0).text();
  40. score2 = wc(pos2, nag2);
  41. if (isNaN(score2)) score2 = 0;
  42. $2.attr('score', score2);
  43. console.log(nag2, pos2, score2);
  44. }
  45. if (score1 > score2) return -1;
  46.  
  47. if (score1 == score2) return 0;
  48. return 1;
  49. });
  50. $('.hot-list').after('<div id="hot-comment"><span style="font-size:16px;color:red;">热门评论</span><ol></ol></div>')
  51. for (var i = 0; i < 10; i++) {
  52. if (cl.eq(i)) {
  53. var node = $(cl.eq(i)).clone();
  54. node.find('small,.comment-report,.tucao-btn,.righttext').remove().empty();
  55. $('#hot-comment>ol').append(node);
  56. }
  57. }
  58. } else {
  59. //执行刷新操作
  60. document.refreshComment = function (node) {
  61. node = node.parent().parent('div');
  62. var c = node.attr('id').split('-')[2];
  63. node.children('.tucao-hot, .tucao-list, .jandan-tucao-more, .jandan-tucao-close, .tucao-form, #tucao-gg').remove().empty();
  64. var a = $('<div class="tucao-tmp">数据加载中....biubiubiu....</div>');
  65. node.append(a);
  66. $.ajax({
  67. url: "/tucao/" + c,
  68. method: "GET",
  69. data: {
  70. _: (new Date()).valueOf()
  71. },
  72. dataType: "json",
  73. success: function (f) {
  74. node.children('.tucao-tmp').remove().empty();
  75. if (f.code != 0) {
  76. alert(f.msg);
  77. return;
  78. }
  79. if (f.hot_tucao.length) {
  80. tucao_show_hot(node, f.hot_tucao);
  81. }
  82. tucao_show_list(node, f.tucao);
  83. if (f.has_next_page) {
  84. tucao_show_more_btn(node, c);
  85. }
  86. tucao_show_close_btn(node, c);
  87. tucao_show_form(node, c);
  88. },
  89. error: function (e) {
  90. a.html("hmm....something wrong...");
  91. }
  92. });
  93. };
  94.  
  95. //添加刷新按钮
  96. $(document).bind('DOMNodeInserted', function (e) {
  97. var element = e.target;
  98. element = $(element);
  99. if (element.hasClass('tucao-form')) {
  100. var node = element.parent('div');
  101. if (node.find('.tucao-refresh').length) return;
  102. node.prepend('<div class="tucao-refresh" style="text-align: right;"><span style="cursor: pointer;" onclick="refreshComment($(this))">刷新</span></div>');
  103. }
  104. });
  105.  
  106. //添加原图大小
  107. $('.view_img_link').each(function (idx, val) {
  108. var view_img_link = $(this).attr('href');
  109. var _this = $(this);
  110. GM_xmlhttpRequest({
  111. method: "HEAD",
  112. url: 'https:' + view_img_link,
  113. onload: function (response) {
  114. var headers = {};
  115. var headarray = response.responseHeaders.split('\n');
  116. for (var i in headarray) {
  117. var d = headarray[i].split(':');
  118. var k = d[0],
  119. v = d[1] === undefined ? undefined : d[1].trim();
  120. headers[k] = v;
  121. }
  122.  
  123. var imgSize = headers['Content-Length'.toLowerCase()];
  124. imgSize = bytesToSize(imgSize);
  125. var vim_node = $("<a href='javascript:;' style='font-weight:700;font-size:12px;'> (" + imgSize + ")</a>");
  126. var img_elem = _this.next('br').next('img');
  127. var mask_elem = img_elem.next('.gif-mask');
  128. if (mask_elem.size() > 0) {
  129. mask_elem = mask_elem.eq(0);
  130. vim_node.attr('mask', true);
  131. }
  132. vim_node.click(function () {
  133. if (vim_node.attr('mask') === 'true') {
  134. mask_elem.click();
  135. vim_node.attr('mask', false);
  136. } else {
  137. img_elem.click();
  138. }
  139. })
  140. vim_node.insertAfter(_this);
  141. _this.prop('title', '原图大小:' + imgSize);
  142. }
  143. });
  144. });
  145. }
  146.  
  147.  
  148. //byte -> KB,MB,GB
  149. function bytesToSize(bytes) {
  150. if (bytes < 1024) return bytes + " Bytes";
  151. else if (bytes < 1048576) return (bytes / 1024).toFixed(3) + " KB";
  152. else if (bytes < 1073741824) return (bytes / 1048576).toFixed(3) + " MB";
  153. else return (bytes / 1073741824).toFixed(3) + " GB";
  154. }
  155.  
  156. function wilsonScore(z) {
  157. if (z == null) {
  158. // z represents the statistical confidence
  159. // z = 1.0 => ~69%, 1.96 => ~95% (default)
  160. z = 1.96;
  161. }
  162.  
  163. return function (ups, downs) {
  164. var n = ups + downs;
  165. if (n === 0) {
  166. return 0;
  167. }
  168.  
  169. var p = ups / n,
  170. sqrtexpr = (p * (1 - p) + z * z / (4 * n)) / n;
  171. return (p + z * z / (2 * n) - z * Math.sqrt(sqrtexpr)) / (1 + z * z / n);
  172. };
  173. };
  174. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址