E-H Visited

Marks visited galleries.

Ekde 2019/07/30. Vidu La ĝisdata versio.

  1. // ==UserScript==
  2. // @name E-H Visited
  3. // @description Marks visited galleries.
  4. // @author Hen-Tie
  5. // @homepage https://hen-tie.tumblr.com/
  6. // @namespace https://greasyfork.org/en/users/8336
  7. // @include /https?:\/\/(e-|ex)hentai\.org\/.*/
  8. // @exclude https://e-hentai.org/toplist.php?tl=*
  9. // @require https://code.jquery.com/jquery-3.3.1.min.js
  10. // @require https://greasyfork.org/scripts/381436-gmshim/code/GMshim.js
  11. // @grant GM_setValue
  12. // @grant GM_getValue
  13. // @icon https://i.imgur.com/pMMVGRx.png
  14. // @version 3.13
  15. // ==/UserScript==
  16. // 2019 fork of exvisited (sleazyfork.org/en/scripts/22270)
  17. // bug reports: greasyfork.org/en/forum/post/discussion?script=377945
  18.  
  19. /*════════════════════╗
  20. ║ configuration ║
  21. ╚════════════════════*/
  22. // true: adds column with eye icon to viewed galleries
  23. // false: truly minimal, hover on gallery title for timestamp
  24. var minimalAddColumn = true;
  25. // true: (minimalAddColumn must also be true) replace eye icon with timestamp text
  26. var minimalShowText = false;
  27. /*═══════════════════*/
  28.  
  29. var storageName = "ehVisited";
  30. var sto = localStorage.getItem(storageName) ? localStorage.getItem(storageName) : '{"data":{}}';
  31. var vis = JSON.parse(sto);
  32. var spl = document.URL.split("/");
  33. var d1 = spl[3];
  34. var d2 = spl[4];
  35. var d3 = spl[5];
  36. var css = GM_getValue("css") ? GM_getValue("css") : "box-shadow: inset 0 0 0 500px rgba(2, 129, 255, .2) !important;"; //default highlight colour
  37. var postInfiniteScroll = 0;
  38. var observer = new MutationObserver(function () {
  39. // track galleries opened on-site
  40. $('a').on('mouseup', function () {
  41. var spl = this.href.split("/");
  42. var d1 = spl[3];
  43. var d2 = spl[4];
  44. var d3 = spl[5];
  45.  
  46. if (d1 == "g") {
  47. var c = d2 + "." + d3;
  48. vis = JSON.parse(localStorage.getItem(storageName));
  49. vis.data[c] = Date.now();
  50. localStorage.setItem(storageName, JSON.stringify(vis));
  51. }
  52. });
  53. postInfiniteScroll = 1;
  54. ehvTimestamp();
  55. });
  56.  
  57. vis.data = !vis.data ? Array() : vis.data;
  58.  
  59. // convert keywords to CSS
  60. if (css === "initial") {
  61. css = "box-shadow: inset 0 0 0 500px rgba(2, 129, 255, .2) !important;";
  62. } else if (css === "none") {
  63. css = "";
  64. }
  65.  
  66. // localstorage unsupported warning
  67. if (typeof (Storage) == "undefined") {
  68. alert("E-H Visited:\nYour browser does not support localStorage :(");
  69. }
  70.  
  71. // get time difference in words
  72. function timeDifference(current, previous, abbreviate) {
  73. var msPerMinute = 60 * 1000;
  74. var msPerHour = msPerMinute * 60;
  75. var msPerDay = msPerHour * 24;
  76. var msPerMonth = msPerDay * 30;
  77. var msPerYear = msPerDay * 365;
  78. var elapsed = current - previous;
  79.  
  80. if (elapsed < msPerMinute) {
  81. return Math.round(elapsed / 1000) + ((typeof abbreviate !== 'undefined') ? '&nbsp;sec' : ' seconds ago');
  82. } else if (elapsed < msPerHour) {
  83. return Math.round(elapsed / msPerMinute) + ((typeof abbreviate !== 'undefined') ? '&nbsp;min' : ' minutes ago');
  84. } else if (elapsed < msPerDay) {
  85. return Math.round(elapsed / msPerHour) + ((typeof abbreviate !== 'undefined') ? '&nbsp;hrs' : ' hours ago');
  86. } else if (elapsed < msPerMonth) {
  87. return Math.round(elapsed / msPerDay) + ((typeof abbreviate !== 'undefined') ? '&nbsp;days' : ' days ago');
  88. } else if (elapsed < msPerYear) {
  89. return Math.round(elapsed / msPerMonth) + ((typeof abbreviate !== 'undefined') ? '&nbsp;mos' : ' months ago');
  90. } else {
  91. return Math.round(elapsed / msPerYear) + ((typeof abbreviate !== 'undefined') ? '&nbsp;yrs' : ' years ago');
  92. }
  93. }
  94.  
  95. function ehvExport(message) {
  96. var data = "";
  97. for (var d in vis.data) {
  98. if (vis.data.hasOwnProperty(d)) {
  99. data += d + ":" + vis.data[d] + ";";
  100. }
  101. }
  102. if ($('.ehv-exported-data').length) {
  103. $('.ehv-exported-data').remove();
  104. }
  105. GM_setValue("archive", data);
  106. console.log("E-H Visited data:");
  107. console.log(GM_getValue("archive"));
  108. $('.ehv-controls').append('<ehv class="ehv-exported-data"><strong>' + message + '</strong><textarea class="ehv-exported-data-text">' + data + '</textarea><a class="ehv-exported-data-button cs" href="data:text,'+data+'" download="E-H Visited Data">Download Text File</a></ehv>');
  109. }
  110.  
  111. function ehvTimestamp() {
  112. observer.disconnect();
  113. var list = $("table.itg>tbody>tr").has('.glhide, .gldown, th'); //present only in list views
  114. var thumb = $(".itg .gl1t"); //present only in thumbnail view
  115. var gid;
  116. var d;
  117. var galleryId;
  118. var onFavs = 0;
  119.  
  120. // check current view
  121. if (list.length > 0) {
  122. if ($('.gl1e').length) { //extended
  123. if ($('h1').text() === "Favorites") {
  124. onFavs = 1;
  125. }
  126. for (var i = 0; i < list.length; i++) {
  127. gid = $(list[i]).find(".gl1e a").attr("href").split("/");
  128. galleryId = gid[4] + "." + gid[5];
  129. if ($(list[i])[0].children.length === 2 && onFavs) {
  130. $(list[i]).append('<td></td>');
  131. }
  132. if (vis.data[galleryId] != undefined) {
  133. d = new Date(vis.data[galleryId]);
  134. if (!$(list[i]).hasClass('ehv-visited')) {
  135. $(list[i]).addClass("ehv-visited");
  136. //check for fav pages
  137. if ($(list[i]).find('.gl3e').children('div').length >= 7) { //date favourited div is present
  138. $(list[i]).find('.gl3e > div:last-child').append("<br><ehv class='ehv-extended-favs'>\uD83D\uDC41" + timeDifference(Date.now(), vis.data[galleryId]) + "<br>" + d.getFullYear().toString() + "\u2011" + (d.getMonth() + 1) + "\u2011" + d.getDate() + " (" + d.getHours().toString().padStart(2, '0') + ":" + d.getMinutes().toString().padStart(2, '0') + ")</ehv>");
  139. } else {
  140. $(list[i]).find('.gl3e').append("<ehv class='ehv-extended'>\uD83D\uDC41" + timeDifference(Date.now(), vis.data[galleryId]) + "<br>" + d.getFullYear().toString() + "\u2011" + (d.getMonth() + 1) + "\u2011" + d.getDate() + " (" + d.getHours().toString().padStart(2, '0') + ":" + d.getMinutes().toString().padStart(2, '0') + ")</ehv>");
  141. }
  142. }
  143. }
  144. }
  145. } else if ($('.gl1c').length) { //compact
  146. var borderColour = $('.gl1c').first().css('border-top-color'); //border colour different between domains
  147. if (!postInfiniteScroll) {
  148. $('table.itg tbody>tr:first-child th:nth-child(2)').after('<th>Visited</th>');
  149. }
  150. if ($('h1').text() === "Favorites") {
  151. onFavs = 1;
  152. }
  153. for (i = 1; i < list.length; i++) {
  154. gid = $(list[i]).find(".glname a").attr("href").split("/");
  155. galleryId = gid[4] + "." + gid[5];
  156. if ($(list[i])[0].children.length === 4 || $(list[i])[0].children.length === 5 && onFavs) {
  157. if ($(list[i])[0].children.length === 4 && onFavs) {
  158. $(list[i]).append('<td></td>');
  159. }
  160. if (vis.data[galleryId] != undefined) {
  161. d = new Date(vis.data[galleryId]);
  162. $(list[i]).addClass("ehv-visited");
  163. $(list[i]).children('.gl2c').after('<td class="ehv-compact" style="border-color:' + borderColour + ';"><ehv>' + timeDifference(Date.now(), vis.data[galleryId], true) + "<br>(" + d.getHours().toString().padStart(2, '0') + ":" + d.getMinutes().toString().padStart(2, '0') + ')<br>' + d.getFullYear().toString().substr(2) + "\u2011" + (d.getMonth() + 1) + "\u2011" + d.getDate() + '</ehv></td>');
  164. } else {
  165. $(list[i]).children('.gl2c').after('<td class="ehv-compact" style="border-color:' + borderColour + ';"></td>');
  166. }
  167. }
  168. }
  169. } else { //minimal
  170. if (minimalAddColumn && !postInfiniteScroll) {
  171. $('table.itg tbody>tr:first-child th:nth-child(2)').after('<th title="E-H Visited: Hover for timestamps">\uD83D\uDC41</th>');
  172. }
  173. if ($('h1').text() === "Favorites") {
  174. onFavs = 1;
  175. }
  176. for (i = 1; i < list.length; i++) {
  177. gid = $(list[i]).find(".glname a").attr("href").split("/");
  178. galleryId = gid[4] + "." + gid[5];
  179. if ($(list[i])[0].children.length === 6 || $(list[i])[0].children.length === 7 && onFavs) {
  180. if ($(list[i])[0].children.length === 6 && onFavs) {
  181. $(list[i]).append('<td></td>');
  182. }
  183. if (minimalAddColumn) { //append viewed column
  184. if (vis.data[galleryId] != undefined) {
  185. d = new Date(vis.data[galleryId]);
  186. $(list[i]).addClass("ehv-visited");
  187. $(list[i]).children('.glname')[0].setAttribute("title", 'E-H Visited: ' + timeDifference(Date.now(), vis.data[galleryId]) + " (" + d.getHours().toString().padStart(2, '0') + ":" + d.getMinutes().toString().padStart(2, '0') + ") " + d.getFullYear().toString() + "\u2011" + (d.getMonth() + 1) + "\u2011" + d.getDate());
  188. if (minimalShowText) { //show text in appended column
  189. $(list[i]).children('.gl2m').after('<td class="ehv-minimal-text"><ehv>' + timeDifference(Date.now(), vis.data[galleryId], true) + "<br>(" + d.getHours().toString().padStart(2, '0') + ":" + d.getMinutes().toString().padStart(2, '0') + ')<br>' + d.getFullYear().toString().substr(2) + "\u2011" + (d.getMonth() + 1) + "\u2011" + d.getDate() + '</ehv></td>');
  190. } else { //show icon in appended column
  191. $(list[i]).children('.gl2m').after('<td class="ehv-minimal" title="E-H Visited: ' + timeDifference(Date.now(), vis.data[galleryId]) + " (" + d.getHours().toString().padStart(2, '0') + ":" + d.getMinutes().toString().padStart(2, '0') + ") " + d.getFullYear().toString() + "\u2011" + (d.getMonth() + 1) + "\u2011" + d.getDate() + '"><ehv>\uD83D\uDC41</ehv></td>');
  192. }
  193. } else { //not viewed
  194. $(list[i]).children('.gl2m').after('<td class="ehv-minimal"></td>');
  195. }
  196. } else { //append nothing, highlight only
  197. if (vis.data[galleryId] != undefined) {
  198. d = new Date(vis.data[galleryId]);
  199. $(list[i]).addClass("ehv-visited");
  200. $(list[i]).children('.glname')[0].setAttribute("title", 'E-H Visited: ' + timeDifference(Date.now(), vis.data[galleryId]) + " (" + d.getHours().toString().padStart(2, '0') + ":" + d.getMinutes().toString().padStart(2, '0') + ") " + d.getFullYear().toString() + "\u2011" + (d.getMonth() + 1) + "\u2011" + d.getDate());
  201. }
  202. }
  203. }
  204. }
  205. }
  206. } else if (thumb.length > 0) { //thumbnail
  207. for (i = 0; i < thumb.length; i++) {
  208. gid = $(thumb[i]).find(".gl3t a").attr("href").split("/");
  209. galleryId = gid[4] + "." + gid[5];
  210. if (!$(thumb[i]).hasClass('ehv-visited')) {
  211. if (vis.data[galleryId] != undefined) {
  212. d = new Date(vis.data[galleryId]);
  213. $(thumb[i]).addClass("ehv-visited");
  214. $(thumb[i]).children('.gl5t').after("<ehv class='ehv-thumbnail'>\uD83D\uDC41" + timeDifference(Date.now(), vis.data[galleryId]) + " (" + d.getHours().toString().padStart(2, '0') + ":" + d.getMinutes().toString().padStart(2, '0') + ") " + d.getFullYear().toString() + "\u2011" + (d.getMonth() + 1) + "\u2011" + d.getDate() + "</ehv>");
  215. }
  216. }
  217. }
  218. } else {
  219. console.log("E-H Visited:\n Something is wrong, I don't know what view mode this is!\n Bug reports: greasyfork.org/en/forum/post/discussion?script=377945");
  220. }
  221. observer.observe($('.itg').get(0), {
  222. childList: true,
  223. subtree: true
  224. });
  225. }
  226.  
  227. $(function () {
  228. var d = JSON.parse('{"data":{}}');
  229.  
  230. // track galleries opened on-site
  231. $('a').on('mouseup', function () {
  232. var spl = this.href.split("/");
  233. var d1 = spl[3];
  234. var d2 = spl[4];
  235. var d3 = spl[5];
  236.  
  237. if (d1 == "g") {
  238. var c = d2 + "." + d3;
  239. vis = JSON.parse(localStorage.getItem(storageName));
  240. vis.data[c] = Date.now();
  241. localStorage.setItem(storageName, JSON.stringify(vis));
  242. }
  243. });
  244.  
  245. // track galleries opened indirectly (offsite link, shortcut file, context menu, bookmark, etc.)
  246. $(window).one('click scroll', function () {
  247. if (d1 == "g") {
  248. var c = d2 + "." + d3;
  249. vis = JSON.parse(localStorage.getItem(storageName));
  250. vis.data[c] = Date.now();
  251. localStorage.setItem(storageName, JSON.stringify(vis));
  252. }
  253. });
  254. if (/[?#ft]/.test(d1.substr(0, 1)) || /^watched/.test(d1) || !d1) {
  255. var len = Object.keys(vis.data).length;
  256. var ehvClearConfirm = 0;
  257. var controlsHTML = "<ehv class='ehv-controls'>Galleries visited: " + len + " (<a href='javascript:;' class='ehv-import'>Import</a> / <a href='javascript:;' class='ehv-export'>Export</a> / <a href='javascript:;' class='ehv-merge'>Merge</a> / <a href='javascript:;' class='ehv-clear'>Clear</a> / <a href='javascript:;' class='ehv-css'>CSS</a>)</ehv>";
  258. if ($('body > img[src="https://exhentai.org/img/kokomade.jpg"]').length > 0) {
  259. $('body').prepend(controlsHTML);
  260. } else {
  261. $('#toppane').append(controlsHTML);
  262. }
  263. $(".ehv-import").click(function () {
  264. var c = prompt("E-H Visited:\nPaste here to import, and overwrite current data.");
  265. if (c) {
  266. var sp = c.split(";");
  267. //sp = sp.filter(Boolean);
  268. for (var k in sp) {
  269. if (sp.hasOwnProperty(k)) {
  270. var s = sp[k].split(":");
  271. d.data[s[0]] = parseInt(s[1]);
  272. }
  273. }
  274. localStorage.setItem(storageName, JSON.stringify(d));
  275. console.log(d);
  276. alert("E-H Visited:\nImported " + Object.keys(d.data).length + " entries.");
  277. location.reload();
  278. }
  279. });
  280.  
  281. $(".ehv-export").click(function () {
  282. ehvExport('Exported entries:');
  283. });
  284.  
  285. $(".ehv-css").click(function () {
  286. var c = prompt("E-H Visited:\nThis CSS is applied to visited galleries.\n('initial' to reset, or 'none' for no styling)", css);
  287. if (c) {
  288. GM_setValue("css", c);
  289. location.reload();
  290. }
  291. });
  292.  
  293. $(".ehv-merge").click(function () {
  294. var c = prompt("E-H Visited:\nPaste here to import, and merge with current data.");
  295. if (c) {
  296. var sp = c.split(";");
  297. sp = sp.filter(Boolean);
  298. for (var k in sp) {
  299. if (sp.hasOwnProperty(k)) {
  300. var s = sp[k].split(":");
  301. d.data[s[0]] = parseInt(s[1]);
  302. }
  303. }
  304. for (var i = 0; i < Object.keys(vis.data).length; i++) {
  305. d.data[Object.keys(vis.data)[i]] = vis.data[Object.keys(vis.data)[i]];
  306. }
  307. alert("E-H Visited\nMerged data, " + Object.keys(d.data).length + " unique entries.");
  308. localStorage.setItem(storageName, JSON.stringify(d));
  309. location.reload();
  310. }
  311. });
  312.  
  313. $(".ehv-clear").click(function () {
  314. if (!ehvClearConfirm) {
  315. ehvClearConfirm = 1;
  316. $('.ehv-clear').append(': Are you sure?');
  317. ehvExport('Backup your current data:');
  318. } else {
  319. alert("E-H Visited:\nCleared all entries.");
  320. localStorage.removeItem(storageName);
  321. location.reload();
  322. }
  323. });
  324.  
  325. // append icon friendly fonts to the calculated font stack
  326. var inheritFonts = $('body').css('font-family') + ', "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", symbola';
  327. $(`<style data-jqstyle='ehVisited'>
  328. ehv { font-family:` + inheritFonts + ` }
  329. .gl2c { width: 115px; }
  330. .ehv-visited .gl3e { min-height: 206px; }
  331. .ehv-visited .gl4e { min-height: 264px !important; }
  332. .ehv-exported-data { display: block; }
  333. .ehv-exported-data-text { display: block; margin: 0 auto; height: 5em; width: 50vw; padding: .25em; }
  334. .ehv-exported-data-button { padding: 0 1em; width:unset; text-decoration:none; background:#777777; margin-top:.5em; }
  335. .ehv-minimal-text { text-align: center; display: block; }
  336. .ehv-compact { border-style: solid; border-width: 1px 0; text-align: center; }
  337. .ehv-extended { width: 120px; position: absolute; left: 3px; top: 172px; text-align: center; font-size: 8pt; line-height: 1.5; }
  338. .ehv-extended-favs { padding: 3px 1px; display: block; line-height: 1.5; }
  339. .ehv-thumbnail { display: block; text-align: center; margin: 3px 0 5px; line-height: 12px; }
  340. .ehv-controls { padding: 3px 1px; text-align: center; display: block; }
  341. table.itg > tbody > tr.ehv-visited, .gl1t.ehv-visited { ` + css + ` }
  342. </style>`).appendTo("head");
  343.  
  344. ehvTimestamp();
  345. }
  346. });