nhentai Display and Highlight Tag with Thumbnail

nHentai 显示并高亮Tag在缩略图模式

Per 11-01-2020. Zie de nieuwste versie.

  1. // ==UserScript==
  2. // @name nhentai Display and Highlight Tag with Thumbnail
  3. // @namespace nhentai_display_and_highlight_tag_with_thumbnail
  4. // @supportURL https://github.com/zhuzemin
  5. // @description nHentai 显示并高亮Tag在缩略图模式
  6. // @include https://nhentai.net/*
  7. // @include https://en.nyahentai3.com/*
  8. // @include https://zh.nyahentai.co/*
  9. // @include https://ja.nyahentai.net/*
  10. // @version 1.3
  11. // @grant GM_xmlhttpRequest
  12. // @grant GM_registerMenuCommand
  13. // @grant GM_setValue
  14. // @grant GM_getValue
  15. // @run-at document-start
  16. // @author zhuzemin
  17. // @license Mozilla Public License 2.0; http://www.mozilla.org/MPL/2.0/
  18. // @license CC Attribution-ShareAlike 4.0 International; http://creativecommons.org/licenses/by-sa/4.0/
  19. // ==/UserScript==
  20. var config = {
  21. 'debug': false
  22. }
  23. var debug = config.debug ? console.log.bind(console) : function () {
  24. };
  25.  
  26. // setting User Preferences
  27. function setUserPref(varName, defaultVal, menuText, promtText, sep){
  28. GM_registerMenuCommand(menuText, function() {
  29. var val = prompt(promtText, GM_getValue(varName, defaultVal));
  30. if (val === null) { return; } // end execution if clicked CANCEL
  31. // prepare string of variables separated by the separator
  32. if (sep && val){
  33. var pat1 = new RegExp('\\s*' + sep + '+\\s*', 'g'); // trim space/s around separator & trim repeated separator
  34. var pat2 = new RegExp('(?:^' + sep + '+|' + sep + '+$)', 'g'); // trim starting & trailing separator
  35. //val = val.replace(pat1, sep).replace(pat2, '');
  36. }
  37. //val = val.replace(/\s{2,}/g, ' ').trim(); // remove multiple spaces and trim
  38. GM_setValue(varName, val);
  39. // Apply changes (immediately if there are no existing highlights, or upon reload to clear the old ones)
  40. //if(!document.body.querySelector(".THmo")) THmo_doHighlight(document.body);
  41. //else location.reload();
  42. });
  43. }
  44.  
  45. // prepare UserPrefs
  46. setUserPref(
  47. 'highlights',
  48. 'chinese;',
  49. 'Set Highlight Tags',
  50. `Set highlights, split with ";". Example: "mmf threesome; chinese"`,
  51. ','
  52. );
  53. setUserPref(
  54. 'BlackList',
  55. 'english;',
  56. 'Set BlackList',
  57. `Set BlackList, split with ";". Example: "chinese; yaoi"`,
  58. ','
  59. );
  60.  
  61.  
  62. CreateStyle=function(){
  63. debug("Start: CreateStyle");
  64. var style=document.createElement("style");
  65. style.setAttribute("type","text/css");
  66. style.innerHTML=`
  67. .glowbox {
  68. background: #4c4c4c;
  69. //width: 400px;
  70. //margin: 40px 0 0 40px;
  71. //padding: 10px;
  72. -moz-box-shadow: 0 0 5px 5px #FFFF00;
  73. -webkit-box-shadow: 0 0 5px 5px #FFFF00;
  74. box-shadow: 0 0 5px 5px #FFFF00;
  75. }
  76. `;
  77. debug("Processing: CreateStyle");
  78. var head=document.querySelector("head");
  79. head.insertBefore(style,null);
  80. debug("End: CreateStyle");
  81. }
  82. class Gallery{
  83. constructor(href) {
  84. this.method = 'GET';
  85. this.url = href;
  86. this.headers = {
  87. 'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
  88. 'Accept': 'application/atom+xml,application/xml,text/xml',
  89. 'Referer': window.location.href,
  90. };
  91. this.charset = 'text/plain;charset=utf8';
  92. }
  93. }
  94. var init = function () {
  95. var LastDivNum=0;
  96. var highlights=[];
  97. var BlackList=[];
  98. try{
  99. highlights=GM_getValue("highlights").split(";");
  100. BlackList=GM_getValue("BlackList").split(";");
  101. }catch(e){
  102. debug("Not set GM_Value.");
  103. }
  104. CreateStyle();
  105. setInterval(function(){
  106. var divs = document.querySelectorAll('div.gallery');
  107. debug("DivNum: "+divs.length);
  108. if(LastDivNum<divs.length) {
  109. for (var i = LastDivNum; i < divs.length; ++i) {
  110. (function (div) {
  111. div.style.maxHeight = "900px";
  112. div.style.height = "900px";
  113. var a = div.querySelector("a");
  114. var img = a.querySelector("img");
  115. var data_src=img.getAttribute("data-src");
  116. img.setAttribute("src",data_src);
  117. div.insertBefore(img, a);
  118. a.style.overflow = "auto";
  119. a.style.maxHeight = 900 - img.offsetHeight + "px";
  120. var caption = a.querySelector("div.caption");
  121. caption.style.position = "static";
  122. var taglist = document.createElement("section");
  123. taglist.setAttribute("id", "tags");
  124. a.insertBefore(taglist, null);
  125. var href = div.querySelector('a').href;
  126. debug(href);
  127. var gallery = new Gallery(href);
  128. var retries = 10;
  129. var request = function () {
  130. GM_xmlhttpRequest({
  131. method: gallery.method,
  132. url: gallery.url,
  133. headers: gallery.headers,
  134. overrideMimeType: gallery.charset,
  135. //synchronous: true
  136. onload: function (responseDetails) {
  137. if (responseDetails.status != 200) {
  138. // retry
  139. if (retries--) { // *** Recurse if we still have retries
  140. setTimeout(request(),2000);
  141. return;
  142. }
  143. }
  144. debug(responseDetails);
  145. var galleryHtml = new DOMParser().parseFromString(responseDetails.responseText, "text/html");
  146. //debug(galleryHtml);
  147. taglist = galleryHtml.querySelector('#tags');
  148. var links = taglist.querySelectorAll("a.tag");
  149. //debug(taglist);
  150. for (var link of links) {
  151. for (var BlackWord of BlackList) {
  152. if (BlackWord.length > 1) {
  153. for (var highlight of highlights) {
  154. //debug("Highlight: "+highlight);
  155. if (highlight.length > 1) {
  156. //var span=link.querySelector("span.count");
  157. //link.removeChild(span);
  158. var tag = link.innerText.toLowerCase().match(/([\w\s]*)/)[1].trim();
  159. //debug("Tag: "+tag);
  160. if (tag == BlackWord.trim()) {
  161. div.className +=" blacklisted";
  162. return;
  163. }
  164. else if (tag == highlight.trim()) {
  165. debug("Tag: " + link.innerText);
  166. link.className += " glowbox";
  167. }
  168. }
  169. }
  170. }
  171. }
  172. }
  173. a.replaceChild(taglist, a.querySelector("#tags"));
  174. }
  175. });
  176. }
  177. request();
  178. })(divs[i]);
  179. }
  180. }
  181. LastDivNum=divs.length;
  182.  
  183. }, 2000)
  184. }
  185. window.addEventListener('DOMContentLoaded', init);