Danbooru Ajax Interface

New interface to search images on Booru-style sites.

Per 28-04-2018. Zie de nieuwste versie.

  1. // ==UserScript==
  2. // @name Danbooru Ajax Interface
  3. // @namespace http://danbooru.donmai.us
  4. // @description New interface to search images on Booru-style sites.
  5. // @match *://danbooru.donmai.us/
  6. // @match *://danbooru.donmai.us/#*
  7. // @match *://*.gelbooru.com/
  8. // @match *://*.gelbooru.com/#*
  9. // @match *://konachan.com/
  10. // @match *://konachan.com/#*
  11. // @match *://konachan.net/
  12. // @match *://konachan.net/#*
  13. // @match *://yande.re/post
  14. // @match *://yande.re/post#*
  15. // @version 4.669
  16. // @grant GM_deleteValue
  17. // ==/UserScript==
  18.  
  19. // NEW SITES CAN BE ADDED NOW
  20. var sites = [
  21. {
  22. name : "Danbooru", // Site name
  23. site : "donmai\.us", // Regular expression check on current url
  24. post : "/posts.xml", // Relative url to xml post API
  25. note : "/notes.xml", // Relative url to xml note API
  26. list : "/posts/", // Relative url to post listing
  27. page : "/posts/", // Relative url to post page
  28. query : function(tags, images, page, postid) { // Query passed to API
  29. return (postid ? "?limit=99999&search[is_active]=true&search[post_id]=" + postid : "?tags=" + tags + (page ? "&page=" + page + "&limit=" + images : ""));
  30. }
  31. },
  32. {
  33. name : "Gelbooru",
  34. site : "(www\.)?gelbooru\.",
  35. post : "/index.php?page=dapi&s=post&q=index",
  36. note : "/index.php?page=dapi&s=note&q=index&post_id=",
  37. list : "/index.php?page=post&s=list",
  38. page : "/index.php?page=post&s=view&id=",
  39. query : function(tags, images, page, postid) {
  40. return (postid ? postid : "&tags=" + tags + "&limit=" + images + "&pid=" + (page - 1));
  41. }
  42. },
  43. {
  44. name : "Konachan",
  45. site : "konachan\.",
  46. post : "/post.xml",
  47. note : "/note.xml?post_id=",
  48. list : "/post/",
  49. page : "/post/show/",
  50. query : function(tags, images, page, postid) {
  51. return (postid ? postid : "?tags=" + tags + "&limit=" + images + "&page=" + page);
  52. }
  53. },
  54. {
  55. name : "Yande.re",
  56. site : "yande\.re",
  57. post : "/post.xml",
  58. note : "/note.xml?post_id=",
  59. list : "/post/",
  60. page : "/post/show/",
  61. query : function(tags, images, page, postid) {
  62. return (postid ? postid : "?tags=" + tags + "&limit=" + images + "&page=" + page);
  63. }
  64. }
  65. ];
  66.  
  67. // CONSTANTS
  68. const ratio = ((1 + Math.sqrt(5)) / 2);
  69. const d = document;
  70. const cacheExpires = 1000 * 60 * 60 * 24 * 7; // cache expires in 7 days : (ms * s * m * h * d)
  71.  
  72. // PAGE CLEANING
  73. while(d.documentElement.firstChild)
  74. d.documentElement.removeChild(d.documentElement.firstChild);
  75.  
  76. // IMPORTANT VARIABLES
  77. var booru, storage, requestPost, requestNote, requestCount, requestCache, requestTag, reqTagTimer, tagTimer, noteclearTimer;
  78. var tags = "", images = 20, page = 1, rating = "s", sampleRate = 1, blacklist = "spoilers \nguro \nscat ";
  79. var cacheHide = [], cacheID = [], cacheTags = [];
  80. var domParser = new DOMParser();
  81. var xsltText = "<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:template match='*'><xsl:copy><xsl:for-each select='*|@*'><xsl:copy><xsl:for-each select='*|@*'><xsl:attribute name='{name()}'><xsl:value-of select='.|text()'/><xsl:if test='@nil'>false</xsl:if></xsl:attribute></xsl:for-each></xsl:copy></xsl:for-each></xsl:copy></xsl:template></xsl:stylesheet>";
  82.  
  83. for(var i = 0; i < sites.length; i++)
  84. if(new RegExp(sites[i].site).test(window.location.hostname))
  85. booru = sites[i];
  86.  
  87. if(checkStorage()) {
  88. getStorage();
  89. window.addEventListener("unload", setStorage); // save last search when page is changed/closed
  90. }
  91.  
  92. // SCRIPT STARTS HERE
  93. d.documentElement.appendChild(d.createElement("HEAD"));
  94. d.documentElement.appendChild(d.createElement("BODY"));
  95. d.documentElement.firstChild.appendChild(title = d.createElement("TITLE"));
  96. title.appendChild(d.createTextNode(booru.name));
  97.  
  98. var fa = d.createElement("LINK");
  99. fa.setAttribute("rel", "stylesheet");
  100. fa.setAttribute("href", "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css");
  101. d.head.appendChild(fa);
  102.  
  103. var dai_css = d.createElement("STYLE");
  104. dai_css.setAttribute("type", "text/css");
  105. dai_css.appendChild(d.createTextNode("body { margin: 4px; } a { text-decoration: none; color: #0000EE; } img { border: 0px; } .thumb { border: 1px solid WhiteSmoke; min-width: 150px; min-height: 150px; max-width: 150px; max-height: 150px; margin: -1px 0px 0px -1px; padding: 1px; display: flex; justify-content: center; align-items: center; font-size: small; overflow: hidden; } .yellow { background-color: LightYellow; } .red { background-color: MistyRose; } .trans { border: 1px solid Black; background-color: LightYellow; position: absolute; } *:not(body) { transition: all 2s ease-in-out 0s; }"));
  106.  
  107. d.body.appendChild(searchTable = d.createElement("TABLE"));
  108. searchTable.appendChild(searchTr = d.createElement("TR"));
  109. searchTr.setAttribute("style", "vertical-align: top;");
  110. searchTr.appendChild(searchTd = d.createElement("TD"));
  111. searchTd.setAttribute("style", "text-align: center;");
  112.  
  113. searchForm = d.createElement("FORM");
  114. searchForm.addEventListener("submit", function(event) {
  115. tags = aTags.value;
  116. page = Math.max(1, parseInt(aPage.value, 10));
  117. images = Math.max(1, Math.min(parseInt(aImages.value, 10), 200));
  118. rating = (aRS.checked ? "s" : "") + (aRQ.checked ? "q" : "") + (aRE.checked ? "e" : "") + (aFIT.checked ? "f" : "") + (aSD && aSD.checked ? "d" : "");
  119. search(tags, page);
  120. event.preventDefault();
  121. }, false);
  122.  
  123. searchForm.appendChild(aLink = d.createElement("A"));
  124. aLink.setAttribute("href", booru.list);
  125. aLink.setAttribute("tabindex", "1");
  126. aLink.appendChild(d.createTextNode(booru.name));
  127.  
  128. searchForm.appendChild(d.createElement("BR"));
  129.  
  130. searchForm.appendChild(aTags = d.createElement("INPUT"));
  131. aTags.setAttribute("type", "text");
  132. aTags.setAttribute("style", "width: 80%");
  133. aTags.setAttribute("value", tags);
  134. aTags.setAttribute("tabindex", "2");
  135. aTags.setAttribute("list", "autocomplete");
  136. aTags.addEventListener("change", function(event) {
  137. aPage.value = 1;
  138. }, false);
  139.  
  140. searchForm.appendChild(aDatalist = d.createElement("DATALIST"));
  141. aDatalist.setAttribute("id", "autocomplete");
  142. if(cacheTags.length == 0) {
  143. popularTags = "touhou kantai_collection idolmaster vocaloid fate_(series) mahou_shoujo_madoka_magica".split(" ").reverse();
  144. for(var i in popularTags) {
  145. cacheTags.push({ tag : popularTags[i], expires : Date.now() + cacheExpires });
  146. }
  147. }
  148. for(var i = cacheTags.length - 1; i >= Math.max(cacheTags.length - 6, 0); i--) { // last 6 added values first
  149. var tagoption = d.createElement("OPTION");
  150. tagoption.setAttribute("value", cacheTags[i].tag + " ");
  151. aDatalist.appendChild(tagoption);
  152. }
  153.  
  154. aTags.addEventListener("input", function(e) {
  155. clearTimeout(tagTimer);
  156. clearTimeout(reqTagTimer);
  157. if(requestTag)
  158. requestTag.abort();
  159. tagTimer = setTimeout(function() {
  160. lookuptag = aTags.value.split(" ").pop();
  161. if(!lookuptag)
  162. return;
  163. taglist = aTags.value.split(" ");
  164. taglist.pop();
  165. taglist = taglist.join(" ");
  166. if(taglist.length > 0)
  167. taglist += " ";
  168.  
  169. while(aDatalist.hasChildNodes())
  170. aDatalist.removeChild(aDatalist.firstChild);
  171. aDL = d.createElement("DATALIST"); // adding new datalist to main datalist updates the main list contents
  172. for(var a = cacheTags.length - 1; a >= 0 && aDL.childNodes.length < 6; a--) {
  173. if(cacheTags[a] && cacheTags[a].tag.toLowerCase().startsWith(lookuptag.toLowerCase())) {
  174. aOptionTag = d.createElement("OPTION");
  175. aOptionTag.setAttribute("value", cacheTags[a].tag ? taglist + cacheTags[a].tag + " " : "");
  176. aDL.appendChild(aOptionTag);
  177. }
  178. }
  179. aDatalist.appendChild(aDL);
  180. aTags.focus();
  181. }, 10);
  182. if(booru.name != "Danbooru")
  183. return;
  184. reqTagTimer = setTimeout(function() {
  185. lookuptag = aTags.value.split(" ").pop();
  186. if(!lookuptag)
  187. return;
  188. requestTag = xmlhttpRequest({
  189. method : "GET",
  190. url : window.location.origin + "/tags.xml?search[hide_empty]=yes&search[order]=count&search[name_matches]=" + lookuptag + "*",
  191. headers : {
  192. "Accept" : "application/xml"
  193. },
  194. overrideMimeType : "application/xml; charset=utf-8",
  195. onload : function(response) {
  196. xmldoc = domParser.parseFromString(response.responseText, "application/xml");
  197. atags = xmldoc.evaluate("tags/tag/name", xmldoc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  198.  
  199. taglist = aTags.value.split(" ");
  200. taglist.pop();
  201. taglist = taglist.join(" ");
  202. if(taglist.length > 0)
  203. taglist += " ";
  204. if(atags.snapshotItem(0))
  205. while(aDatalist.hasChildNodes())
  206. aDatalist.removeChild(aDatalist.firstChild);
  207. aDL = d.createElement("DATALIST"); // adding new datalist to main datalist updates the main list contents
  208. for(var a = 0; a < 6; a++) {
  209. atag = atags.snapshotItem(a);
  210. aOptionTag = d.createElement("OPTION");
  211. aOptionTag.setAttribute("value", atag ? taglist + atag.textContent + " " : "");
  212. aDL.appendChild(aOptionTag);
  213. }
  214. aDatalist.appendChild(aDL);
  215. aTags.focus();
  216. }
  217. });
  218. }, 3000);
  219. }, false);
  220.  
  221. searchForm.appendChild(d.createTextNode(" "));
  222. searchForm.appendChild(aBlacklink = d.createElement("A"));
  223. aBlacklink.setAttribute("href", "#");
  224. aBlacklink.setAttribute("style", "color: red; float: right");
  225. aBlacklink.setAttribute("title", "Blacklist");
  226. aBlacklink.appendChild(d.createTextNode("[B]"));
  227. aBlacklink.addEventListener("click", function(e) {
  228. if(aBlacklist.style.getPropertyValue("opacity") == "0") {
  229. aBlacklist.style.setProperty("height", "15em", "");
  230. aBlacklist.style.setProperty("opacity", "1", "");
  231. } else {
  232. aBlacklist.style.setProperty("height", "0em", "");
  233. aBlacklist.style.setProperty("opacity", "0", "");
  234. }
  235. }, false);
  236.  
  237. searchForm.appendChild(d.createElement("BR"));
  238. searchForm.appendChild(aBlacklist = d.createElement("TEXTAREA"));
  239. aBlacklist.setAttribute("style", "box-sizing: border-box; height: 0em; min-width: 100%; opacity: 0");
  240. aBlacklist.appendChild(d.createTextNode(blacklist));
  241.  
  242. searchForm.appendChild(d.createElement("BR"));
  243. searchForm.appendChild(aReply = d.createElement("SPAN"));
  244. aReply.appendChild(d.createTextNode("Nobody here but us chickens!"));
  245.  
  246. searchForm.appendChild(d.createElement("P"));
  247.  
  248. // Slider
  249. aTableBar = d.createElement("TABLE");
  250. aTableBar.appendChild(d.createElement("TR"));
  251. aTableBar.setAttribute("style", "border-collapse: collapse; border: 1px solid Black; width: 100%; padding: 0px");
  252. aLeftBar = d.createElement("TD");
  253. aLeftBar.setAttribute("style", "padding: 0px;");
  254. aTableBar.firstChild.appendChild(aLeftBar);
  255. aCenterBar = d.createElement("TD");
  256. aCenterBar.setAttribute("style", "border: 1px solid Black; padding: 1.5px 0px; background-color: WhiteSmoke; width: 25%; min-width: 1px;");
  257. aTableBar.firstChild.appendChild(aCenterBar);
  258. aRightBar = d.createElement("TD");
  259. aRightBar.setAttribute("style", "padding: 0px; width: 100%;");
  260. aTableBar.firstChild.appendChild(aRightBar);
  261. searchForm.appendChild(aTableBar);
  262.  
  263. // Search options
  264. aTable = d.createElement("TABLE");
  265. aTr1 = d.createElement("TR");
  266.  
  267. aTd1 = d.createElement("TD");
  268. aTd1.appendChild(d.createTextNode("Page:"));
  269. aTr1.appendChild(aTd1);
  270.  
  271. aTd2 = d.createElement("TD");
  272. aPage = d.createElement("INPUT");
  273. aPage.setAttribute("type", "text");
  274. aPage.setAttribute("size", "1");
  275. aPage.setAttribute("value", page);
  276. aPage.setAttribute("tabindex", "3");
  277. aTd2.appendChild(aPage);
  278. aTr1.appendChild(aTd2);
  279.  
  280. aTd3 = d.createElement("TD");
  281. aTd3.setAttribute("style", "text-align: left;");
  282. aTd3.setAttribute("rowspan", "3");
  283.  
  284. aRS = d.createElement("INPUT");
  285. aRS.setAttribute("type", "checkbox");
  286. if(/s/.test(rating))
  287. aRS.setAttribute("checked", "checked");
  288. aLS = d.createElement("LABEL");
  289. aLS.appendChild(aRS);
  290. aLS.appendChild(d.createTextNode("Safe"));
  291.  
  292. aTd3.appendChild(aLS);
  293. aTd3.appendChild(d.createElement("BR"));
  294.  
  295. aRQ = d.createElement("INPUT");
  296. aRQ.setAttribute("type", "checkbox");
  297. if(/q/.test(rating))
  298. aRQ.setAttribute("checked", "checked");
  299. aLQ = d.createElement("LABEL");
  300. aLQ.appendChild(aRQ);
  301. aLQ.appendChild(d.createTextNode("Questionable"));
  302.  
  303. aTd3.appendChild(aLQ);
  304. aTd3.appendChild(d.createElement("BR"));
  305.  
  306. aRE = d.createElement("INPUT");
  307. aRE.setAttribute("type", "checkbox");
  308. if(/e/.test(rating))
  309. aRE.setAttribute("checked", "checked");
  310. aLE = d.createElement("LABEL");
  311. aLE.appendChild(aRE);
  312. aLE.appendChild(d.createTextNode("Explicit"));
  313.  
  314. aTd3.appendChild(aLE);
  315. aTd3.appendChild(d.createElement("BR"));
  316.  
  317. aSD = d.createElement("INPUT");
  318. if(booru.name == "Danbooru") {
  319. aSD.setAttribute("type", "checkbox");
  320. if(/d/.test(rating))
  321. aSD.setAttribute("checked", "checked");
  322. aLD = d.createElement("LABEL");
  323. aLD.appendChild(aSD);
  324. aLD.appendChild(d.createTextNode("Show deleted"));
  325.  
  326. aTd3.appendChild(aLD);
  327. aTd3.appendChild(d.createElement("BR"));
  328.  
  329. aSD.addEventListener("change", function(event) {
  330. for(var i in cacheHide)
  331. content.insertBefore(cacheHide[i], content.childNodes[cacheHide[i].style.getPropertyValue("order")]);
  332.  
  333. setTimeout(function() { // transitions are stupid
  334. deletedlist = d.evaluate("//DIV[contains(@class, 'red')]", d, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  335. if(aSD.checked) {
  336. for(var n = 0, deleted = null; deleted = deletedlist.snapshotItem(n++); n) {
  337. deleted.style.setProperty("opacity", "1", "");
  338. deleted.style.setProperty("padding", "1px", "");
  339. deleted.style.setProperty("margin", "-1px 0px 0px -1px", "");
  340. deleted.style.setProperty("border-width", "1px", "");
  341. deleted.style.setProperty("min-width", "150px", "");
  342. deleted.style.setProperty("max-width", "150px", "");
  343. }
  344. } else {
  345. cacheHide.length = 0;
  346. for(var n = 0, deleted = null; deleted = deletedlist.snapshotItem(n++); n) {
  347. deleted.style.setProperty("opacity", "0", "");
  348. deleted.style.setProperty("padding", "0px", "");
  349. deleted.style.setProperty("margin", "0px", "");
  350. deleted.style.setProperty("border-width", "0px", "");
  351. deleted.style.setProperty("min-width", "0px", "");
  352. deleted.style.setProperty("max-width", "0px", "");
  353. cacheHide.push(deleted);
  354. }
  355. }
  356. }, 25);
  357. }, false);
  358. } else {
  359. rating.replace("d", "");
  360. }
  361. aTr1.appendChild(aTd3);
  362.  
  363. aTr2 = d.createElement("TR");
  364.  
  365. aTd4 = d.createElement("TD");
  366. aTd4.appendChild(d.createTextNode("Images:"));
  367. aTr2.appendChild(aTd4);
  368.  
  369. aTd5 = d.createElement("TD");
  370. aImages = d.createElement("INPUT");
  371. aImages.setAttribute("type", "text");
  372. aImages.setAttribute("size", "1");
  373. aImages.setAttribute("value", images);
  374. aImages.setAttribute("tabindex", "4");
  375. aTd5.appendChild(aImages);
  376. aTr2.appendChild(aTd5);
  377.  
  378. aTr3 = d.createElement("TR");
  379.  
  380. aTd6 = d.createElement("TD");
  381. aTd6.setAttribute("colspan", "2");
  382. aFIT = d.createElement("INPUT");
  383. aFIT.setAttribute("type", "checkbox");
  384. aFIT.setAttribute("tabindex", "5");
  385. if(/f/.test(rating))
  386. aFIT.setAttribute("checked", "checked");
  387. aLF = d.createElement("LABEL");
  388. aLF.appendChild(aFIT);
  389. aLF.appendChild(d.createTextNode("Fit width"));
  390.  
  391. aTd6.appendChild(aLF);
  392. aTr3.appendChild(aTd6);
  393.  
  394. aTable.appendChild(aTr1);
  395. aTable.appendChild(aTr2);
  396. aTable.appendChild(aTr3);
  397. searchForm.appendChild(aTable);
  398.  
  399. searchForm.appendChild(d.createElement("HR"));
  400.  
  401. searchForm.appendChild(aPrev = d.createElement("INPUT"));
  402. aPrev.setAttribute("type", "button");
  403. aPrev.setAttribute("value", "<");
  404. aPrev.setAttribute("disabled", "disabled");
  405. aPrev.addEventListener("click", function(event) {
  406. search(tags, --page);
  407. }, false);
  408.  
  409. searchForm.appendChild(aSearch = d.createElement("INPUT"));
  410. aSearch.setAttribute("type", "submit");
  411. aSearch.setAttribute("value", "Search");
  412.  
  413. searchForm.appendChild(aNext = d.createElement("INPUT"));
  414. aNext.setAttribute("type", "button");
  415. aNext.setAttribute("value", ">");
  416. aNext.setAttribute("disabled", "disabled");
  417. aNext.addEventListener("click", function(event) {
  418. search(tags, ++page);
  419. }, false);
  420.  
  421. searchForm.appendChild(d.createElement("HR"));
  422.  
  423. searchForm.appendChild(aTagsDisplay = d.createElement("DIV"));
  424. aTagsDisplay.setAttribute("style", "overflow-x: hidden; overflow-wrap: break-word;");
  425.  
  426. searchTd.appendChild(searchForm);
  427. searchTr.appendChild(imagesLayer = d.createElement("TD"));
  428.  
  429. fixsize = (searchTd.getBoundingClientRect().right - searchTd.getBoundingClientRect().left) + "px";
  430. searchTd.style.setProperty("min-width", fixsize, "important");
  431. searchTd.style.setProperty("max-width", fixsize, "important");
  432.  
  433. // "Lightbox"
  434. overlay = d.createElement("DIV");
  435. overlay.setAttribute("style", "display: none; position: fixed; top: 0px; left: 0px; width: 100%; height: 100%; background-color: Black; opacity: 0.8;");
  436. innerDisplay = d.createElement("DIV");
  437. innerDisplay.setAttribute("id", "innerDisplay");
  438. innerDisplay.setAttribute("style", "background-color: White; display: inline-table; padding: 10px; min-width: 200px; min-height: 200px; border-radius: 10px; margin: auto;");
  439. display = d.createElement("DIV");
  440. display.setAttribute("style", "display: none; position: fixed; top: 0px; left: 0px; width: 100%; height: 100%; text-align: center; justify-content: space-between; overflow: auto;");
  441. display.appendChild(innerDisplay);
  442.  
  443. prevImage = d.createElement("DIV");
  444. prevImage.setAttribute("id", "prevImage");
  445. prevImage.setAttribute("style", "display: flex; justify-content: center; align-items: center; position: fixed; top: 0px; left: 0px; background-image: linear-gradient(to right, Black, Transparent); color: White; font-size: xx-large; width: 30px; height: 100%; opacity: 0;");
  446. prevImage.setAttribute("class", "fa fa-chevron-left");
  447.  
  448. nextImage = d.createElement("DIV");
  449. nextImage.setAttribute("id", "nextImage");
  450. nextImage.setAttribute("style", "display: flex; justify-content: center; align-items: center; position: fixed; top: 0px; right: 0px; background-image: linear-gradient(to left, Black, Transparent); color: White; font-size: xx-large; width: 30px; height: 100%; opacity: 0;");
  451. nextImage.setAttribute("class", "fa fa-chevron-right");
  452.  
  453. d.body.insertBefore(display, d.body.firstChild);
  454. d.body.insertBefore(overlay, d.body.firstChild);
  455.  
  456. prevImage.addEventListener("click", function(e) {
  457. next = openimage.parentNode.previousElementSibling || openimage.parentNode.parentNode.lastChild;
  458. next.firstChild.firstChild.dispatchEvent(new MouseEvent("click", { "bubbles" : true, "cancelable": true }));
  459. }, false);
  460. prevImage.addEventListener("mouseover", function(e) {
  461. prevImage.style.setProperty("opacity", "1", "");
  462. }, false);
  463. prevImage.addEventListener("mouseout", function(e) {
  464. prevImage.style.setProperty("opacity", "0", "");
  465. }, false);
  466.  
  467. nextImage.addEventListener("click", function(e) {
  468. next = openimage.parentNode.nextElementSibling || openimage.parentNode.parentNode.firstChild;
  469. next.firstChild.firstChild.dispatchEvent(new MouseEvent("click", { "bubbles" : true, "cancelable": true }));
  470. }, false);
  471. nextImage.addEventListener("mouseover", function(e) {
  472. nextImage.style.setProperty("opacity", "1", "");
  473. }, false);
  474. nextImage.addEventListener("mouseout", function(e) {
  475. nextImage.style.setProperty("opacity", "0", "");
  476. }, false);
  477.  
  478. display.addEventListener("click", function(e) {
  479. if(e.target.id || d.evaluate("ancestor-or-self::div[contains(@id, 'bodynote')]", e.target, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue)
  480. return;
  481. d.body.style.setProperty("overflow", "auto", "");
  482. overlay.style.setProperty("display", "none", "");
  483. display.style.setProperty("display", "none", "");
  484. }, false);
  485.  
  486. display.addEventListener("dblclick", function(e) {
  487. d.body.style.setProperty("overflow", "auto", "");
  488. overlay.style.setProperty("display", "none", "");
  489. display.style.setProperty("display", "none", "");
  490. }, false);
  491.  
  492. // CTRL+S to save image
  493. var keylistener = function(event) {
  494. if(display.style.getPropertyValue("display") == "none")
  495. return
  496. else
  497. if(event.ctrlKey && event.keyCode == 83) { // CTRL+S
  498. event.preventDefault();
  499. event.stopPropagation();
  500. } else
  501. if(aFIT.checked && event.keyCode == 37) { // LEFT KEY
  502. next = openimage.parentNode.previousElementSibling || openimage.parentNode.parentNode.lastChild;
  503. next.firstChild.firstChild.dispatchEvent(new MouseEvent("click", { "bubbles" : true, "cancelable": true }));
  504. } else
  505. if(aFIT.checked && event.keyCode == 39) { // RIGHT KEY
  506. next = openimage.parentNode.nextElementSibling || openimage.parentNode.parentNode.firstChild;
  507. next.firstChild.firstChild.dispatchEvent(new MouseEvent("click", { "bubbles" : true, "cancelable": true }));
  508. }
  509. };
  510. var saveimage = function(event) {
  511. if(event.ctrlKey && event.keyCode == 83) { // CTRL+S
  512. var sauce = openimage.href.match(/[^\/]+$/)[0];
  513. if(/%/.test(sauce))
  514. sauce = decodeURIComponent(sauce);
  515. var imgdown = d.createElement("A");
  516. imgdown.setAttribute("download", sauce);
  517. imgdown.setAttribute("href", openimage.href);
  518. d.body.appendChild(imgdown);
  519. imgdown.dispatchEvent(new MouseEvent("click"));
  520. imgdown.remove();
  521. }
  522. };
  523. window.addEventListener("keydown", keylistener, false);
  524. window.addEventListener("keyup", saveimage, false);
  525.  
  526. if(!window.location.origin)
  527. window.location.origin = (window.location.protocol + "//" + window.location.host);
  528.  
  529. if(window.location.hash)
  530. search(window.location.hash.split("#")[1], 1);
  531.  
  532. function search(newtags, newpage) {
  533. if(requestPost)
  534. requestPost.abort();
  535. if(requestNote)
  536. requestNote.abort();
  537. if(requestCount)
  538. requestCount.abort();
  539. if(requestCache)
  540. requestCache.abort();
  541. if(requestTag)
  542. requestTag.abort();
  543.  
  544. clearTimeout(tagTimer);
  545. clearTimeout(reqTagTimer);
  546.  
  547. aTags.value = tags = newtags;
  548. aPage.value = page = newpage;
  549. aImages.value = images;
  550. blacklist = aBlacklist.value.trim();
  551.  
  552. aPrev.disabled = (newpage < 2);
  553. aRS.checked = /s/.test(rating);
  554. aRQ.checked = /q/.test(rating);
  555. aRE.checked = /e/.test(rating);
  556.  
  557. if(/^s(?!q|e)/.test(rating))
  558. newtags += " rating:safe";
  559. if(/^q(?!e)/.test(rating))
  560. newtags += " rating:questionable";
  561. if(/^e/.test(rating))
  562. newtags += " rating:explicit";
  563. if(/^qe/.test(rating))
  564. newtags += " -rating:safe";
  565. if(/^se/.test(rating))
  566. newtags += " -rating:questionable";
  567. if(/^sq(?!e)/.test(rating))
  568. newtags += " -rating:explicit";
  569.  
  570. if(imagesLayer.hasChildNodes())
  571. imagesLayer.removeChild(imagesLayer.firstChild);
  572. while(aDatalist.hasChildNodes())
  573. aDatalist.removeChild(aDatalist.firstChild);
  574.  
  575. imagesLayer.appendChild(d.createTextNode("Loading..."));
  576. requestPost = xmlhttpRequest({
  577. method : "GET",
  578. url : window.location.origin + booru.post + booru.query(encodeURIComponent(newtags.trim()), images, page),
  579. headers : {
  580. "Accept" : "application/xml"
  581. },
  582. overrideMimeType : "application/xml; charset=utf-8",
  583. onload : function(response) {
  584. getContent(domParser.parseFromString(response.responseText, "application/xml"), newtags);
  585. }
  586. });
  587. saveValues();
  588. }
  589.  
  590. function showContent(xmldoc) {
  591. if(imagesLayer.hasChildNodes())
  592. imagesLayer.removeChild(imagesLayer.firstChild);
  593.  
  594. aReply.textContent = "Nobody here but us chickens!";
  595.  
  596. var posts = xmldoc.evaluate("posts", xmldoc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  597. if(!posts) {
  598. reason = xmldoc.evaluate("response/@reason | result/text()", xmldoc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  599. if(reason)
  600. imagesLayer.textContent = reason.nodeValue;
  601. else
  602. imagesLayer.textContent = "Something broke.";
  603. return;
  604. }
  605. var post = xmldoc.evaluate("posts/post", xmldoc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  606. if(((parseInt(posts.getAttribute("offset"), 10) + 1) > posts.getAttribute("count")) & posts.getAttribute("count") > 0) {
  607. search(tags, Math.ceil(parseInt(posts.getAttribute("count"), 10) / images));
  608. return;
  609. }
  610.  
  611. if(posts.getAttribute("count") > 0)
  612. aReply.textContent = "Found " + posts.getAttribute("count") + ", showing " + (parseInt(posts.getAttribute("offset"), 10) + 1) + "-" + (Math.min(posts.getAttribute("count"), parseInt(posts.getAttribute("offset"), 10) + parseInt(images, 10)));
  613. aLeftBar.style.setProperty("width", (posts.getAttribute("offset") / posts.getAttribute("count") * 100) + "%");
  614. aCenterBar.style.setProperty("width", (images / posts.getAttribute("count") * 100) + "%");
  615.  
  616. aNext.disabled = (page >= Math.ceil(posts.getAttribute("count") / images));
  617.  
  618. imagesLayer.appendChild(content = d.createElement("DIV"));
  619. content.setAttribute("style", "display: flex; flex-flow: row wrap;");
  620. content.setAttribute("id", "content");
  621. cacheHide.length = 0;
  622.  
  623. for(var i = 0; i < images; i++) {
  624. data = post.snapshotItem(i);
  625. if(data) {
  626. if(!data.getAttribute("preview-file-url") && booru.name == "Danbooru")
  627. if(!checkCache(data))
  628. continue;
  629. content.appendChild(thumb = d.createElement("DIV"));
  630. if(!!data.getAttribute("last-noted-at") && data.getAttribute("last-noted-at") != "false")
  631. data.setAttribute("has_notes", "true");
  632. var image = d.createElement("IMG");
  633. image.setAttribute("width", (data.getAttribute("preview_width") || "auto"));
  634. image.setAttribute("height", (data.getAttribute("preview_height") || "auto"));
  635. image.setAttribute("src", data.getAttribute("preview_url") || data.getAttribute("preview-file-url"));
  636. image.setAttribute("id", data.getAttribute("id"));
  637. image.setAttribute("alt", data.getAttribute("tag-string") || data.getAttribute("tags").trim());
  638. image.setAttribute("title", (data.getAttribute("tag-string") || data.getAttribute("tags").trim()) + " rating:" + data.getAttribute("rating").replace(/e$/, "Explicit").replace(/s$/, "Safe").replace(/q$/, "Questionable") + " score:" + data.getAttribute("score"));
  639.  
  640. if(/\.zip$/.test(data.getAttribute("file_url")) || /\.zip$/.test(data.getAttribute("file-url"))) {
  641. data.setAttribute("file_url", (data.getAttribute("preview_url") || data.getAttribute("preview-file-url")).replace("preview/", "sample/sample-").replace(/\..+$/, ".webm"));
  642. data.setAttribute("file-url", data.getAttribute("file_url"));
  643. }
  644. image.setAttribute("fullsize", data.getAttribute("file_url") || data.getAttribute("file-url"));
  645. image.setAttribute("fullwidth", data.getAttribute("width") || data.getAttribute("image-width"));
  646. image.setAttribute("fullheight", data.getAttribute("height") || data.getAttribute("image-height"));
  647. image.setAttribute("notes", data.getAttribute("has_notes"));
  648. if(data.getAttribute("last_noted_at"))
  649. image.setAttribute("notes", "true");
  650. image.setAttribute("md5", data.getAttribute("md5"));
  651.  
  652. // Show tags on sidebar
  653. image.addEventListener("click", function(event) {
  654. if(aTagsDisplay.hasChildNodes())
  655. aTagsDisplay.removeChild(aTagsDisplay.firstChild);
  656. aTagsDisplay.appendChild(d.createElement("DIV"));
  657.  
  658. var tagnames = this.getAttribute("alt").split(" ");
  659. for(var t = 0; t < tagnames.length; t++) {
  660. var taglink = d.createElement("A");
  661. taglink.appendChild(d.createTextNode(tagnames[t]));
  662. taglink.setAttribute("href", "#" + tagnames[t]);
  663. taglink.addEventListener("click", function(event) {
  664. aTags.value = event.target.textContent;
  665. aPage.value = 1;
  666. aSearch.dispatchEvent(new MouseEvent("click"));
  667. event.preventDefault();
  668. }, false);
  669. aTagsDisplay.firstChild.appendChild(taglink);
  670. aTagsDisplay.firstChild.appendChild(document.createElement("BR"));
  671. }
  672.  
  673. // CTRL + click to show only the tags
  674. if(event.ctrlKey)
  675. return;
  676.  
  677. // "Lightbox" by VIPPER ("How do I jQuery?")
  678. while(innerDisplay.hasChildNodes())
  679. innerDisplay.removeChild(innerDisplay.firstChild);
  680.  
  681. innerDisplay.appendChild(prevImage);
  682. innerDisplay.appendChild(nextImage);
  683.  
  684. if(/\.swf$/.test(this.getAttribute("fullsize")))
  685. fullsize = d.createElement("EMBED");
  686. else if(/\.webm$|\.zip$/.test(this.getAttribute("fullsize"))) {
  687. fullsize = d.createElement("VIDEO");
  688. fullsize.setAttribute("controls", true);
  689. fullsize.setAttribute("loop", true);
  690. fullsize.setAttribute("autoplay", true);
  691. if(/\.zip$/.test(this.getAttribute("fullsize")))
  692. this.setAttribute("fullsize", this.getAttribute("fullsize").replace("data/", "data/sample/sample-").replace(".zip", ".webm"));
  693. } else {
  694. fullsize = d.createElement("IMG");
  695. fullsize.addEventListener("click", function(event) {
  696. noteDivs = d.evaluate("./DIV[starts-with(@id, 'note')]", innerDisplay, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  697. for(var n = 0, note = null; note = noteDivs.snapshotItem(n++); n) {
  698. if(note.style.getPropertyValue("visibility") == "visible")
  699. note.style.setProperty("visibility", "hidden", "");
  700. else
  701. note.style.setProperty("visibility", "visible", "");
  702. }
  703. }, false);
  704. }
  705. fullsize.setAttribute("src", this.getAttribute("fullsize"));
  706. fullsize.setAttribute("width", this.getAttribute("fullwidth"));
  707. fullsize.setAttribute("height", this.getAttribute("fullheight"));
  708. fullsize.setAttribute("id", this.getAttribute("id"));
  709. fullsize.setAttribute("notes", this.getAttribute("notes"));
  710. fullsize.setAttribute("md5", this.getAttribute("md5"));
  711. fullsize.style.setProperty("transition-property", "none", "");
  712.  
  713. var pagelink = d.createElement("A");
  714. pagelink.setAttribute("href", booru.page + fullsize.getAttribute("id") + "?tags=" + encodeURIComponent(tags.trim()));
  715. pagelink.appendChild(d.createTextNode(fullsize.getAttribute("id")));
  716.  
  717. innerDisplay.appendChild(fullsize);
  718. innerDisplay.appendChild(d.createElement("BR"));
  719. innerDisplay.appendChild(pagelink);
  720.  
  721. overlay.style.setProperty("display", "", "");
  722. display.style.setProperty("display", "flex", "");
  723. display.scrollTo(0, 0);
  724. d.body.style.setProperty("overflow", "hidden", "");
  725.  
  726. sampleRate = 1;
  727. clientH = parseInt(document.documentElement.clientHeight, 10);
  728. clientW = Math.min(parseInt(document.documentElement.clientWidth, 10), parseInt(document.body.clientWidth, 10)) - 20;
  729.  
  730. if(aFIT.checked && this.getAttribute("fullwidth") > clientW) {
  731. if(parseInt(this.getAttribute("fullheight"), 10) + 40 > clientH) {
  732. sampleRate = (document.documentElement.clientWidth - (window.outerWidth - window.innerWidth) - 21) / this.getAttribute("fullwidth");
  733. } else {
  734. sampleRate = (document.documentElement.clientWidth - 20) / this.getAttribute("fullwidth");
  735. }
  736. fullsize.setAttribute("width", this.getAttribute("fullwidth") * sampleRate + "px"); // 100%
  737. fullsize.setAttribute("height", this.getAttribute("fullheight") * sampleRate + "px"); // auto
  738. }
  739. nextImage.style.setProperty("right", (display.scrollHeight > clientH ? (window.outerWidth - window.innerWidth + 1) : 0) + "px");
  740.  
  741. if(this.getAttribute("notes") == "true")
  742. //fullsize.addEventListener("load", function(e) {
  743. requestNote = xmlhttpRequest({
  744. method : "GET",
  745. url : window.location.origin + booru.note + booru.query(null, null, null, fullsize.getAttribute("id")),
  746. headers : {
  747. "Accept" : "application/xml"
  748. },
  749. overrideMimeType : "application/xml; charset=utf-8",
  750. onload : function(response) {
  751. getNotes(domParser.parseFromString(response.responseText, "application/xml"), response.responseURL);
  752. }
  753. });
  754. //}, false);
  755. }, false);
  756.  
  757. var link = d.createElement("A");
  758. link.setAttribute("href", data.getAttribute("file_url") || data.getAttribute("file-url"));
  759. link.setAttribute("alt", data.getAttribute("id"));
  760. link.appendChild(image);
  761. link.addEventListener("click", function(event) {
  762. openimage = event.target.parentNode;
  763. event.preventDefault();
  764. }, false);
  765.  
  766. thumb.classList.add("thumb");
  767. if(booru.name == "Danbooru")
  768. thumb.style.setProperty("order", i, "");
  769. thumb.appendChild(link);
  770. if(/true/.test(data.getAttribute("has_notes")))
  771. thumb.classList.add("yellow");
  772. if(/deleted/.test(data.getAttribute("status")) || /true/.test(data.getAttribute("is-deleted"))) {
  773. thumb.classList.add("red");
  774. thumb.addEventListener("transitionend", function() {
  775. if(!aSD.checked)
  776. this.remove();
  777. }, false);
  778. if(!aSD.checked) {
  779. thumb.style.setProperty("opacity", "0", "");
  780. thumb.style.setProperty("padding", "0", "");
  781. thumb.style.setProperty("margin", "0", "");
  782. thumb.style.setProperty("border-width", "0px", "");
  783. thumb.style.setProperty("min-width", "0px", "");
  784. thumb.style.setProperty("max-width", "0px", "");
  785. cacheHide.push(thumb);
  786. thumb.remove();
  787. }
  788. }
  789. taglist = image.getAttribute("alt") + " ";
  790. blacklisted = blacklist.split("\n");
  791. for(var b = 0; b < blacklisted.length; b++) {
  792. bLine = blacklisted[b].replace(/\s\s+/g, " ").trim().split(" "); // normalize
  793. var todelete = 0;
  794. for(var c = 0; c < bLine.length; c++) {
  795. if(bLine[c] == "")
  796. break;
  797. if(taglist.indexOf(bLine[c] + " ") >= 0)
  798. todelete++;
  799. }
  800. if(todelete == bLine.length)
  801. thumb.remove();
  802. }
  803. }
  804. }
  805. }
  806.  
  807. function getContent(xmldoc, newtags) {
  808. if(booru.name == "Danbooru") { // Inject the count where it should be by default...
  809. requestCount = xmlhttpRequest({
  810. method : "GET",
  811. url : window.location.origin + "/counts/posts.xml" + booru.query(encodeURIComponent(newtags)),
  812. headers : {
  813. "Accept" : "application/xml"
  814. },
  815. overrideMimeType : "application/xml; charset=utf-8",
  816. onload : function(response) {
  817. newxmldoc = domParser.parseFromString(response.responseText, "application/xml");
  818. count = newxmldoc.evaluate("counts/posts", newxmldoc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  819. posts = xmldoc.evaluate("posts", xmldoc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  820. if(posts) {
  821. posts.setAttribute("count", count ? count.textContent.trim() : "0");
  822. posts.setAttribute("offset", (page - 1) * images);
  823. // processing of new XML back to legacy format
  824. var xsltProcessor = new XSLTProcessor();
  825. xsltProcessor.importStylesheet(domParser.parseFromString(xsltText, "text/xml"));
  826. xmldoc = xsltProcessor.transformToDocument(xmldoc);
  827. }
  828. showContent(xmldoc);
  829. }
  830. });
  831. } else {
  832. showContent(xmldoc);
  833. }
  834. }
  835.  
  836. function checkCache(data) {
  837. var cached = false;
  838. for(var i in cacheID)
  839. if(data.getAttribute("id") == cacheID[i].id && cacheID[i].file != "null") { // insert the cached url directly to data
  840. cached = true;
  841. data.setAttribute("file-url", cacheID[i].file);
  842. data.setAttribute("preview-file-url", "/data/preview/" + cacheID[i].file.match(/[a-f0-9]{32}/)[0] + ".jpg");
  843. data.setAttribute("md5", cacheID[i].file.match(/[a-f0-9]{32}/)[0]);
  844. cacheID[i].expires = Math.abs(Date.now() + cacheExpires);
  845. break;
  846. }
  847. return cached;
  848. /*
  849. if(!cached)
  850. requestCache = xmlhttpRequest({
  851. method : "GET",
  852. url : window.location.origin + "/posts/" + data.getAttribute("id"),
  853. headers : {
  854. "Accept" : "text/html"
  855. },
  856. onload : function(response) {
  857. parsedPage = domParser.parseFromString(response.responseText, "text/html");
  858. parsed = parsedPage.getElementById("image-container");
  859. if(!parsed)
  860. return;
  861. id = parsed.getAttribute("data-id");
  862. file = parsed.getAttribute("data-file-url");
  863. if(/\.zip$/.test(file))
  864. file = parsed.getAttribute("data-large-file-url");
  865. cacheID.push({ id : id, file : file, expires : Math.abs(Date.now() + cacheExpires) });
  866. image = d.getElementById(id);
  867. if(!image) // check the deleted images too
  868. for(var i in cacheHide)
  869. if(cacheHide[i].firstChild.firstChild.getAttribute("id") == id)
  870. image = cacheHide[i].firstChild.firstChild;
  871. if(!image)
  872. return;
  873. image.setAttribute("fullsize", file);
  874. image.setAttribute("src", "/data/preview/" + file.match(/[a-f0-9]{32}/)[0] + ".jpg");
  875. image.setAttribute("md5", file.match(/[a-f0-9]{32}/)[0]);
  876. image.parentNode.setAttribute("href", file);
  877. }
  878. });
  879. */
  880. }
  881.  
  882. function showNotes(note, id) {
  883. var offsetx = Math.max(10, fullsize.getBoundingClientRect().left); //+ (d.documentElement.scrollLeft || d.body.scrollLeft);
  884. var offsety = Math.max(10, fullsize.getBoundingClientRect().top); //+ (d.documentElement.scrollTop || d.body.scrollTop);
  885. var vp_bottom = Math.max(window.innerHeight, innerDisplay.getBoundingClientRect().bottom - innerDisplay.getBoundingClientRect().top);
  886.  
  887. for(var i = 0, ndata = null; ndata = note.snapshotItem(i++); i) {
  888. if(id.match(/[0-9]+$/)[0] != fullsize.getAttribute("id"))
  889. continue;
  890. if(d.getElementById("note" + ndata.getAttribute("id")))
  891. continue;
  892.  
  893. var noteDiv = d.createElement("DIV");
  894. noteDiv.setAttribute("id", "note" + ndata.getAttribute("id"));
  895. noteDiv.setAttribute("class", "trans");
  896. noteDiv.setAttribute("style", "opacity: 0.4; transition: none !important;");
  897. noteDiv.style.setProperty("left", ndata.getAttribute("x") * sampleRate + offsetx + "px", "");
  898. noteDiv.style.setProperty("top", ndata.getAttribute("y") * sampleRate + offsety + "px", "");
  899. noteDiv.style.setProperty("width", ndata.getAttribute("width") * sampleRate + "px", "");
  900. noteDiv.style.setProperty("height", ndata.getAttribute("height") * sampleRate + "px", "");
  901. noteDiv.addEventListener("mouseover", function(event) {
  902. noteclearTimer = setTimeout(function() {
  903. d.getElementById("body" + this.getAttribute("id")).style.setProperty("display", "", "");
  904. }.bind(this), 100);
  905. }, false);
  906. noteDiv.addEventListener("mouseout", function(event) {
  907. noteclearTimer = setTimeout(function() {
  908. d.getElementById("body" + this.getAttribute("id")).style.setProperty("display", "none", "");
  909. }.bind(this), 200);
  910. }, false);
  911. innerDisplay.appendChild(noteDiv);
  912.  
  913. var noteBody = d.createElement("DIV");
  914. noteBody.innerHTML = ndata.getAttribute("body");
  915. noteBody.setAttribute("id", "bodynote" + ndata.getAttribute("id"));
  916. noteBody.setAttribute("class", "trans");
  917. noteBody.setAttribute("style", "color: Black; text-align: left; padding: 4px; z-index: 1" + i + ";");
  918. noteBody.addEventListener("mouseover", function(event) {
  919. clearTimeout(noteclearTimer);
  920. this.style.setProperty("display", "", "");
  921. }, false);
  922. noteBody.addEventListener("mouseout", function(event) {
  923. this.style.setProperty("display", "none", "");
  924. }, false);
  925. innerDisplay.appendChild(noteBody);
  926.  
  927. // this sucks, find another method!
  928. var w = ndata.getAttribute("width") * sampleRate;
  929. var h = ndata.getAttribute("height") * sampleRate;
  930. if(w < h) { // FUCK YEAH XOR SWAP
  931. w ^= h;
  932. h ^= w;
  933. w ^= h;
  934. }
  935. while(w / h > ratio) {
  936. w -= ratio;
  937. h += ratio;
  938. }
  939.  
  940. noteBody.style.setProperty("min-width", "-moz-min-content", "");
  941. noteBody.style.setProperty("min-width", "-webkit-min-content", "");
  942. noteBody.style.setProperty("max-width", w + "px", "");
  943.  
  944. ntop = (ndata.getAttribute("y") * sampleRate) + (ndata.getAttribute("height") * sampleRate) + offsety + 5;
  945. nheight = noteBody.getBoundingClientRect().bottom - noteBody.getBoundingClientRect().top;
  946. if(ntop + nheight > vp_bottom)
  947. noteBody.style.setProperty("top", vp_bottom - nheight + "px", "");
  948. else
  949. noteBody.style.setProperty("top", ntop + "px", "");
  950.  
  951. noteBody.style.setProperty("left", ndata.getAttribute("x") * sampleRate + offsetx + "px", "");
  952. noteBody.style.setProperty("display", "none", "");
  953. }
  954. }
  955.  
  956. function getNotes(xmldoc, id) {
  957. if(booru.name == "Danbooru") { // Parses the nodes as attributes for each note
  958. var xsltProcessor = new XSLTProcessor();
  959. xsltProcessor.importStylesheet(domParser.parseFromString(xsltText, "text/xml"));
  960. xmldoc = xsltProcessor.transformToDocument(xmldoc);
  961. showNotes(xmldoc.evaluate("notes/note", xmldoc, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null), id);
  962. } else {
  963. showNotes(xmldoc.evaluate("notes/note[@is_active = 'true']", xmldoc, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null), id);
  964. }
  965. }
  966.  
  967. function saveValues() { // sanitize and save as string
  968. tags = ("" + tags).replace(",", "");
  969. images = ("" + images).replace(",", "");
  970. page = ("" + page).replace(",", "");
  971. rating = ("" + rating).replace(",", "");
  972. blacklist = ("" + blacklist).replace(",", "");
  973. var cachedTags = tags.trim().split(" ");
  974. var cached = false;
  975. for(var i in cachedTags) {
  976. for(var j in cacheTags)
  977. if(cachedTags[i] == cacheTags[j].tag) {
  978. delete cacheTags[j];
  979. break;
  980. }
  981. cacheTags.push({ tag : cachedTags[i], expires : Date.now() + cacheExpires });
  982. }
  983. }
  984.  
  985. function setStorage() {
  986. saveValues(); // double sure that it was saved
  987. if(!checkStorage())
  988. return;
  989. storage.setItem("dai-tags", tags);
  990. storage.setItem("dai-images", images);
  991. storage.setItem("dai-page", page);
  992. storage.setItem("dai-rating", rating);
  993. storage.setItem("dai-blacklist", blacklist);
  994. var cachedID = "";
  995. for(var i in cacheID) // save as a shorter CSV string : id,file,expires,
  996. cachedID += cacheID[i].id + "," + cacheID[i].file + "," + cacheID[i].expires + ",";
  997. storage.setItem("dai-cacheID", cachedID);
  998. var cachedTags = "";
  999. for(var i in cacheTags) // save as a shorter CSV string : tag,expires,
  1000. cachedTags += cacheTags[i].tag ? cacheTags[i].tag + "," + cacheTags[i].expires + "," : "";
  1001. storage.setItem("dai-cacheTags", cachedTags);
  1002. }
  1003.  
  1004. function getStorage() { // load as string
  1005. tags = storage.getItem("dai-tags") || tags;
  1006. images = parseInt(storage.getItem("dai-images") || images, 10);
  1007. page = parseInt(storage.getItem("dai-page") || page, 10);
  1008. rating = storage.getItem("dai-rating") || rating;
  1009. blacklist = storage.getItem("dai-blacklist") || blacklist;
  1010. var cachedID = (storage.getItem("dai-cacheID") || "").split(",");
  1011. for(var i = 0; i < cachedID.length - 1; i += 3) // load the cached ids
  1012. if(cachedID[i+2] > Date.now()) // but only if not expired
  1013. cacheID.push({ id : cachedID[i], file : cachedID[i+1], expires : cachedID[i+2] });
  1014. var cachedTags = (storage.getItem("dai-cacheTags") || "").split(",");
  1015. for(var i = 0; i < cachedTags.length - 1; i += 2) // load the cached tags
  1016. if(cachedTags[i+1] > Date.now()) // but only if not expired
  1017. cacheTags.push({ tag : cachedTags[i], expires : cachedTags[i+1] });
  1018. }
  1019.  
  1020. function checkStorage() {
  1021. try {
  1022. storage = window.localStorage, test = "__storage_test__";
  1023. storage.setItem(test, test);
  1024. storage.removeItem(test);
  1025. return true;
  1026. } catch(e) {
  1027. console.log(e);
  1028. if(e == QUOTA_EXCEEDED_ERR) {
  1029. storage.setItem("dai-cacheID", "");
  1030. storage.setItem("dai-cacheTags", "");
  1031. }
  1032. return false;
  1033. }
  1034. }
  1035.  
  1036. function xmlhttpRequest(request) {
  1037. var xReq = new XMLHttpRequest();
  1038. xReq.overrideMimeType(request.overrideMimeType);
  1039. xReq.open(request.method, request.url, true);
  1040. Object.getOwnPropertyNames(request.headers).forEach(function(header) {
  1041. xReq.setRequestHeader(header, request.headers[header]);
  1042. });
  1043. xReq.onreadystatechange = function(e) {
  1044. if(xReq.readyState == 4)
  1045. if(xReq.status > 0)
  1046. request.onload(xReq);
  1047. };
  1048. xReq.send(null);
  1049. return xReq;
  1050. }
  1051.  
  1052. d.head.appendChild(dai_css);
  1053.  
  1054. // clear old GM values
  1055. GM_deleteValue("tags");
  1056. GM_deleteValue("images");
  1057. GM_deleteValue("page");
  1058. GM_deleteValue("rating");
  1059. GM_deleteValue("column");