jandan comment refresh

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

当前为 2018-07-01 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name jandan comment refresh
  3. // @namespace mtdwss@gmail.com
  4. // @version 0.2.2
  5. // @description 1、添加评论页内刷新按钮;2、显示图片原始大小。
  6. // @author mtdwss@gmail.com
  7. // @match http*://jandan.net/pic*
  8. // @match http*://jandan.net/ooxx*
  9. // @match http*://jandan.net/duan*
  10. // @match http*://jandan.net/qa*
  11. // @match http*://jandan.net/top
  12. // @match http*://jandan.net/drawings*
  13. // @match http*://jandan.net/pond*
  14. // @match http*://jandan.net/zhoubian*
  15. // @grant GM_xmlhttpRequest
  16. // ==/UserScript==
  17.  
  18. (function () {
  19. 'use strict';
  20.  
  21. if (window.top != window.self) return;
  22.  
  23. //执行刷新操作
  24. document.refreshComment = function (node) {
  25. node = node.parent().parent('div');
  26. var c = node.attr('id').split('-')[2];
  27. node.children('.tucao-hot, .tucao-list, .jandan-tucao-more, .jandan-tucao-close, .tucao-form, #tucao-gg').remove().empty();
  28. var a = $('<div class="tucao-tmp">数据加载中....biubiubiu....</div>');
  29. node.append(a);
  30. $.ajax({
  31. url: "/tucao/" + c,
  32. method: "GET",
  33. data: { _: (new Date()).valueOf() },
  34. dataType: "json",
  35. success: function (f) {
  36. node.children('.tucao-tmp').remove().empty();
  37. if (f.code != 0) {
  38. alert(f.msg);
  39. return;
  40. }
  41. if (f.hot_tucao.length) {
  42. tucao_show_hot(node, f.hot_tucao);
  43. }
  44. tucao_show_list(node, f.tucao);
  45. if (f.has_next_page) {
  46. tucao_show_more_btn(node, c);
  47. }
  48. tucao_show_close_btn(node, c);
  49. tucao_show_form(node, c);
  50. },
  51. error: function (e) {
  52. a.html("hmm....something wrong...");
  53. }
  54. });
  55. };
  56.  
  57. //添加刷新按钮
  58. $(document).bind('DOMNodeInserted', function (e) {
  59. var element = e.target;
  60. element = $(element);
  61. if (element.hasClass('tucao-form')) {
  62. var node = element.parent('div');
  63. if (node.find('.tucao-refresh').length) return;
  64. node.prepend('<div class="tucao-refresh" style="text-align: right;"><span style="cursor: pointer;" onclick="refreshComment($(this))">刷新</span></div>');
  65. }
  66. });
  67.  
  68. //添加原图大小
  69. $('.view_img_link').each(function (idx, val) {
  70. var view_img_link = $(this).attr('href');
  71. var _this = $(this);
  72. GM_xmlhttpRequest({
  73. method: "HEAD",
  74. url: 'https:' + view_img_link,
  75. onload: function (response) {
  76. var headers = {};
  77. var headarray = response.responseHeaders.split('\n');
  78. for (var i in headarray) {
  79. var d = headarray[i].split(':');
  80. var k = d[0],
  81. v = d[1] === undefined ? undefined : d[1].trim();
  82. headers[k] = v;
  83. }
  84.  
  85. var imgSize = headers['Content-Length'.toLowerCase()];
  86. imgSize = bytesToSize(imgSize);
  87. var vim_node = $("<a href='javascript:;' style='font-weight:700;font-size:12px;'> (" + imgSize + ")</a>");
  88. var img_elem = _this.next('br').next('img');
  89. var mask_elem = img_elem.next('.gif-mask');
  90. if(mask_elem.size()>0){
  91. mask_elem = mask_elem.eq(0);
  92. vim_node.attr('mask',true);
  93. }
  94. vim_node.click(function(){
  95. if(vim_node.attr('mask')==='true'){
  96. mask_elem.click();
  97. vim_node.attr('mask',false);
  98. }
  99. else{
  100. img_elem.click();
  101. }
  102. })
  103. vim_node.insertAfter(_this);
  104. _this.prop('title', '原图大小:' + imgSize);
  105. }
  106. });
  107. });
  108.  
  109. //byte -> KB,MB,GB
  110. function bytesToSize(bytes) {
  111. if (bytes < 1024) return bytes + " Bytes";
  112. else if (bytes < 1048576) return (bytes / 1024).toFixed(3) + " KB";
  113. else if (bytes < 1073741824) return (bytes / 1048576).toFixed(3) + " MB";
  114. else return (bytes / 1073741824).toFixed(3) + " GB";
  115. }
  116. })();