Exhentai better experience

This script will bring a better experience for exhentai

  1. // ==UserScript==
  2. // @name Exhentai better experience
  3. // @namespace discuz
  4. // @description This script will bring a better experience for exhentai
  5. // @include http*://exhentai.org/*
  6. // @include http*://e-hentai.org/*
  7. // @require https://cdn.jsdelivr.net/npm/jquery@3.2.1/dist/jquery.min.js
  8. // @version 1.92
  9. // ==/UserScript==
  10.  
  11. // 默认收藏夹值,修改数字可更改默认收藏夹
  12. var default_favcat = 0
  13.  
  14. // gallery detail page
  15. if (/\/g\//.test(location.pathname)) {
  16. // enable tag link
  17. var xpath = "//div[@class='gt' or @class='gtl']/a[@onclick]";
  18. var nodes = document.evaluate(
  19. xpath,
  20. document,
  21. null,
  22. XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
  23. null
  24. );
  25. var node = null;
  26. var index = 0;
  27. while ((node = nodes.snapshotItem(index))) {
  28. node.removeAttribute("onclick");
  29. index++;
  30. }
  31.  
  32. var fav_url = popbase + "addfav"; // popbase is from exhentai js file
  33.  
  34. // fav operation without popup
  35. function fav_post() {
  36. if (
  37. $("#favoritelink").find("img").length
  38. ) {
  39. $.post(fav_url, {
  40. favcat: default_favcat,
  41. favnote: "",
  42. apply: "Add to Favorites",
  43. update: "1",
  44. }).done(function () {
  45. if (document.getElementById("favoritelink") != undefined) {
  46. document.getElementById("fav").innerHTML =
  47. '<div class="i" style="background-image:url(https://exhentai.org/img/fav.png); background-position:0px -2px; margin-left:10px" title="Favorites '+default_favcat+'"></div>';
  48. document.getElementById("favoritelink").innerHTML = "Favorites " + default_favcat;
  49. }
  50. });
  51. } else {
  52. $.post(fav_url, {
  53. favcat: "favdel",
  54. favnote: "",
  55. apply: "Apply Changes",
  56. update: "1",
  57. }).done(function () {
  58. if (document.getElementById("favoritelink") != undefined) {
  59. document.getElementById("fav").innerHTML = "";
  60. document.getElementById("favoritelink").innerHTML =
  61. '<img src="https://exhentai.org/img/mr.gif" /> Add to Favorites';
  62. }
  63. });
  64. }
  65. }
  66.  
  67. var gdf_div = document.querySelector("#gdf");
  68. gdf_div.removeAttribute("onclick");
  69. gdf_div.addEventListener("click", fav_post, false);
  70.  
  71. }
  72.  
  73. // general gallery pages
  74. if (/(\/g\/)|(watched)|(popular)|(favorites)|()/.test(location.pathname)) {
  75.  
  76. // global search
  77. var nb_div = document.querySelector("#nb");
  78.  
  79. function createElementFromHTML(htmlString) {
  80. var div = document.createElement("div");
  81. div.innerHTML = htmlString.trim();
  82. return div.firstChild;
  83. }
  84.  
  85. function global_search() {
  86. var s_content = document.getElementById("g_search");
  87. window.location.href = "/?f_search=" + s_content.value + "&advsearch=1&f_sname=on&f_stags=on&f_sh=on&f_spf=&f_spt="; //relative to domain
  88. }
  89.  
  90. var html_string =
  91. '<p class="nopm"><input type="text" id="g_search" name="g_search" placeholder="Search expunged galleries" size="50" maxlength="200"><input type="submit" id="g_search_btn" value="Apply Filter"></p>';
  92. var search_bar = createElementFromHTML(html_string);
  93. nb_div.appendChild(search_bar);
  94.  
  95. var g_search_btn = document.querySelector("#g_search_btn");
  96. g_search_btn.addEventListener("click", global_search, false);
  97. $("#g_search").keydown(function (e){
  98. if(e.keyCode == 13){
  99. global_search();}
  100. })
  101.  
  102. // global search only appears when hovering
  103. $("#nb").css("transition", "0.2s")
  104. var global_s_style = "<style>#nb:hover{max-height:47px} #nb { max-height: 47px};</style>";
  105. $("head").append(global_s_style);
  106.  
  107. // quick fav operation, show favcat num
  108. function fav_post_pages() {
  109. $('[id*="posted_"]').each(function() {
  110. var date_div = $(this);
  111. var favcat_tag_id = date_div.attr("id") + "_favcat"
  112. var favcat_btn_id = date_div.attr("id")+ "_favbtn"
  113. var fav_btn_content = `
  114. <div class="cs ct2" id = ${favcat_btn_id} style="
  115. position: absolute;
  116. top: -2px;
  117. left: -36px;
  118. width: 30px;
  119. height: 18px;
  120. line-height: 18px;
  121. background: #1c563dcc;
  122. border-color: #671c17;
  123. ">` + "收藏" + "</div>";
  124. // If favoriite, change style, show favcat num
  125. if (date_div.attr("title")){
  126. var title = date_div.attr("title")
  127. var favcat = title.slice(-1)
  128. var style = date_div.attr("style")
  129. var border_color = date_div.css("border-color")
  130. // var background_color = date_div.css("background-color")
  131. var background_color = border_color
  132. var style_for_fav1 = "border-color:#e4ee2c;background-color:rgb(70 141 220 / 85%)"
  133. if (favcat == "0"){background_color="#309427"; border_color="#671c17"; style=style_for_fav1}
  134. date_div.attr("style", style);
  135. date_div.attr("title", title);
  136. var favcat_tag_content = `
  137. <div class="cs ct2" id = ${favcat_tag_id} style="
  138. position: absolute;
  139. top: -2px;
  140. left: -36px;
  141. width: 30px;
  142. height: 18px;
  143. line-height: 18px;
  144. background: ${background_color};
  145. border-color: ${border_color};
  146. ">fav` + favcat + "</div>";
  147. date_div.append(favcat_tag_content)
  148. // debugger;
  149. } else {
  150. date_div.append(fav_btn_content)
  151. }
  152. var popUpStr = $(this).attr("onclick");
  153. var fav_url = popUpStr.slice(7, -10);
  154. $(this).removeAttr("onclick");
  155. $(this).click(function(){
  156. if (! $(this).attr("title")) {
  157. $.post(fav_url, {
  158. favcat: default_favcat,
  159. favnote: "",
  160. apply: "Add to Favorites",
  161. update: "1",
  162. }).done(function (data) {
  163. // debugger;
  164. var patt_borderColor = /borderColor="(.*?)"/
  165. var borderColor = patt_borderColor.exec(data)[1];
  166. var patt_backgroundColor = /backgroundColor="(.*?)"/
  167. // var backgroundColor = patt_backgroundColor.exec(data)[1];
  168. var backgroundColor = borderColor
  169. var patt_title = /title="(.*?)"/
  170. var title = patt_title.exec(data)[1];
  171. var favcat = title.slice(-1);
  172. var style_for_fav1 = "border-color:#e4ee2c;background-color:rgb(70 141 220 / 85%)"
  173.  
  174. if (favcat == "0"){backgroundColor="#309427"; borderColor="#671c17"; style=style_for_fav1};
  175.  
  176. date_div.attr("style", style_for_fav1);
  177. date_div.attr("title", title)
  178. var favcat_tag_content = `
  179. <div class="cs ct2" id = ${favcat_tag_id} style="
  180. position: absolute;
  181. top: -2px;
  182. left: -36px;
  183. width: 30px;
  184. height: 18px;
  185. line-height: 18px;
  186. background: ${backgroundColor};
  187. border-color: ${borderColor};
  188. ">fav` + default_favcat + "</div>";
  189. date_div.append(favcat_tag_content);
  190. $("#"+favcat_btn_id).remove();
  191. // debugger;
  192. })
  193. } else {
  194. $.post(fav_url, {
  195. favcat: "favdel",
  196. favnote: "",
  197. apply: "Apply Changes",
  198. update: "1",
  199. }).done(function () {
  200. // debugger;
  201. date_div.attr("style", "");
  202. date_div.attr("title", "");
  203. $("#"+favcat_tag_id).remove();
  204. date_div.append(fav_btn_content);
  205. // debugger;
  206. });
  207. }
  208. }
  209. )
  210. }
  211. )
  212. };
  213. fav_post_pages()
  214. }
  215.  
  216. // Chinsese translation highlight
  217. var title_path = "//div[contains(@class, 'gl4t')]";
  218. var titles = document.evaluate(
  219. title_path,
  220. document,
  221. null,
  222. XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
  223. null
  224. );
  225. var title = null;
  226. var idx = 0;
  227. while ((title = titles.snapshotItem(idx))) {
  228. var titlePatt = /中文|中国|Chinese|中國/
  229. var text = title.innerText;
  230. if (titlePatt.test(text)) {
  231. title.setAttribute("style", "color: #ff8d8d; font-weight: bold;");}
  232. idx++;
  233. }