E-H Visited

Marks visited galleries.

2019-04-18 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

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