Danbooru Ajax Interface

New interface to search images on Booru-style sites.

Från och med 2015-03-02. Se den senaste versionen.

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