Danbooru Ajax Interface

New interface to search images on Booru-style sites.

Ajankohdalta 10.2.2016. Katso uusin versio.

  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.1415926
  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. // CTRL+S to save image
  356. var keylistener = function(event) {
  357. if(display.style.getPropertyValue("display") == "none")
  358. return
  359. else
  360. if(event.ctrlKey && event.keyCode == 83) { // CTRL+S
  361. event.preventDefault();
  362. event.stopPropagation();
  363. } else
  364. if(aFIT.checked && event.keyCode == 37) { // LEFT KEY
  365. next = openimage.parentNode.previousElementSibling || openimage.parentNode.parentNode.lastChild;
  366. next.firstChild.firstChild.click();
  367. } else
  368. if(aFIT.checked && event.keyCode == 39) { // RIGHT KEY
  369. next = openimage.parentNode.nextElementSibling || openimage.parentNode.parentNode.firstChild;
  370. next.firstChild.firstChild.click();
  371. }
  372. };
  373. var saveimage = function(event) {
  374. if(event.ctrlKey && event.keyCode == 83) { // CTRL+S
  375. var sauce = openimage.href.match(/[^\/]+$/)[0];
  376. if(/%/.test(sauce)) sauce = decodeURIComponent(sauce);
  377. var imgdown = d.createElement("A");
  378. imgdown.setAttribute("download", sauce);
  379. imgdown.setAttribute("href", openimage.href);
  380. d.body.appendChild(imgdown);
  381. imgdown.click();
  382. imgdown.remove();
  383. }
  384. };
  385. window.addEventListener("keydown", keylistener, false);
  386. window.addEventListener("keyup", saveimage, false);
  387.  
  388. if(!window.location.origin)
  389. window.location.origin = (window.location.protocol + "//" + window.location.host);
  390.  
  391. if(window.location.hash)
  392. search(window.location.hash.split("#")[1], 1);
  393.  
  394. var requestPost, requestNote, requestCount;
  395.  
  396. function search(newtags, newpage) {
  397. if(requestPost)
  398. requestPost.abort();
  399. if(requestNote)
  400. requestNote.abort();
  401. if(requestCount)
  402. requestCount.abort();
  403.  
  404. aTags.value = tags = newtags;
  405. aPage.value = page = newpage;
  406. aImages.value = images;
  407.  
  408. alltags = setValue("tags", alltags, tags);
  409. allpage = setValue("page", allpage, page);
  410.  
  411. aPrev.disabled = (newpage < 2);
  412. aRS.checked = /s/.test(rating);
  413. aRQ.checked = /q/.test(rating);
  414. aRE.checked = /e/.test(rating);
  415.  
  416. if(/^sf?d?$/.test(rating))
  417. newtags += " rating:safe";
  418. if(/^qf?d?$/.test(rating))
  419. newtags += " rating:questionable";
  420. if(/^ef?d?$/.test(rating))
  421. newtags += " rating:explicit";
  422. if(/^qef?d?$/.test(rating))
  423. newtags += " -rating:safe";
  424. if(/^sef?d?$/.test(rating))
  425. newtags += " -rating:questionable";
  426. if(/^sqf?d?$/.test(rating))
  427. newtags += " -rating:explicit";
  428.  
  429. if(imagesLayer.hasChildNodes())
  430. imagesLayer.removeChild(imagesLayer.firstChild);
  431.  
  432. imagesLayer.appendChild(d.createTextNode("Loading..."));
  433. requestPost = GM_xmlhttpRequest({
  434. method : "GET",
  435. url : window.location.origin + booru.post + booru.query(encodeURIComponent(newtags), images, page),
  436. headers : {
  437. "Accept" : "application/xml",
  438. "Cookie" : d.cookie
  439. },
  440. overrideMimeType : "application/xml; charset=utf-8",
  441. onload : function(response) {
  442. getContent(new DOMParser().parseFromString(response.responseText, "application/xml"), newtags);
  443. }
  444. });
  445. }
  446.  
  447. function showContent(xmldoc) {
  448. if(imagesLayer.hasChildNodes())
  449. imagesLayer.removeChild(imagesLayer.firstChild);
  450.  
  451. aReply.textContent = "Nobody here but us chickens!";
  452.  
  453. var posts = xmldoc.evaluate("posts", xmldoc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  454. if(!posts) {
  455. reason = xmldoc.evaluate("response/@reason", xmldoc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  456. if(reason)
  457. imagesLayer.textContent = reason.nodeValue;
  458. else
  459. imagesLayer.textContent = "Something broke.";
  460. return;
  461. }
  462. var post = xmldoc.evaluate("posts/post", xmldoc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  463. if(((parseInt(posts.getAttribute("offset"), 10) + 1) > posts.getAttribute("count")) & posts.getAttribute("count") > 0) {
  464. search(tags, Math.ceil(parseInt(posts.getAttribute("count"), 10) / images));
  465. return;
  466. }
  467.  
  468. 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));
  469. aLeftBar.style.setProperty("width", (posts.getAttribute("offset") / posts.getAttribute("count") * 100) + "%");
  470. aCenterBar.style.setProperty("width", (images / posts.getAttribute("count") * 100) + "%");
  471.  
  472. aNext.disabled = (page >= Math.ceil(posts.getAttribute("count") / images));
  473.  
  474. imagesLayer.appendChild(content = d.createElement("DIV"));
  475. content.setAttribute("style", "display: flex; flex-flow: row wrap;");
  476. content.setAttribute("id", "content");
  477. cache.length = 0;
  478.  
  479. for(var i = 0; i < images; i++) {
  480. content.appendChild(thumb = d.createElement("DIV"));
  481. data = post.snapshotItem(i);
  482. if(data) {
  483. if(/true/.test(data.getAttribute("has_notes")))
  484. thumb.style.setProperty("background-color", "LightYellow", "");
  485. if(/deleted/.test(data.getAttribute("status"))) {
  486. thumb.style.setProperty("background-color", "MistyRose", "");
  487. thumb.addEventListener("transitionend", function() { if(!aSD.checked) this.remove(); }, false);
  488. if(!aSD.checked) {
  489. thumb.style.setProperty("opacity", "0", "");
  490. thumb.style.setProperty("padding", "0", "");
  491. thumb.style.setProperty("margin", "0", "");
  492. thumb.style.setProperty("border-width", "0px", "");
  493. thumb.style.setProperty("min-width", "0px", "");
  494. thumb.style.setProperty("max-width", "0px", "");
  495. cache.push(thumb);
  496. thumb.remove();
  497. }
  498. }
  499.  
  500. var image = d.createElement("IMG");
  501. image.setAttribute("width", (data.getAttribute("preview_width") || "auto"));
  502. image.setAttribute("height", (data.getAttribute("preview_height") || "auto"));
  503. image.setAttribute("src", data.getAttribute("preview_url"));
  504. image.setAttribute("id", data.getAttribute("id"));
  505. image.setAttribute("alt", data.getAttribute("tags").trim());
  506. image.setAttribute("title", data.getAttribute("tags").trim() + " rating:" + data.getAttribute("rating").replace(/e$/, "Explicit").replace(/s$/, "Safe").replace(/q$/, "Questionable") + " score:" + data.getAttribute("score"));
  507.  
  508. if(/\.zip$/.test(data.getAttribute("file_url")))
  509. data.setAttribute("file_url", data.getAttribute("file_url").replace("data/", "data/sample/sample-").replace(".zip", ".webm"));
  510.  
  511. image.setAttribute("fullsize", data.getAttribute("file_url"));
  512. image.setAttribute("fullwidth", data.getAttribute("width"));
  513. image.setAttribute("fullheight", data.getAttribute("height"));
  514. image.setAttribute("notes", data.getAttribute("has_notes"));
  515. image.setAttribute("md5", data.getAttribute("md5"));
  516.  
  517. // Show tags on sidebar
  518. image.addEventListener("click", function(event) {
  519. if(aTagsDisplay.hasChildNodes())
  520. aTagsDisplay.removeChild(aTagsDisplay.firstChild);
  521. aTagsDisplay.appendChild(d.createElement("DIV"));
  522.  
  523. var tagnames = this.getAttribute("alt").split(" ");
  524. for(var t = 0; t < tagnames.length; t++) {
  525. var taglink = d.createElement("A");
  526. taglink.appendChild(d.createTextNode(tagnames[t]));
  527. taglink.setAttribute("href", "#" + tagnames[t]);
  528. taglink.addEventListener("click", function(event) {
  529. aTags.value = this.textContent;
  530. aPage.value = 1;
  531. aSearch.click();
  532. event.preventDefault();
  533. }, false);
  534. aTagsDisplay.firstChild.appendChild(taglink);
  535. aTagsDisplay.firstChild.appendChild(document.createElement("BR"));
  536. }
  537.  
  538. // CTRL + click to show only the tags
  539. if(event.ctrlKey) return;
  540.  
  541. // "Lightbox" by VIPPER ("How do I jQuery?")
  542. while(innerDisplay.hasChildNodes())
  543. innerDisplay.removeChild(innerDisplay.firstChild);
  544.  
  545. overlay.addEventListener("click", function(event) {
  546. if(event.target.id) return;
  547. overlay.style.setProperty("display", "none", "");
  548. display.style.setProperty("display", "none", "");
  549. }, false);
  550.  
  551. display.addEventListener("click", function(event) {
  552. if(event.target.id) return;
  553. overlay.style.setProperty("display", "none", "");
  554. display.style.setProperty("display", "none", "");
  555. }, false);
  556.  
  557. display.addEventListener("dblclick", function(event) {
  558. overlay.style.setProperty("display", "none", "");
  559. display.style.setProperty("display", "none", "");
  560. }, false);
  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')]", d, null, XPathResult.UNORDERED_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. note.style.setProperty("visibility", "hidden", "");
  578. else
  579. note.style.setProperty("visibility", "visible", "");
  580. }
  581. }, false);
  582. }
  583. fullsize.setAttribute("src", this.getAttribute("fullsize"));
  584. fullsize.setAttribute("width", this.getAttribute("fullwidth"));
  585. fullsize.setAttribute("height", this.getAttribute("fullheight"));
  586. fullsize.setAttribute("id", this.getAttribute("id"));
  587. fullsize.setAttribute("notes", this.getAttribute("notes"));
  588. fullsize.setAttribute("md5", this.getAttribute("md5"));
  589. fullsize.style.setProperty("transition-property", "none", "");
  590.  
  591. if(fullsize.getAttribute("notes") == "true") {
  592. requestNote = GM_xmlhttpRequest({
  593. method : "GET",
  594. url : window.location.origin + booru.note + booru.query(null, null, null, fullsize.getAttribute("id")),
  595. headers : {
  596. "Accept" : "application/xml",
  597. "Cookie" : d.cookie
  598. },
  599. overrideMimeType : "application/xml; charset=utf-8",
  600. onload : function(response) {
  601. getNotes(new DOMParser().parseFromString(response.responseText, "application/xml"), response.finalUrl);
  602. }
  603. });
  604. }
  605.  
  606. var pagelink = d.createElement("A");
  607. pagelink.setAttribute("href", booru.page + fullsize.getAttribute("id"));
  608. pagelink.appendChild(d.createTextNode(fullsize.getAttribute("id")));
  609.  
  610. innerDisplay.appendChild(fullsize);
  611. innerDisplay.appendChild(d.createElement("BR"));
  612. innerDisplay.appendChild(pagelink);
  613.  
  614. overlay.style.setProperty("display", "", "");
  615. display.style.setProperty("display", "", "");
  616.  
  617. sampleRate = 1;
  618. var clientwidth = Math.min(d.documentElement.clientWidth, d.body.clientWidth) - 22;
  619. if(aFIT.checked && this.getAttribute("fullwidth") > clientwidth) {
  620. sampleRate = clientwidth / this.getAttribute("fullwidth");
  621. fullsize.setAttribute("width", clientwidth);
  622. fullsize.setAttribute("height", this.getAttribute("fullheight") * sampleRate);
  623. }
  624. }, false);
  625.  
  626. var link = d.createElement("A");
  627. link.setAttribute("href", data.getAttribute("file_url"));
  628. link.setAttribute("alt", data.getAttribute("id"));
  629. link.appendChild(image);
  630. link.addEventListener("click", function(event) {
  631. openimage = this;
  632. event.preventDefault();
  633. }, false);
  634. thumb.setAttribute("class", "thumb");
  635. thumb.style.setProperty("order", i, "");
  636. thumb.appendChild(link);
  637. }
  638. }
  639. }
  640.  
  641. function getContent(xmldoc, newtags) {
  642. if(booru.name == "Danbooru") { // Inject the count where it should be by default...
  643. requestCount = GM_xmlhttpRequest({
  644. method : "GET",
  645. url : window.location.origin + "/counts/posts.xml" + booru.query(encodeURIComponent(newtags)),
  646. headers : {
  647. "Accept" : "application/xml",
  648. "Cookie" : d.cookie
  649. },
  650. overrideMimeType : "application/xml; charset=utf-8",
  651. onload : function(response) {
  652. newxmldoc = new DOMParser().parseFromString(response.responseText, "application/xml");
  653. posts = xmldoc.evaluate("posts", xmldoc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  654. count = newxmldoc.evaluate("counts/posts", newxmldoc, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  655. if(posts) {
  656. posts.setAttribute("count", count ? count.textContent.trim() : "0");
  657. posts.setAttribute("offset", (page - 1) * images);
  658. }
  659. showContent(xmldoc);
  660. }
  661. });
  662. } else {
  663. showContent(xmldoc);
  664. }
  665. }
  666.  
  667. function showNotes(note, id) {
  668. var offsetx = fullsize.getBoundingClientRect().left + (d.documentElement.scrollLeft || d.body.scrollLeft);
  669. var offsety = fullsize.getBoundingClientRect().top + (d.documentElement.scrollTop || d.body.scrollTop);
  670. var vp_bottom = Math.max(window.innerHeight, innerDisplay.getBoundingClientRect().bottom - innerDisplay.getBoundingClientRect().top);
  671.  
  672. for(var i = 0, ndata = null; ndata = note.snapshotItem(i++); i) {
  673. if(id.match(/[0-9]+$/)[0] != fullsize.getAttribute("id")) continue;
  674. if(d.getElementById("note" + ndata.getAttribute("id"))) continue;
  675.  
  676. var noteDiv = d.createElement("DIV");
  677. noteDiv.setAttribute("id", "note" + ndata.getAttribute("id"));
  678. noteDiv.setAttribute("class", "trans");
  679. noteDiv.setAttribute("style", "opacity: 0.4;");
  680. noteDiv.style.setProperty("left", ndata.getAttribute("x") * sampleRate + offsetx + "px", "");
  681. noteDiv.style.setProperty("top", ndata.getAttribute("y") * sampleRate + offsety + "px", "");
  682. noteDiv.style.setProperty("width", ndata.getAttribute("width") * sampleRate + "px", "");
  683. noteDiv.style.setProperty("height", ndata.getAttribute("height") * sampleRate + "px", "");
  684. noteDiv.addEventListener("mouseover", function(event) {
  685. d.getElementById("body" + this.getAttribute("id")).style.setProperty("display", "", "");
  686. }, false);
  687. noteDiv.addEventListener("mouseout", function(event) {
  688. noteclear = setTimeout(function() { d.getElementById("body" + this.getAttribute("id")).style.setProperty("display", "none", ""); }.bind(this), 200);
  689. }, false);
  690. innerDisplay.appendChild(noteDiv);
  691.  
  692. var noteBody = d.createElement("DIV");
  693. noteBody.innerHTML = ndata.getAttribute("body");
  694. noteBody.setAttribute("id", "bodynote" + ndata.getAttribute("id"));
  695. noteBody.setAttribute("class", "trans");
  696. noteBody.setAttribute("style", "color: Black; text-align: left; padding: 4px; z-index: 1" + i + ";");
  697. noteBody.addEventListener("mouseover", function(event) {
  698. clearTimeout(noteclear);
  699. this.style.setProperty("display", "", "");
  700. }, false);
  701. noteBody.addEventListener("mouseout", function(event) {
  702. this.style.setProperty("display", "none", "");
  703. }, false);
  704. innerDisplay.appendChild(noteBody);
  705.  
  706. // this sucks, find another method!
  707. var w = ndata.getAttribute("width") * sampleRate;
  708. var h = ndata.getAttribute("height") * sampleRate;
  709. if(w < h) { w ^= h; h ^= w; w ^= h; } // FUCK YEAH XOR SWAP
  710. while(w / h > ratio) {
  711. w -= ratio;
  712. h += ratio;
  713. }
  714.  
  715. noteBody.style.setProperty("min-width", "-moz-min-content", "");
  716. noteBody.style.setProperty("min-width", "-webkit-min-content", "");
  717. noteBody.style.setProperty("max-width", w + "px", "");
  718.  
  719. ntop = (ndata.getAttribute("y") * sampleRate) + (ndata.getAttribute("height") * sampleRate) + offsety + 5;
  720. nheight = noteBody.getBoundingClientRect().bottom - noteBody.getBoundingClientRect().top;
  721. if(ntop + nheight > vp_bottom)
  722. noteBody.style.setProperty("top", vp_bottom - nheight + "px", "");
  723. else
  724. noteBody.style.setProperty("top", ntop + "px", "");
  725.  
  726. noteBody.style.setProperty("left", ndata.getAttribute("x") * sampleRate + offsetx + "px", "");
  727. noteBody.style.setProperty("display", "none", "");
  728. }
  729. }
  730.  
  731. function getNotes(xmldoc, id) {
  732. if(booru.name == "Danbooru") { // Parses the nodes as attributes for each note
  733. notes = xmldoc.evaluate("notes/note/is-active[text() = 'true']/..", xmldoc, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
  734. for(var i = 0, note = null; note = notes.snapshotItem(i++); i) {
  735. while(note.firstChild) {
  736. if(note.firstChild.nodeName != "#text")
  737. note.setAttribute(note.firstChild.nodeName, note.firstChild.textContent);
  738. note.removeChild(note.firstChild);
  739. }
  740. }
  741. showNotes(xmldoc.evaluate("notes/note[@is-active = 'true']", xmldoc, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null), id);
  742. } else {
  743. showNotes(xmldoc.evaluate("notes/note[@is_active = 'true']", xmldoc, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null), id);
  744. }
  745. }
  746.  
  747. function getValue(name, values) {
  748. for(var i = 0; i < sites.length; i++)
  749. if(new RegExp(sites[i].site).test(window.location.hostname)) {
  750. if(eval(values)[i] != "" && !eval(values)[i]) {
  751. values = setValue(name.replace(/all/, ""), values, setDefaults(name, 1));
  752. }
  753. return eval(values)[i];
  754. }
  755. }
  756.  
  757. function setValue(name, values, newvalue) {
  758. for(var i = 0; i < sites.length; i++)
  759. if(new RegExp(sites[i].site).test(window.location.hostname)) {
  760. oldvalue = eval(values);
  761. oldvalue[i] = (newvalue == "" ? "" : newvalue || oldvalue[i]);
  762. GM_setValue(name, JSON.stringify(oldvalue));
  763. return(JSON.stringify(oldvalue));
  764. }
  765. }
  766.  
  767. function setDefaults(name, all) {
  768. var def, string = "[";
  769. for(var i = 0; i < all; i++) {
  770. if(name == "alltags")
  771. def = "\"\"";
  772. if(name == "allpage")
  773. def = "1";
  774. if(name == "allimages")
  775. def = "20";
  776. if(name == "allrating")
  777. def = "\"s\"";
  778. string += (def) + (i < sites.length - 1 ? ", " : "]");
  779. }
  780. return (all > 1 ? string : eval(def));
  781. }