nhentai show Tag in search page

nhentai show Tag in search page, and highlight block function

Fra 18.02.2020. Se den seneste versjonen.

  1. // ==UserScript==
  2. // @name nhentai show Tag in search page
  3. // @name:ja nhentai show Tag in search page
  4. // @name:zh-TW nhentai show Tag in search page
  5. // @name:zh-CN nhentai show Tag in search page
  6. // @namespace nhentai_display_and_highlight_tag_with_thumbnail
  7. // @supportURL https://github.com/zhuzemin
  8. // @description nhentai show Tag in search page, and highlight block function
  9. // @description:zh-CN nhentai show Tag in search page, and highlight block function
  10. // @description:zh-TW nhentai show Tag in search page, and highlight block function
  11. // @description:ja nhentai show Tag in search page, and highlight block function
  12. // @include https://nhentai.net/*
  13. // @include https://en.nyahentai3.com/*
  14. // @include https://zh.nyahentai.co/*
  15. // @include https://ja.nyahentai.net/*
  16. // @include https://zh.nyahentai.pro/*
  17. // @include https://ja.nyahentai.org/g/*
  18. // @include https://zh.nyahentai4.com/g/*
  19. // @version 1.51
  20. // @grant GM_xmlhttpRequest
  21. // @grant GM_registerMenuCommand
  22. // @grant GM_setValue
  23. // @grant GM_getValue
  24. // @run-at document-start
  25. // @author zhuzemin
  26. // @license Mozilla Public License 2.0; http://www.mozilla.org/MPL/2.0/
  27. // @license CC Attribution-ShareAlike 4.0 International; http://creativecommons.org/licenses/by-sa/4.0/
  28. // @connect-src zh.nyahentai4.com
  29. // @connect-src ja.nyahentai.org
  30. // @connect-src zh.nyahentai.pro
  31. // @connect-src ja.nyahentai.net
  32. // @connect-src zh.nyahentai.co
  33. // @connect-src en.nyahentai3.com
  34. // @connect-src nhentai.net
  35. // ==/UserScript==
  36. var config = {
  37. 'debug': false
  38. }
  39. var debug = config.debug ? console.log.bind(console) : function () {
  40. };
  41.  
  42. // setting User Preferences
  43. function setUserPref(varName, defaultVal, menuText, promtText, sep){
  44. GM_registerMenuCommand(menuText, function() {
  45. var val = prompt(promtText, GM_getValue(varName, defaultVal));
  46. if (val === null) { return; } // end execution if clicked CANCEL
  47. // prepare string of variables separated by the separator
  48. if (sep && val){
  49. var pat1 = new RegExp('\\s*' + sep + '+\\s*', 'g'); // trim space/s around separator & trim repeated separator
  50. var pat2 = new RegExp('(?:^' + sep + '+|' + sep + '+$)', 'g'); // trim starting & trailing separator
  51. //val = val.replace(pat1, sep).replace(pat2, '');
  52. }
  53. //val = val.replace(/\s{2,}/g, ' ').trim(); // remove multiple spaces and trim
  54. GM_setValue(varName, val);
  55. // Apply changes (immediately if there are no existing highlights, or upon reload to clear the old ones)
  56. //if(!document.body.querySelector(".THmo")) THmo_doHighlight(document.body);
  57. //else location.reload();
  58. });
  59. }
  60.  
  61. // prepare UserPrefs
  62. setUserPref(
  63. 'highlights',
  64. 'chinese;',
  65. 'Set Highlight Tags',
  66. `Set highlights, split with ";". Example: "mmf threesome; chinese"`,
  67. ','
  68. );
  69. setUserPref(
  70. 'BlackList',
  71. 'english;',
  72. 'Set BlackList',
  73. `Set BlackList, split with ";". Example: "chinese; yaoi"`,
  74. ','
  75. );
  76.  
  77.  
  78. CreateStyle=function(){
  79. debug("Start: CreateStyle");
  80. var style=document.createElement("style");
  81. style.setAttribute("type","text/css");
  82. style.innerHTML=`
  83. .glowbox {
  84. background: #4c4c4c;
  85. //width: 400px;
  86. //margin: 40px 0 0 40px;
  87. //padding: 10px;
  88. -moz-box-shadow: 0 0 5px 5px #FFFF00;
  89. -webkit-box-shadow: 0 0 5px 5px #FFFF00;
  90. box-shadow: 0 0 5px 5px #FFFF00;
  91. }
  92. `;
  93. debug("Processing: CreateStyle");
  94. var head=document.querySelector("head");
  95. head.insertBefore(style,null);
  96. debug("End: CreateStyle");
  97. }
  98. class Gallery{
  99. constructor(href,other=null) {
  100. this.method = 'GET';
  101. this.url = href;
  102. this.headers = {
  103. 'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
  104. 'Accept': 'application/atom+xml,application/xml,text/xml',
  105. 'Referer': window.location.href,
  106. };
  107. this.charset = 'text/plain;charset=utf8';
  108. this.other=other;
  109. }
  110. }
  111. var BlackListLast=[];
  112. var highlightsLast=[];
  113. var DivCount;
  114. function HighlightTag(responseDetails,divs){
  115. //debug("HighlightTag");
  116. var dom;
  117. if(responseDetails!=null){
  118. var responseText=responseDetails.responseText;
  119. dom = new DOMParser().parseFromString(responseText, "text/html");
  120.  
  121. }
  122. var highlights;
  123. var BlackList;
  124. try{
  125. highlights=GM_getValue("highlights").trim().replace(/;$/,"").split(";");
  126. BlackList=GM_getValue("BlackList").trim().replace(/;$/,"").split(";");
  127. }catch(e){
  128. debug("Not set GM_Value.");
  129. }
  130. if (BlackList == undefined||BlackList.length ==0) {
  131. BlackList = [];
  132. }
  133. if (highlights == undefined||highlights.length ==0) {
  134. highlights = [];
  135. }
  136. debug("BlackList: " + BlackList);
  137. if(responseDetails!=null||JSON.stringify(BlackList)!=JSON.stringify(BlackListLast)||JSON.stringify(highlights)!=JSON.stringify(highlightsLast)){
  138.  
  139. var taglist;
  140. var NewDivs;
  141. if(responseDetails==null){
  142. NewDivs = divs;
  143. }
  144. else{
  145. NewDivs=[0];
  146. }
  147. //debug("NewDivs.length: "+NewDivs.length);
  148. for(var i=0;i<NewDivs.length;i++){
  149. var Break=false;
  150. var div;
  151. if(responseDetails!=null){
  152. div=divs[DivCount];
  153. taglist = dom.querySelector('#tags');
  154. }
  155. else{
  156. div=divs[i];
  157. taglist = div.querySelector('#tags');
  158.  
  159. }
  160. //debug(taglist);
  161. var links = taglist.querySelectorAll("a.tag");
  162. //debug(links);
  163. if(responseDetails!=null||JSON.stringify(BlackList)!=JSON.stringify(BlackListLast)){
  164. for (var link of links) {
  165. var tag = link.innerText.toLowerCase().match(/([\w\s]*)/)[1].trim();
  166. //debug("Tag: "+tag);
  167. for (var BlackWord of BlackList) {
  168. if (BlackWord.length > 1) {
  169. if (tag == BlackWord.trim()) {
  170. debug("BlackWord: " + link.innerText);
  171. div.className += " blacklisted";
  172. Break=true;
  173. break;
  174. }
  175. else if (link==links[links.length-1]&&BlackWord == BlackList[BlackList.length - 1]) {
  176. div.className = div.className.replace(" blacklisted", "");
  177. }
  178. }
  179. else{
  180. div.className = div.className.replace(" blacklisted", "");
  181.  
  182. }
  183. }
  184. if (Break) {
  185. break;
  186. }
  187. }
  188.  
  189. }
  190. if(responseDetails!=null||JSON.stringify(highlights)!=JSON.stringify(highlightsLast)){
  191. for (var link of links) {
  192. var tag = link.innerText.toLowerCase().match(/([\w\s]*)/)[1].trim();
  193. //debug("Tag: "+tag);
  194. for (var highlight of highlights) {
  195. if (highlight.length > 1) {
  196. //debug("Highlight: "+highlight);
  197. if (tag == highlight.trim()) {
  198. debug("highlight: " + link.innerText);
  199. link.className += " glowbox";
  200. break;
  201. }
  202. else if (highlight == highlights[highlights.length - 1]) {
  203. link.className = link.className.replace(" glowbox", "");
  204. }
  205. }
  206. else{
  207. link.className = link.className.replace(" glowbox", "");
  208.  
  209. }
  210. }
  211. }
  212.  
  213. }
  214.  
  215. if(responseDetails!=null) {
  216. var a = div.querySelector("a");
  217. a.replaceChild(taglist, a.querySelector("#tags"));
  218. DivCount++;
  219. }
  220. }
  221. if(responseDetails!=null) {
  222.  
  223. if (DivCount < divs.length) {
  224. MainWoker(divs);
  225. }
  226. }
  227. debug("BlackListLast: "+BlackListLast);
  228. debug("highlightsLast: "+highlightsLast);
  229. highlightsLast=highlights;
  230. BlackListLast=BlackList;
  231. }
  232.  
  233. }
  234.  
  235. function MainWoker(divs){
  236. debug("MainWoker");
  237. var div=divs[DivCount];
  238. div.style.maxHeight = "900px";
  239. div.style.height = "900px";
  240. var a = div.querySelector("a");
  241. var img = a.querySelector("img");
  242. var data_src=img.getAttribute("data-src");
  243. img.setAttribute("src",data_src);
  244. div.insertBefore(img, a);
  245. a.style.overflow = "auto";
  246. a.style.maxHeight = 900 - img.offsetHeight + "px";
  247. var caption = a.querySelector("div.caption");
  248. caption.style.position = "static";
  249. var taglist = document.createElement("section");
  250. taglist.setAttribute("id", "tags");
  251. a.insertBefore(taglist, null);
  252. var href = div.querySelector('a').href;
  253. //debug(href);
  254. var gallery = new Gallery(href);
  255. gallery.other=divs;
  256. request(gallery,HighlightTag);
  257.  
  258. }
  259. var init = function () {
  260. var LastDivNum=0;
  261. CreateStyle();
  262. DivCount=0;
  263. var html = document.querySelector('html');
  264. setInterval(function(){
  265. var divs = html.querySelectorAll('div.gallery');
  266. //debug("DivNum: "+divs.length);
  267. if(LastDivNum<divs.length) {
  268. html.style.height=divs.length/5*900+"px";
  269. debug("html.style.height: "+html.style.height);
  270. MainWoker(divs);
  271. }
  272. LastDivNum=divs.length;
  273. HighlightTag(null,divs);
  274. }, 2000)
  275. }
  276. function request(object,func) {
  277. var retries = 10;
  278. GM_xmlhttpRequest({
  279. method: object.method,
  280. url: object.url,
  281. headers: object.headers,
  282. overrideMimeType: object.charset,
  283. //synchronous: true
  284. onload: function (responseDetails) {
  285. if (responseDetails.status != 200) {
  286. // retry
  287. if (retries--) { // *** Recurse if we still have retries
  288. setTimeout(request,2000);
  289. return;
  290. }
  291. }
  292. //debug(responseDetails);
  293. //Dowork
  294. func(responseDetails,object.other);
  295. }
  296. })
  297. }
  298.  
  299. window.addEventListener('DOMContentLoaded', init);