Pixiv Infinite Scroll/Download Links

Adds infinite scroll and inline expansion on the search page and artists' works pages. For manga mode a two-step expansion is used.

当前为 2014-07-08 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Pixiv Infinite Scroll/Download Links
  3. // @description Adds infinite scroll and inline expansion on the search page and artists' works pages. For manga mode a two-step expansion is used.
  4. // @namespace https://github.com/an-electric-sheep/userscripts
  5. // @match *://www.pixiv.net/search*
  6. // @match *://www.pixiv.net/member_illust*
  7. // @version 0.3
  8. // @grant none
  9. // @run-at document-start
  10. // ==/UserScript==
  11.  
  12. "use strict";
  13.  
  14. var Maybe = function (wrapped) {
  15. if (typeof this !== "object" || Object.getPrototypeOf(this) !== Maybe.prototype) {
  16. var o = Object.create(Maybe.prototype);
  17. o.constructor.apply(o, arguments);
  18. return o;
  19. }
  20. this.wrapped = wrapped;
  21. }
  22.  
  23. Maybe.prototype.isEmpty = function(){return null == this.wrapped}
  24. Maybe.prototype.orElse = function(other){return this.isEmpty() ? Maybe(other) : this}
  25. Maybe.prototype.apply = function(f){if(!this.isEmpty()){f.apply(null, [this.wrapped].concat(Array.slice(arguments, 1)))};return this;}
  26. Maybe.prototype.map = function(f){return this.isEmpty() ? this : Maybe(f.apply(null, [this.wrapped].concat(Array.slice(arguments,1))));}
  27. Maybe.prototype.get = function(){return this.wrapped;}
  28.  
  29. var paginator;
  30. var loading = false;
  31.  
  32. var imgContainerSelector = ".image-items, .display_works > ul";
  33.  
  34. document.addEventListener("DOMContentLoaded", function() {
  35. for(var e of document.querySelectorAll("iframe, .ad-printservice, .popular-introduction")){e.remove()}
  36. var sheet = document.querySelector("head").appendChild(document.createElement("style")).sheet;
  37. [
  38. // global
  39. "#wrapper {width: unset;}",
  40. // search page
  41. ".layout-body {width: 85vw;}",
  42. // member page
  43. ".layout-a {width: unset;}",
  44. ".layout-a .layout-column-2 {width: calc(100vw - 190px);}",
  45. // member works list
  46. ".display_works {width: unset;}",
  47. ".display_works .image-item {float: none; }",
  48. // search and member works list
  49. ".image-items, .display_works > ul {display: flex;flex-wrap: wrap;}",
  50. ".image-item img {padding: 0px; border: none;}",
  51. ".inline-expandable {cursor: pointer;}",
  52. ".image-item.expanded {width: 100%; height: unset;}",
  53. ".image-item.expanded img {max-width: -moz-available;}",
  54. ".manga-item {background-color: #f3f3f3 !important;}",
  55. ".image-item img.manga-medium {max-width: 156px; max-height: 230px; cursor: pointer;}",
  56. // animated content inlined in the search page
  57. ".exploded-animation {display: flex; width: -moz-fit-content; overflow-y: scroll; border: 2px #f1f1f1 inset;}",
  58. ".exploded-animation img {margin-left: 5px;}",
  59. ".control-elements {display: flex; justify-content: space-around;align-items: center;}",
  60. ".control-elements > * {position: relative;}",
  61. ].forEach(r => sheet.insertRule(r,0))
  62. paginator = Maybe(document.querySelectorAll(".pager-container")).map(paginators => paginators[paginators.length-1]).get();
  63. window.addEventListener("scroll", isNextNeeded)
  64. window.addEventListener("resize", isNextNeeded)
  65. for(var e of document.querySelectorAll(".image-item")){customizeImageItem(e)}
  66. isNextNeeded();
  67. })
  68.  
  69.  
  70.  
  71. function inViewport (el) {
  72.  
  73. var rect = el.getBoundingClientRect();
  74.  
  75. return (
  76. rect.top >= 0 &&
  77. rect.left >= 0 &&
  78. rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
  79. rect.right <= (window.innerWidth || document.documentElement.clientWidth)
  80. );
  81. }
  82.  
  83. function mangaItemExpand() {
  84. this.removeEventListener("click", mangaItemExpand)
  85. var container = this.parentElement;
  86. var newImg = document.createElement("img");
  87. // just try to load the big image, this may fail for some older images, just expand in that case
  88. newImg.src = this.src.replace(/(_p\d+)/, "_big$1")
  89. newImg.addEventListener("load", () => {container.replaceChild(newImg, this);container.classList.add("expanded")})
  90. newImg.addEventListener("error", () => container.classList.add("expanded"))
  91. newImg.className = "manga"
  92. }
  93.  
  94.  
  95. function insertMangaItems(parentItem,url) {
  96. var req = new XMLHttpRequest
  97. req.open("get", url)
  98. req.onload = function() {
  99. var rsp = this.responseXML
  100. var nextItem = parentItem.nextSibling
  101. for(var e of rsp.querySelectorAll(".item-container")) {
  102. let mediumImg = e.querySelector(".image")
  103. let bigUrl = e.querySelector(".full-size-container").href
  104. let item = document.createElement("li")
  105. item.className = "image-item manga-item"
  106. let img = document.createElement("img")
  107. img.src = mediumImg.dataset.src
  108. img.className = "manga-medium"
  109. img.addEventListener("click", mangaItemExpand)
  110. item.appendChild(img)
  111. parentItem.parentNode.insertBefore(item, nextItem)
  112. }
  113. }
  114. req.responseType = "document"
  115. req.send()
  116. }
  117.  
  118.  
  119. function insertAnimationItems(container, mediumDoc) {
  120. var script = mediumDoc.querySelector("#wrapper script")
  121. // it's not a strong sandbox. it just avoids the loaded script writing to the main window
  122. var sandbox = document.createElement("iframe")
  123. sandbox.src = window.location.href
  124. sandbox.seamless = true
  125. sandbox.setAttribute("srcdoc", "<!DOCTYPE html><html><head><script async src='https://cdn.jsdelivr.net/jszip/2.2.2/jszip.min.js'></script><script>window.pixiv = {context: {}}</script><script>"+ script.firstChild.data +"</script></head></html>")
  126. sandbox.onload = () => {
  127. var sandboxWindow = sandbox.contentWindow
  128. var illustData = sandboxWindow.pixiv.context.ugokuIllustFullscreenData
  129. var req = new sandboxWindow.XMLHttpRequest
  130. req.open("get", illustData.src)
  131. req.responseType = "arraybuffer"
  132. req.onload = function () {
  133. var controlElements = document.createElement("div")
  134. controlElements.className = "control-elements"
  135. var oldElements = document.createElement("div")
  136. while(container.hasChildNodes()){oldElements.appendChild(container.firstChild)}
  137. controlElements.appendChild(oldElements)
  138. container.appendChild(controlElements)
  139. var downloadInfo = document.createElement("div")
  140. var buffer = this.response
  141. var zip = new sandboxWindow.JSZip(buffer)
  142. var downloadLink = document.createElement("a")
  143. downloadLink.className = "animation-download"
  144. downloadLink.innerHTML = downloadLink.download = sandboxWindow.pixiv.context.illustId + ".zip"
  145. downloadInfo.appendChild(document.createTextNode("Download: "))
  146. downloadInfo.appendChild(downloadLink)
  147. downloadInfo.appendChild(document.createElement("br"))
  148. downloadInfo.appendChild(document.createTextNode("pixiv2webm and pixiv2gif available on "))
  149. downloadInfo.appendChild(Maybe(document.createElement("a")).apply(e => {e.href = "https://github.com/an-electric-sheep/userscripts"; e.innerHTML = "on github"}).get())
  150. controlElements.appendChild(downloadInfo)
  151.  
  152. var explodedAnimation = document.createElement("div")
  153. explodedAnimation.className = "exploded-animation"
  154. container.appendChild(explodedAnimation)
  155. var timingInformation = []
  156.  
  157. for(var name in zip.files){
  158. let file = zip.file(name)
  159. let img = document.createElement("img")
  160. let imgBuf = file.asArrayBuffer()
  161. let imgBlob = new Blob([imgBuf])
  162. img.src = URL.createObjectURL(imgBlob)
  163. timingInformation.push(file.name +"\t"+ illustData.frames.find((e) => e.file == name).delay)
  164. explodedAnimation.appendChild(img)
  165. }
  166. container.classList.add("expanded")
  167. zip.file("frame_delays.txt", timingInformation.join("\n"))
  168. downloadLink.href = URL.createObjectURL(zip.generate({type: "blob"}))
  169. sandbox.remove();
  170. }
  171. req.send()
  172. }
  173. document.body.appendChild(sandbox)
  174.  
  175. }
  176.  
  177. function listItemExpand() {
  178. var container = this.parentNode
  179. var mediumLink = container.querySelector("a.work").href
  180. var req = new XMLHttpRequest
  181. req.open("get", mediumLink)
  182. req.onload = function() {
  183. var rsp = this.responseXML;
  184. if(rsp.querySelector("._ugoku-illust-player-container")) {
  185. insertAnimationItems(container, rsp)
  186. }
  187. Maybe(rsp.querySelector(".works_display a[href]")).apply((modeLink) => {
  188. var modeLinkUrl = modeLink.href
  189. var mediumSrc = modeLink.querySelector("img").src
  190. var mode = modeLinkUrl.match(/mode=(.+?)&/)[1]
  191. if(mode == "big") {
  192. var img = container.querySelector("img")
  193. img.src = mediumSrc.replace("_m.", ".");
  194. container.classList.add("expanded")
  195. }
  196. if(mode == "manga"){
  197. insertMangaItems(container, modeLinkUrl)
  198. }
  199. })
  200. }
  201. req.responseType = "document"
  202. req.send()
  203. this.removeEventListener("click", listItemExpand)
  204. }
  205.  
  206. const greasedImageItems = new WeakMap;
  207.  
  208. function customizeImageItem(e) {
  209. if(greasedImageItems.has(e))
  210. return;
  211. greasedImageItems.set(e, true);
  212. var workLink = e.querySelector("a.work")
  213.  
  214. var img = workLink.querySelector("img")
  215. img.classList.add("inline-expandable")
  216. img.dataset.thumbSrc = img.src
  217. e.insertBefore(img, workLink)
  218. img.addEventListener("click", listItemExpand)
  219. }
  220.  
  221. function loadNext() {
  222. if(loading)
  223. return;
  224. loading = true;
  225. var nextLink = paginator.querySelector("a[rel=next]")
  226. if(nextLink) {
  227. var req = new XMLHttpRequest();
  228. req.open("get", nextLink.href)
  229. req.onload = function() {
  230. var rsp = this.responseXML;
  231. var container = document.querySelector(imgContainerSelector)
  232. for(var e of rsp.querySelectorAll(".image-item")){
  233. var imageItem = document.importNode(e, true)
  234. container.appendChild(imageItem)
  235. customizeImageItem(imageItem)
  236. }
  237. while(paginator.hasChildNodes())
  238. paginator.firstChild.remove()
  239. for(var e of rsp.querySelector(".pager-container").childNodes){paginator.appendChild(document.importNode(e, true) )}
  240. loading = false;
  241. isNextNeeded();
  242. }
  243. req.responseType = "document"
  244. req.send()
  245. }
  246. }
  247.  
  248. function isNextNeeded() {
  249. if(loading)
  250. return;
  251.  
  252. if(paginator && inViewport(document.querySelector(".image-item:last-child"))) {
  253. loadNext();
  254. }
  255. }