KF Preview

預覽帖子內的圖

  1. // ==UserScript==
  2. // @name KF Preview
  3. // @namespace http://tampermonkey.net/
  4. // @author nsps5606
  5. // @version 2017.03.11.1
  6. // @include http://bbs.2dkf.com/thread.php?fid=*
  7. // @include http://bbs.9moe.com/thread.php?fid=*
  8. // @include http://bbs.kfgal.com/thread.php?fid=*
  9. // @include https://kf.miaola.info/thread.php?fid=*
  10. // @require http://libs.baidu.com/jquery/2.1.4/jquery.min.js
  11. // @grant GM_xmlhttpRequest
  12. // @description 預覽帖子內的圖
  13. // ==/UserScript==
  14.  
  15. (function() {
  16.  
  17. var imgDiv = document.createElement("div");
  18. imgDiv.style.cssText = 'max-width: 510px; max-height: 510px; position: fixed; top: 10px; right: 10px; padding: 5px; background-color: #F9E47D; font-size: 24px;';
  19. imgDiv.id = "imgDiv";
  20. document.body.appendChild(imgDiv);
  21.  
  22. var _imgs;
  23. var index = 0;
  24. var onHoverLink;
  25. var isStillOnHover = false;
  26. var tID = -1;
  27. var Links = $(".threadtit1 a");
  28.  
  29. Links.mouseenter(function(){
  30. onHoverLink = this.href;
  31. isStillOnHover = true;
  32. tID = setTimeout(function(){
  33. updateImg(1);
  34. getLink();
  35. }, 500);
  36. });
  37.  
  38. Links.mouseleave(function(){
  39. isStillOnHover = false;
  40. clearTimeout(tID);
  41. updateImg(5);
  42. });
  43.  
  44. function getLink()
  45. {
  46. GM_xmlhttpRequest({
  47. method : 'GET',
  48. synchronous : false,
  49. url : onHoverLink,
  50. onload : function (response) {
  51.  
  52. if(isStillOnHover)
  53. {
  54. var parser = new DOMParser();
  55. var responseDoc = parser.parseFromString(response.responseText, "text/html");
  56.  
  57. var _1F = responseDoc.getElementsByClassName("readtext")[0];
  58. _1F.querySelectorAll(".readidms, .readidm")[0].innerHTML = ""; //去掉頭像
  59. _imgs = _1F.querySelectorAll("img:not([src*='\/post\/smile\/em\/em'])"); //去掉表情
  60.  
  61. if(_imgs.length == 0)
  62. {
  63. updateImg(2);
  64. }
  65. else
  66. {
  67. index = 0;
  68. getNextImg();
  69. }
  70. }
  71.  
  72. }
  73. });
  74. }
  75.  
  76. function getNextImg()
  77. {
  78. var newImg = new Image();
  79. newImg.onload = function(event) {
  80.  
  81. index++;
  82. if(this.naturalHeight > 200 && this.naturalWidth > 200)
  83. {
  84. updateImg(4);
  85. this.style.cssText = "max-width: 500px; max-height: 500px;";
  86. imgDiv.appendChild(this);
  87. }
  88. else if(index >= _imgs.length)
  89. {
  90. updateImg(3);
  91. }
  92. else if(isStillOnHover)
  93. {
  94. getNextImg();
  95. }
  96. };
  97. newImg.src = _imgs[index].src;
  98. }
  99.  
  100. function updateImg(code)
  101. {
  102. if(code == 1)
  103. {
  104. imgDiv.innerHTML = "<div><b>Loading...</b></div>";
  105. imgDiv.style.display = "block";
  106. }
  107. else if(code == 2 && isStillOnHover)
  108. {
  109. imgDiv.innerHTML = "<div><b>沒有圖片</b></div>";
  110. imgDiv.style.display = "block";
  111. }
  112. else if(code == 3 && isStillOnHover)
  113. {
  114. imgDiv.innerHTML = "<div><b>沒有大尺寸圖片</b></div>";
  115. imgDiv.style.display = "block";
  116. }
  117. else if(code == 4 && isStillOnHover)
  118. {
  119. imgDiv.innerHTML = "";
  120. imgDiv.style.display = "block";
  121. }
  122. else if(code == 5)
  123. {
  124. imgDiv.style.display = "none";
  125. imgDiv.innerHTML = "";
  126. }
  127. }
  128.  
  129. })();