Danbooru Ajax Interface

New interface to search images on Booru-style sites.

2016-05-31 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

  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.  
  6. // @include http://danbooru.donmai.us/
  7. // @include http://danbooru.donmai.us/#*
  8. // @include http://www.donmai.us/
  9. // @include http://www.donmai.us/#*
  10. // @include http://donmai.us/
  11. // @include http://donmai.us/#*
  12.  
  13. // @include http://gelbooru.com/
  14. // @include http://gelbooru.com/#*
  15. // @include http://www.gelbooru.com/
  16. // @include http://www.gelbooru.com/#*
  17.  
  18. // @include http://konachan.com/
  19. // @include http://konachan.com/#*
  20. // @include http://konachan.net/
  21. // @include http://konachan.net/#*
  22.  
  23. // @include https://yande.re/post
  24. // @include https://yande.re/post#*
  25.  
  26. // @version 3.141592653
  27. // @grant GM_addStyle
  28. // @grant GM_getValue
  29. // @grant GM_setValue
  30. // @grant GM_deleteValue
  31. // @grant GM_xmlhttpRequest
  32. // ==/UserScript==
  33.  
  34. // CONSTANTS
  35. const ratio = ((1 + Math.sqrt(5)) / 2);
  36. const d = document;
  37.  
  38. // NEW SITES CAN BE ADDED NOW
  39. var sites = [
  40. {
  41. name : "Danbooru", // Site name
  42. site : "donmai\.us", // Regular expression check on current url
  43. post : "/post/index.xml", // Relative url to xml post API
  44. note : "/notes.xml", // Relative url to xml note API
  45. list : "/posts/", // Relative url to post listing
  46. page : "/posts/", // Relative url to post page
  47. query : function(tags, images, page, postid) { // Query passed to API
  48. return (postid ? "?group_by=note&limit=99999&search[post_id]=" + postid : "?tags=" + tags + (page ? "&page=" + page + "&limit=" + images : ""));
  49. }
  50. },
  51. {
  52. name : "Gelbooru",
  53. site : "(www\.)?gelbooru\.",
  54. post : "/index.php?page=dapi&s=post&q=index",
  55. note : "/index.php?page=dapi&s=note&q=index&post_id=",
  56. list : "/index.php?page=post&s=list",
  57. page : "/index.php?page=post&s=view&id=",
  58. query : function(tags, images, page, postid) {
  59. return (postid ? postid : "&tags=" + tags + "&limit=" + images + "&pid=" + (page - 1));
  60. }
  61. },
  62. {
  63. name : "Konachan",
  64. site : "konachan\.",
  65. post : "/post.xml",
  66. note : "/note.xml?post_id=",
  67. list : "/post/",
  68. page : "/post/show/",
  69. query : function(tags, images, page, postid) {
  70. return (postid ? postid : "?tags=" + tags + "&limit=" + images + "&page=" + page);
  71. }
  72. },
  73. {
  74. name : "Yande.re",
  75. site : "yande\.re",
  76. post : "/post.xml",
  77. note : "/note.xml?post_id=",
  78. list : "/post/",
  79. page : "/post/show/",
  80. query : function(tags, images, page, postid) {
  81. return (postid ? postid : "?tags=" + tags + "&limit=" + images + "&page=" + page);
  82. }
  83. }
  84. ];
  85.  
  86. var booru;
  87. for(var i = 0; i < sites.length; i++)
  88. if(new RegExp(sites[i].site).test(window.location.hostname))
  89. booru = sites[i];
  90.  
  91. var alltags = GM_getValue("tags", setDefaults("alltags", sites.length));
  92. var allpage = GM_getValue("page", setDefaults("allpage", sites.length));
  93. var allimages = GM_getValue("images", setDefaults("allimages", sites.length));
  94. var allrating = GM_getValue("rating", setDefaults("allrating", sites.length));
  95.  
  96. // PAGE CLEANING
  97. while(d.documentElement.firstChild)
  98. d.documentElement.removeChild(d.documentElement.firstChild);
  99.  
  100. d.documentElement.appendChild(d.createElement("HEAD"));
  101. d.documentElement.appendChild(d.createElement("BODY"));
  102. d.documentElement.firstChild.appendChild(title = d.createElement("TITLE"));
  103. title.appendChild(d.createTextNode(booru.name));
  104.  
  105. GM_addStyle("* { transition: all 2s ease-in-out 0s; } 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; } .trans { border: 1px solid Black; background-color: LightYellow; position: absolute; }");
  106. GM_deleteValue("columns");
  107.  
  108. // SCRIPT STARTS HERE
  109. var tags = getValue("alltags", alltags);
  110. var page = getValue("allpage", allpage);
  111. var images = getValue("allimages", allimages);
  112. var rating = getValue("allrating", allrating);
  113.  
  114. var searchTable, searchTr, searchTd, searchForm, aLink, aTags, aReply, aTableBar, aLeftBar, aCenterBar, aRightBar, aTable, aTr1, aTr2, aTr3, aTd1, aTd2, aTd3, aTd4, aTd5, aTd6, aPage, aRS, aRQ, aRE, aFIT, aSD, aLS, aLQ, aLE, aLF, aLD, aImages, aPrev, aSearch, aNext, aTagsDisplay, imagesLayer, overlay, display, innerDisplay, sampleRate = 1;
  115. var cache = [];
  116.  
  117. d.body.appendChild(searchTable = d.createElement("TABLE"));
  118. searchTable.appendChild(searchTr = d.createElement("TR"));
  119. searchTr.setAttribute("style", "vertical-align: top;");
  120. searchTr.appendChild(searchTd = d.createElement("TD"));
  121. searchTd.setAttribute("style", "text-align: center;");
  122.  
  123. searchForm = d.createElement("FORM");
  124. searchForm.addEventListener("submit", function(event) {
  125. tags = aTags.value;
  126. page = Math.max(1, parseInt(aPage.value, 10));
  127. images = Math.max(1, Math.min(parseInt(aImages.value, 10), 100));
  128. rating = (aRS.checked ? "s" : "") + (aRQ.checked ? "q" : "") + (aRE.checked ? "e" : "") + (aFIT.checked ? "f" : "") + (aSD && aSD.checked ? "d" : "");
  129.  
  130. allimages = setValue("images", allimages, images);
  131. allrating = setValue("rating", allrating, rating);
  132.  
  133. search(tags, page);
  134. event.preventDefault();
  135. }, false);
  136.  
  137. searchForm.appendChild(aLink = d.createElement("A"));
  138. aLink.setAttribute("href", booru.list);
  139. aLink.setAttribute("tabindex", "1");
  140. aLink.appendChild(d.createTextNode(booru.name));
  141.  
  142. searchForm.appendChild(d.createElement("BR"));
  143.  
  144. searchForm.appendChild(aTags = d.createElement("INPUT"));
  145. aTags.setAttribute("type", "text");
  146. aTags.setAttribute("cols", 25);
  147. aTags.setAttribute("value", tags);
  148. aTags.setAttribute("tabindex", "2");
  149. aTags.addEventListener("change", function(event) {
  150. aPage.value = 1;
  151. }, false);
  152.  
  153. searchForm.appendChild(d.createElement("P"));
  154.  
  155. searchForm.appendChild(aReply = d.createElement("SPAN"));
  156. aReply.appendChild(d.createTextNode("Nobody here but us chickens!"));
  157.  
  158. searchForm.appendChild(d.createElement("P"));
  159.  
  160. // Slider
  161. aTableBar = d.createElement("TABLE");
  162. aTableBar.appendChild(d.createElement("TR"));
  163. aTableBar.setAttribute("style", "border-collapse: collapse; border: 1px solid Black; width: 100%; padding: 0px");
  164. aLeftBar = d.createElement("TD");
  165. aLeftBar.setAttribute("style", "padding: 0px;");
  166. aTableBar.firstChild.appendChild(aLeftBar);
  167. aCenterBar = d.createElement("TD");
  168. aCenterBar.setAttribute("style", "border: 1px solid Black; padding: 1.5px 0px; background-color: WhiteSmoke; width: 25%; min-width: 1px;");
  169. aTableBar.firstChild.appendChild(aCenterBar);
  170. aRightBar = d.createElement("TD");
  171. aRightBar.setAttribute("style", "padding: 0px; width: 100%;");
  172. aTableBar.firstChild.appendChild(aRightBar);
  173. searchForm.appendChild(aTableBar);
  174.  
  175. // Search options
  176. aTable = d.createElement("TABLE");
  177. aTr1 = d.createElement("TR");
  178.  
  179. aTd1 = d.createElement("TD");
  180. aTd1.appendChild(d.createTextNode("Page:"));
  181. aTr1.appendChild(aTd1);
  182.  
  183. aTd2 = d.createElement("TD");
  184. aPage = d.createElement("INPUT");
  185. aPage.setAttribute("type", "text");
  186. aPage.setAttribute("size", "1");
  187. aPage.setAttribute("value", page);
  188. aPage.setAttribute("tabindex", "3");
  189. aTd2.appendChild(aPage);
  190. aTr1.appendChild(aTd2);
  191.  
  192. aTd3 = d.createElement("TD");
  193. aTd3.setAttribute("style", "text-align: left;");
  194. aTd3.setAttribute("rowspan", "3");
  195.  
  196. aRS = d.createElement("INPUT");
  197. aRS.setAttribute("type", "checkbox");
  198. if(/s/.test(rating)) aRS.setAttribute("checked", "checked");
  199. aLS = d.createElement("LABEL");
  200. aLS.appendChild(aRS);
  201. aLS.appendChild(d.createTextNode("Safe"));
  202.  
  203. aTd3.appendChild(aLS);
  204. aTd3.appendChild(d.createElement("BR"));
  205.  
  206. aRQ = d.createElement("INPUT");
  207. aRQ.setAttribute("type", "checkbox");
  208. if(/q/.test(rating)) aRQ.setAttribute("checked", "checked");
  209. aLQ = d.createElement("LABEL");
  210. aLQ.appendChild(aRQ);
  211. aLQ.appendChild(d.createTextNode("Questionable"));
  212.  
  213. aTd3.appendChild(aLQ);
  214. aTd3.appendChild(d.createElement("BR"));
  215.  
  216. aRE = d.createElement("INPUT");
  217. aRE.setAttribute("type", "checkbox");
  218. if(/e/.test(rating)) aRE.setAttribute("checked", "checked");
  219. aLE = d.createElement("LABEL");
  220. aLE.appendChild(aRE);
  221. aLE.appendChild(d.createTextNode("Explicit"));
  222.  
  223. aTd3.appendChild(aLE);
  224. aTd3.appendChild(d.createElement("BR"));
  225.  
  226. if(booru.name=="Danbooru") {
  227. aSD = d.createElement("INPUT");
  228. aSD.setAttribute("type", "checkbox");
  229. if(/d/.test(rating)) aSD.setAttribute("checked", "checked");
  230. aLD = d.createElement("LABEL");
  231. aLD.appendChild(aSD);
  232. aLD.appendChild(d.createTextNode("Show deleted"));
  233.  
  234. aTd3.appendChild(aLD);
  235. aTd3.appendChild(d.createElement("BR"));
  236.  
  237. aSD.addEventListener("change", function(event) {
  238. for(var i in cache)
  239. d.getElementById("content").insertBefore(cache[i], d.getElementById("content").childNodes[cache[i].style.getPropertyValue("order")]);
  240.  
  241. setTimeout(function() { // transitions are stupid
  242. deletedlist = d.evaluate("//DIV[@class='thumb'][contains(@style, 'MistyRose')]", d, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  243. if(aSD.checked) {
  244. for(var n = 0, deleted = null; deleted = deletedlist.snapshotItem(n++); n) {
  245. deleted.style.setProperty("opacity", "1", "");
  246. deleted.style.setProperty("padding", "1px", "");
  247. deleted.style.setProperty("margin", "-1px 0px 0px -1px", "");
  248. deleted.style.setProperty("border-width", "1px", "");
  249. deleted.style.setProperty("min-width", "150px", "");
  250. deleted.style.setProperty("max-width", "150px", "");
  251. }
  252. } else {
  253. cache.length = 0;
  254. for(var n = 0, deleted = null; deleted = deletedlist.snapshotItem(n++); n) {
  255. deleted.style.setProperty("opacity", "0", "");
  256. deleted.style.setProperty("padding", "0px", "");
  257. deleted.style.setProperty("margin", "0px", "");
  258. deleted.style.setProperty("border-width", "0px", "");
  259. deleted.style.setProperty("min-width", "0px", "");
  260. deleted.style.setProperty("max-width", "0px", "");
  261. cache.push(deleted);
  262. }
  263. }
  264. }, 25);
  265. }, false);
  266. } else {
  267. rating.replace("d", "");
  268. }
  269. aTr1.appendChild(aTd3);
  270.  
  271. aTr2 = d.createElement("TR");
  272.  
  273. aTd4 = d.createElement("TD");
  274. aTd4.appendChild(d.createTextNode("Images:"));
  275. aTr2.appendChild(aTd4);
  276.  
  277. aTd5 = d.createElement("TD");
  278. aImages = d.createElement("INPUT");
  279. aImages.setAttribute("type", "text");
  280. aImages.setAttribute("size", "1");
  281. aImages.setAttribute("value", images);
  282. aImages.setAttribute("tabindex", "4");
  283. aTd5.appendChild(aImages);
  284. aTr2.appendChild(aTd5);
  285.  
  286. aTr3 = d.createElement("TR");
  287.  
  288. aTd6 = d.createElement("TD");
  289. aTd6.setAttribute("colspan", "2");
  290. aFIT = d.createElement("INPUT");
  291. aFIT.setAttribute("type", "checkbox");
  292. aFIT.setAttribute("tabindex", "5");
  293. if(/f/.test(rating)) aFIT.setAttribute("checked", "checked");
  294. aLF = d.createElement("LABEL");
  295. aLF.appendChild(aFIT);
  296. aLF.appendChild(d.createTextNode("Fit width"));
  297.  
  298. aTd6.appendChild(aLF);
  299. aTr3.appendChild(aTd6);
  300.  
  301. aTable.appendChild(aTr1);
  302. aTable.appendChild(aTr2);
  303. aTable.appendChild(aTr3);
  304. searchForm.appendChild(aTable);
  305.  
  306. searchForm.appendChild(d.createElement("HR"));
  307.  
  308. searchForm.appendChild(aPrev = d.createElement("INPUT"));
  309. aPrev.setAttribute("type", "button");
  310. aPrev.setAttribute("value", "<");
  311. aPrev.setAttribute("disabled", "disabled");
  312. aPrev.addEventListener("click", function(event) {
  313. search(tags, --page);
  314. }, false);
  315.  
  316. searchForm.appendChild(aSearch = d.createElement("INPUT"));
  317. aSearch.setAttribute("type", "submit");
  318. aSearch.setAttribute("value", "Search");
  319.  
  320. searchForm.appendChild(aNext = d.createElement("INPUT"));
  321. aNext.setAttribute("type", "button");
  322. aNext.setAttribute("value", ">");
  323. aNext.setAttribute("disabled", "disabled");
  324. aNext.addEventListener("click", function(event) {
  325. search(tags, ++page);
  326. }, false);
  327.  
  328. searchForm.appendChild(d.createElement("HR"));
  329.  
  330. searchForm.appendChild(aTagsDisplay = d.createElement("DIV"));
  331. aTagsDisplay.setAttribute("style", "overflow-x: hidden;");
  332.  
  333. searchTd.appendChild(searchForm);
  334. searchTr.appendChild(imagesLayer = d.createElement("TD"));
  335.  
  336. fixsize = (searchTd.getBoundingClientRect().right - searchTd.getBoundingClientRect().left) + "px";
  337. searchTd.style.setProperty("min-width", fixsize, "important");
  338. searchTd.style.setProperty("max-width", fixsize, "important");
  339.  
  340. // "Lightbox"
  341. overlay = d.createElement("DIV");
  342. overlay.setAttribute("style", "display: none; position: fixed; top: 0px; left: 0px; width: 100%; height: 100%; background-color: Black; opacity: 0.8;");
  343. innerDisplay = d.createElement("DIV");
  344. innerDisplay.setAttribute("id", "innerDisplay");
  345. innerDisplay.setAttribute("style", "background-color: White; display: inline-block; padding: 10px; min-width: 200px; min-height: 200px; border-radius: 10px;");
  346. display = d.createElement("TABLE");
  347. display.setAttribute("style", "display: none; position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; text-align: center; vertical-align: middle;");
  348. display.appendChild(d.createElement("TR"));
  349. display.firstChild.appendChild(d.createElement("TD"));
  350. display.firstChild.firstChild.appendChild(innerDisplay);
  351.  
  352. d.body.insertBefore(display, d.body.firstChild);
  353. d.body.insertBefore(overlay, d.body.firstChild);
  354.  
  355. overlay.addEventListener("click", function(event) {
  356. if(event.target.id || d.evaluate("ancestor-or-self::div[contains(@id, 'bodynote')]", event.target, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue) return;
  357. overlay.style.setProperty("display", "none", "");
  358. display.style.setProperty("display", "none", "");
  359. }, false);
  360.  
  361. display.addEventListener("click", function(event) {
  362. if(event.target.id || d.evaluate("ancestor-or-self::div[contains(@id, 'bodynote')]", event.target, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue) return;
  363. overlay.style.setProperty("display", "none", "");
  364. display.style.setProperty("display", "none", "");
  365. }, false);
  366.  
  367. display.addEventListener("dblclick", function(event) {
  368. overlay.style.setProperty("display", "none", "");
  369. display.style.setProperty("display", "none", "");
  370. }, false);
  371.  
  372. // CTRL+S to save image
  373. var keylistener = function(event) {
  374. if(display.style.getPropertyValue("display") == "none")
  375. return
  376. else
  377. if(event.ctrlKey && event.keyCode == 83) { // CTRL+S
  378. event.preventDefault();
  379. event.stopPropagation();
  380. } else
  381. if(aFIT.checked && event.keyCode == 37) { // LEFT KEY
  382. next = openimage.parentNode.previousElementSibling || openimage.parentNode.parentNode.lastChild;
  383. next.firstChild.firstChild.click();
  384. } else
  385. if(aFIT.checked && event.keyCode == 39) { // RIGHT KEY
  386. next = openimage.parentNode.nextElementSibling || openimage.parentNode.parentNode.firstChild;
  387. next.firstChild.firstChild.click();
  388. }
  389. };
  390. var saveimage = function(event) {
  391. if(event.ctrlKey && event.keyCode == 83) { // CTRL+S
  392. var sauce = openimage.href.match(/[^\/]+$/)[0];
  393. if(/%/.test(sauce)) sauce = decodeURIComponent(sauce);
  394. var imgdown = d.createElement("A");
  395. imgdown.setAttribute("download", sauce);
  396. imgdown.setAttribute("href", openimage.href);
  397. d.body.appendChild(imgdown);
  398. imgdown.click();
  399. imgdown.remove();
  400. }
  401. };
  402. window.addEventListener("keydown", keylistener, false);
  403. window.addEventListener("keyup", saveimage, false);
  404.  
  405. if(!window.location.origin)
  406. window.location.origin = (window.location.protocol + "//" + window.location.host);
  407.  
  408. if(window.location.hash)
  409. search(window.location.hash.split("#")[1], 1);
  410.  
  411. var requestPost, requestNote, requestCount;
  412.  
  413. function search(newtags, newpage) {
  414. if(requestPost)
  415. requestPost.abort();
  416. if(requestNote)
  417. requestNote.abort();
  418. if(requestCount)
  419. requestCount.abort();
  420.  
  421. aTags.value = tags = newtags;
  422. aPage.value = page = newpage;
  423. aImages.value = images;
  424.  
  425. alltags = setValue("tags", alltags, tags);
  426. allpage = setValue("page", allpage, page);
  427.  
  428. aPrev.disabled = (newpage < 2);
  429. aRS.checked = /s/.test(rating);
  430. aRQ.checked = /q/.test(rating);
  431. aRE.checked = /e/.test(rating);
  432.  
  433. if(/^sf?d?$/.test(rating))
  434. newtags += " rating:safe";
  435. if(/^qf?d?$/.test(rating))
  436. newtags += " rating:questionable";
  437. if(/^ef?d?$/.test(rating))
  438. newtags += " rating:explicit";
  439. if(/^qef?d?$/.test(rating))
  440. newtags += " -rating:safe";
  441. if(/^sef?d?$/.test(rating))
  442. newtags += " -rating:questionable";
  443. if(/^sqf?d?$/.test(rating))
  444. newtags += " -rating:explicit";
  445.  
  446. if(imagesLayer.hasChildNodes())
  447. imagesLayer.removeChild(imagesLayer.firstChild);
  448.  
  449. imagesLayer.appendChild(d.createTextNode("Loading..."));
  450. requestPost = GM_xmlhttpRequest({
  451. method : "GET",
  452. url : window.location.origin + booru.post + booru.query(encodeURIComponent(newtags), images, page),
  453. headers : {
  454. "Accept" : "application/xml",
  455. "Cookie" : d.cookie
  456. },
  457. overrideMimeType : "application/xml; charset=utf-8",
  458. onload : function(response) {
  459. getContent(new DOMParser().parseFromString(response.responseText, "application/xml"), newtags);
  460. }
  461. });
  462. }
  463.  
  464. function showContent(xmldoc) {
  465. if(imagesLayer.hasChildNodes())
  466. imagesLayer.removeChild(imagesLayer.firstChild);
  467.  
  468. aReply.textContent = "Nobody here but us chickens!";
  469.  
  470. var posts = xmldoc.evaluate("posts", xmldoc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  471. if(!posts) {
  472. reason = xmldoc.evaluate("response/@reason", xmldoc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  473. if(reason)
  474. imagesLayer.textContent = reason.nodeValue;
  475. else
  476. imagesLayer.textContent = "Something broke.";
  477. return;
  478. }
  479. var post = xmldoc.evaluate("posts/post", xmldoc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  480. if(((parseInt(posts.getAttribute("offset"), 10) + 1) > posts.getAttribute("count")) & posts.getAttribute("count") > 0) {
  481. search(tags, Math.ceil(parseInt(posts.getAttribute("count"), 10) / images));
  482. return;
  483. }
  484.  
  485. if(posts.getAttribute("count") > 0) aReply.textContent = "Found " + posts.getAttribute("count") + ", showing " + (parseInt(posts.getAttribute("offset"), 10) + 1) + "-" + (Math.min(posts.getAttribute("count"), parseInt(posts.getAttribute("offset"), 10) + images));
  486. aLeftBar.style.setProperty("width", (posts.getAttribute("offset") / posts.getAttribute("count") * 100) + "%");
  487. aCenterBar.style.setProperty("width", (images / posts.getAttribute("count") * 100) + "%");
  488.  
  489. aNext.disabled = (page >= Math.ceil(posts.getAttribute("count") / images));
  490.  
  491. imagesLayer.appendChild(content = d.createElement("DIV"));
  492. content.setAttribute("style", "display: flex; flex-flow: row wrap;");
  493. content.setAttribute("id", "content");
  494. cache.length = 0;
  495.  
  496. for(var i = 0; i < images; i++) {
  497. data = post.snapshotItem(i);
  498. if(data) {
  499. content.appendChild(thumb = d.createElement("DIV"));
  500. if(/true/.test(data.getAttribute("has_notes")))
  501. thumb.style.setProperty("background-color", "LightYellow", "");
  502. if(/deleted/.test(data.getAttribute("status"))) {
  503. thumb.style.setProperty("background-color", "MistyRose", "");
  504. thumb.addEventListener("transitionend", function() { if(!aSD.checked) this.remove(); }, false);
  505. if(!aSD.checked) {
  506. thumb.style.setProperty("opacity", "0", "");
  507. thumb.style.setProperty("padding", "0", "");
  508. thumb.style.setProperty("margin", "0", "");
  509. thumb.style.setProperty("border-width", "0px", "");
  510. thumb.style.setProperty("min-width", "0px", "");
  511. thumb.style.setProperty("max-width", "0px", "");
  512. cache.push(thumb);
  513. thumb.remove();
  514. }
  515. }
  516.  
  517. var image = d.createElement("IMG");
  518. image.setAttribute("width", (data.getAttribute("preview_width") || "auto"));
  519. image.setAttribute("height", (data.getAttribute("preview_height") || "auto"));
  520. image.setAttribute("src", data.getAttribute("preview_url"));
  521. image.setAttribute("id", data.getAttribute("id"));
  522. image.setAttribute("alt", data.getAttribute("tags").trim());
  523. image.setAttribute("title", data.getAttribute("tags").trim() + " rating:" + data.getAttribute("rating").replace(/e$/, "Explicit").replace(/s$/, "Safe").replace(/q$/, "Questionable") + " score:" + data.getAttribute("score"));
  524.  
  525. if(/\.zip$/.test(data.getAttribute("file_url")))
  526. data.setAttribute("file_url", data.getAttribute("preview_url").replace("preview/", "sample/sample-").replace(/\..+$/, ".webm"));
  527.  
  528. image.setAttribute("fullsize", data.getAttribute("file_url"));
  529. image.setAttribute("fullwidth", data.getAttribute("width"));
  530. image.setAttribute("fullheight", data.getAttribute("height"));
  531. image.setAttribute("notes", data.getAttribute("has_notes"));
  532. image.setAttribute("md5", data.getAttribute("md5"));
  533.  
  534. // Show tags on sidebar
  535. image.addEventListener("click", function(event) {
  536. if(aTagsDisplay.hasChildNodes())
  537. aTagsDisplay.removeChild(aTagsDisplay.firstChild);
  538. aTagsDisplay.appendChild(d.createElement("DIV"));
  539.  
  540. var tagnames = this.getAttribute("alt").split(" ");
  541. for(var t = 0; t < tagnames.length; t++) {
  542. var taglink = d.createElement("A");
  543. taglink.appendChild(d.createTextNode(tagnames[t]));
  544. taglink.setAttribute("href", "#" + tagnames[t]);
  545. taglink.addEventListener("click", function(event) {
  546. aTags.value = this.textContent;
  547. aPage.value = 1;
  548. aSearch.click();
  549. event.preventDefault();
  550. }, false);
  551. aTagsDisplay.firstChild.appendChild(taglink);
  552. aTagsDisplay.firstChild.appendChild(document.createElement("BR"));
  553. }
  554.  
  555. // CTRL + click to show only the tags
  556. if(event.ctrlKey) return;
  557.  
  558. // "Lightbox" by VIPPER ("How do I jQuery?")
  559. while(innerDisplay.hasChildNodes())
  560. innerDisplay.removeChild(innerDisplay.firstChild);
  561.  
  562. if(/\.swf$/.test(this.getAttribute("fullsize"))) {
  563. fullsize = d.createElement("EMBED");
  564. } else if(/\.webm$|\.zip$/.test(this.getAttribute("fullsize"))) {
  565. fullsize = d.createElement("VIDEO");
  566. fullsize.setAttribute("controls", true);
  567. fullsize.setAttribute("loop", true);
  568. fullsize.setAttribute("autoplay", true);
  569. if(/\.zip$/.test(this.getAttribute("fullsize")))
  570. this.setAttribute("fullsize", this.getAttribute("fullsize").replace("data/", "data/sample/sample-").replace(".zip", ".webm"));
  571. } else {
  572. fullsize = d.createElement("IMG");
  573. fullsize.addEventListener("click", function(event) {
  574. noteDivs = d.evaluate("./DIV[starts-with(@id, 'note')]", innerDisplay, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  575. for(var n = 0, note = null; note = noteDivs.snapshotItem(n++); n) {
  576. //if(window.getComputedStyle(note, null).getPropertyValue("visibility") == "visible")
  577. if(note.style.getPropertyValue("visibility") == "visible")
  578. note.style.setProperty("visibility", "hidden", "");
  579. else
  580. note.style.setProperty("visibility", "visible", "");
  581. }
  582. }, false);
  583. }
  584. fullsize.setAttribute("src", this.getAttribute("fullsize"));
  585. fullsize.setAttribute("width", this.getAttribute("fullwidth"));
  586. fullsize.setAttribute("height", this.getAttribute("fullheight"));
  587. fullsize.setAttribute("id", this.getAttribute("id"));
  588. fullsize.setAttribute("notes", this.getAttribute("notes"));
  589. fullsize.setAttribute("md5", this.getAttribute("md5"));
  590. fullsize.style.setProperty("transition-property", "none", "");
  591.  
  592. if(fullsize.getAttribute("notes") == "true") {
  593. requestNote = GM_xmlhttpRequest({
  594. method : "GET",
  595. url : window.location.origin + booru.note + booru.query(null, null, null, fullsize.getAttribute("id")),
  596. headers : {
  597. "Accept" : "application/xml",
  598. "Cookie" : d.cookie
  599. },
  600. overrideMimeType : "application/xml; charset=utf-8",
  601. onload : function(response) {
  602. getNotes(new DOMParser().parseFromString(response.responseText, "application/xml"), response.finalUrl);
  603. }
  604. });
  605. }
  606.  
  607. var pagelink = d.createElement("A");
  608. pagelink.setAttribute("href", booru.page + fullsize.getAttribute("id"));
  609. pagelink.appendChild(d.createTextNode(fullsize.getAttribute("id")));
  610.  
  611. innerDisplay.appendChild(fullsize);
  612. innerDisplay.appendChild(d.createElement("BR"));
  613. innerDisplay.appendChild(pagelink);
  614.  
  615. overlay.style.setProperty("display", "", "");
  616. display.style.setProperty("display", "", "");
  617.  
  618. sampleRate = 1;
  619. var clientwidth = Math.min(d.documentElement.clientWidth, d.body.clientWidth) - 22;
  620. if(aFIT.checked && this.getAttribute("fullwidth") > clientwidth) {
  621. sampleRate = clientwidth / this.getAttribute("fullwidth");
  622. fullsize.setAttribute("width", clientwidth);
  623. fullsize.setAttribute("height", this.getAttribute("fullheight") * sampleRate);
  624. }
  625. }, false);
  626.  
  627. var link = d.createElement("A");
  628. link.setAttribute("href", data.getAttribute("file_url"));
  629. link.setAttribute("alt", data.getAttribute("id"));
  630. link.appendChild(image);
  631. link.addEventListener("click", function(event) {
  632. openimage = this;
  633. event.preventDefault();
  634. }, false);
  635. thumb.setAttribute("class", "thumb");
  636. thumb.style.setProperty("order", i, "");
  637. thumb.appendChild(link);
  638. }
  639. }
  640. }
  641.  
  642. function getContent(xmldoc, newtags) {
  643. if(booru.name == "Danbooru") { // Inject the count where it should be by default...
  644. requestCount = GM_xmlhttpRequest({
  645. method : "GET",
  646. url : window.location.origin + "/counts/posts.xml" + booru.query(encodeURIComponent(newtags)),
  647. headers : {
  648. "Accept" : "application/xml",
  649. "Cookie" : d.cookie
  650. },
  651. overrideMimeType : "application/xml; charset=utf-8",
  652. onload : function(response) {
  653. newxmldoc = new DOMParser().parseFromString(response.responseText, "application/xml");
  654. posts = xmldoc.evaluate("posts", xmldoc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  655. count = newxmldoc.evaluate("counts/posts", newxmldoc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  656. if(posts) {
  657. posts.setAttribute("count", count ? count.textContent.trim() : "0");
  658. posts.setAttribute("offset", (page - 1) * images);
  659. }
  660. showContent(xmldoc);
  661. }
  662. });
  663. } else {
  664. showContent(xmldoc);
  665. }
  666. }
  667.  
  668. function showNotes(note, id) {
  669. var offsetx = fullsize.getBoundingClientRect().left + (d.documentElement.scrollLeft || d.body.scrollLeft);
  670. var offsety = fullsize.getBoundingClientRect().top + (d.documentElement.scrollTop || d.body.scrollTop);
  671. var vp_bottom = Math.max(window.innerHeight, innerDisplay.getBoundingClientRect().bottom - innerDisplay.getBoundingClientRect().top);
  672.  
  673. for(var i = 0, ndata = null; ndata = note.snapshotItem(i++); i) {
  674. if(id.match(/[0-9]+$/)[0] != fullsize.getAttribute("id")) continue;
  675. if(d.getElementById("note" + ndata.getAttribute("id"))) continue;
  676.  
  677. var noteDiv = d.createElement("DIV");
  678. noteDiv.setAttribute("id", "note" + ndata.getAttribute("id"));
  679. noteDiv.setAttribute("class", "trans");
  680. noteDiv.setAttribute("style", "opacity: 0.4; transition: none !important;");
  681. noteDiv.style.setProperty("left", ndata.getAttribute("x") * sampleRate + offsetx + "px", "");
  682. noteDiv.style.setProperty("top", ndata.getAttribute("y") * sampleRate + offsety + "px", "");
  683. noteDiv.style.setProperty("width", ndata.getAttribute("width") * sampleRate + "px", "");
  684. noteDiv.style.setProperty("height", ndata.getAttribute("height") * sampleRate + "px", "");
  685. noteDiv.addEventListener("mouseover", function(event) {
  686. //clearTimeout(noteclear);
  687. //d.getElementById("body" + this.getAttribute("id")).style.setProperty("display", "", "");
  688. noteclear = setTimeout(function() { d.getElementById("body" + this.getAttribute("id")).style.setProperty("display", "", ""); }.bind(this), 100);
  689. }, false);
  690. noteDiv.addEventListener("mouseout", function(event) {
  691. noteclear = setTimeout(function() { d.getElementById("body" + this.getAttribute("id")).style.setProperty("display", "none", ""); }.bind(this), 200);
  692. }, false);
  693. innerDisplay.appendChild(noteDiv);
  694.  
  695. var noteBody = d.createElement("DIV");
  696. noteBody.innerHTML = ndata.getAttribute("body");
  697. noteBody.setAttribute("id", "bodynote" + ndata.getAttribute("id"));
  698. noteBody.setAttribute("class", "trans");
  699. noteBody.setAttribute("style", "color: Black; text-align: left; padding: 4px; z-index: 1" + i + ";");
  700. noteBody.addEventListener("mouseover", function(event) {
  701. clearTimeout(noteclear);
  702. this.style.setProperty("display", "", "");
  703. }, false);
  704. noteBody.addEventListener("mouseout", function(event) {
  705. this.style.setProperty("display", "none", "");
  706. }, false);
  707. innerDisplay.appendChild(noteBody);
  708.  
  709. // this sucks, find another method!
  710. var w = ndata.getAttribute("width") * sampleRate;
  711. var h = ndata.getAttribute("height") * sampleRate;
  712. if(w < h) { w ^= h; h ^= w; w ^= h; } // FUCK YEAH XOR SWAP
  713. while(w / h > ratio) {
  714. w -= ratio;
  715. h += ratio;
  716. }
  717.  
  718. noteBody.style.setProperty("min-width", "-moz-min-content", "");
  719. noteBody.style.setProperty("min-width", "-webkit-min-content", "");
  720. noteBody.style.setProperty("max-width", w + "px", "");
  721.  
  722. ntop = (ndata.getAttribute("y") * sampleRate) + (ndata.getAttribute("height") * sampleRate) + offsety + 5;
  723. nheight = noteBody.getBoundingClientRect().bottom - noteBody.getBoundingClientRect().top;
  724. if(ntop + nheight > vp_bottom)
  725. noteBody.style.setProperty("top", vp_bottom - nheight + "px", "");
  726. else
  727. noteBody.style.setProperty("top", ntop + "px", "");
  728.  
  729. noteBody.style.setProperty("left", ndata.getAttribute("x") * sampleRate + offsetx + "px", "");
  730. noteBody.style.setProperty("display", "none", "");
  731. }
  732. }
  733.  
  734. function getNotes(xmldoc, id) {
  735. if(booru.name == "Danbooru") { // Parses the nodes as attributes for each note
  736. notes = xmldoc.evaluate("notes/note/is-active[text() = 'true']/..", xmldoc, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  737. for(var i = 0, note = null; note = notes.snapshotItem(i++); i) {
  738. while(note.firstChild) {
  739. if(note.firstChild.nodeName != "#text")
  740. note.setAttribute(note.firstChild.nodeName, note.firstChild.textContent);
  741. note.removeChild(note.firstChild);
  742. }
  743. }
  744. showNotes(xmldoc.evaluate("notes/note[@is-active = 'true']", xmldoc, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null), id);
  745. } else {
  746. showNotes(xmldoc.evaluate("notes/note[@is_active = 'true']", xmldoc, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null), id);
  747. }
  748. }
  749.  
  750. function getValue(name, values) {
  751. for(var i = 0; i < sites.length; i++)
  752. if(new RegExp(sites[i].site).test(window.location.hostname)) {
  753. if(eval(values)[i] != "" && !eval(values)[i]) {
  754. values = setValue(name.replace(/all/, ""), values, setDefaults(name, 1));
  755. }
  756. return eval(values)[i];
  757. }
  758. }
  759.  
  760. function setValue(name, values, newvalue) {
  761. for(var i = 0; i < sites.length; i++)
  762. if(new RegExp(sites[i].site).test(window.location.hostname)) {
  763. oldvalue = eval(values);
  764. oldvalue[i] = (newvalue == "" ? "" : newvalue || oldvalue[i]);
  765. GM_setValue(name, JSON.stringify(oldvalue));
  766. return(JSON.stringify(oldvalue));
  767. }
  768. }
  769.  
  770. function setDefaults(name, all) {
  771. var def, string = "[";
  772. for(var i = 0; i < all; i++) {
  773. if(name == "alltags")
  774. def = "\"\"";
  775. if(name == "allpage")
  776. def = "1";
  777. if(name == "allimages")
  778. def = "20";
  779. if(name == "allrating")
  780. def = "\"s\"";
  781. string += (def) + (i < sites.length - 1 ? ", " : "]");
  782. }
  783. return (all > 1 ? string : eval(def));
  784. }