Pixiv Lazy plus

provide a direct link to original image ([s] link).

目前為 2014-11-27 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Pixiv Lazy plus
  3. // @namespace pixivlazyplus
  4. // @description provide a direct link to original image ([s] link).
  5. // @version 0.8.9
  6. // @include http://www.pixiv.net/*
  7. // ==/UserScript==
  8. // version 0.8.9 - append [U] link after canvas
  9. // version 0.8.8 - fix loading big manga URL bug introduced in 0.8.7
  10. // version 0.8.7 - fix loading big manga URL in with PNG format
  11. // version 0.8.6 - fix modifying big manga URL in manga page
  12. // version 0.8.5 - fix preloading new manga URL format
  13. // version 0.8.4 - fix detecting URL new format
  14. // version 0.8.3 - fix detecting URL new format
  15. // version 0.8.2 - fix detecting URL new format
  16. // version 0.8.1 - fix detecting ugoira in prev/next
  17. // version 0.8.0 - add ability for downloading ugoira as zip
  18. // version 0.7.9 - fix for no profile image
  19. // version 0.7.8 - fix for spapi return value changes
  20. // version 0.7.7 - change [s] to [M](go directly to manga page) for manga links
  21. // version 0.7.6 - fix prev/next [s] link
  22. // version 0.7.5 - fix new manga page
  23. // version 0.7.4 - fix modified images
  24. // version 0.7.3 - fix missing session ID when it is not the end of cookie
  25. // version 0.7.2 - fix comma in tags breaking parsing logic
  26. // version 0.7.1 - fix iPhone API by supplying session ID
  27. // version 0.7 - work with new sample images with iPhone API, fix old manga
  28. // version 0.6.1 - preload manga images
  29. // version 0.6 - change manga mode to big images
  30. // version 0.5 - remove [b] link, add stylish style class
  31. // version 0.4 - updated to filter new thumbnails
  32. // version 0.3 - fix a bug, hat-tip to syosyo
  33. // version 0.2 - updated on 2008-06-25
  34. var pixivlink_run = 0;
  35. var isNewManga = 1;
  36. var postProcImg = new Array();
  37. var preloadImg = new Array();
  38. var sessID = (/PHPSESSID=[^;]*?(?=;|$)/.exec(document.cookie) || "");
  39. var mangaFormat = 'jpg';
  40.  
  41. String.prototype.splitCSV = function(sep) {
  42. for (var foo = this.split(sep = sep || ","), x = foo.length - 1, tl; x >= 0; x--) {
  43. if (foo[x].replace(/"\s+$/, '"').charAt(foo[x].length - 1) == '"') {
  44. if ((tl = foo[x].replace(/^\s+"/, '"')).length > 1 && tl.charAt(0) == '"') {
  45. foo[x] = foo[x].replace(/^\s*"|"\s*$/g, '').replace(/""/g, '"');
  46. } else if (x) {
  47. foo.splice(x - 1, 2, [foo[x - 1], foo[x]].join(sep));
  48. } else foo = foo.shift().split(sep).concat(foo);
  49. } else foo[x].replace(/""/g, '"');
  50. } return foo;
  51. };
  52.  
  53. function GetImageIDFromLink(imageLink) {
  54. var imgID = 0; // If lower 11319936 it means Manga does not have Big version
  55.  
  56. var re = /\d+([_a-z0-9]+)?\.(jpe?g|gif|png)\??.*$|id=[0-9]+$/;
  57. var s = re.exec(imageLink);
  58. if (s && s.length > 0) {
  59. re = /\d+/;
  60. imgID = re.exec(s[0])[0];
  61. }
  62. return imgID;
  63. }
  64.  
  65. function getElementsByClassName(matchClass) {
  66. var clselems = new Array();
  67. var elems = document.getElementsByTagName('*'), i;
  68. for (i in elems) {
  69. if((' ' + elems[i].className + ' ').indexOf(' ' + matchClass + ' ')
  70. > -1) {
  71. clselems.push(elems[i]);
  72. }
  73. }
  74. return clselems;
  75. }
  76.  
  77. function pixivlink() {
  78. //alert(pixivlink_run);
  79. if (!pixivlink_run) pixivlink_run = 1;
  80. else return;
  81. var Items = document.getElementsByTagName('img');
  82. var rexa = /\?mode\=medium\&illust_id|\?mode\=big\&illust_id/;
  83. var rexb = /source.pixiv.net/;
  84. var rexc = /\/img-inf\//;
  85. var rexd = /\/mobile\//;
  86. var rexe = /\/c\//;
  87. for (var i = 0; i < Items.length; i++) {
  88. var imgR = Items[i];
  89. var aR = imgR.parentNode.parentNode;
  90. var aR2 = imgR.parentNode;
  91. if (rexa.test(aR2.href)) {
  92. aR = aR2;
  93. }
  94. if (rexa.test(aR.href)) {
  95. var imgID = GetImageIDFromLink(imgR.src);
  96. var srcR = imgR.src.replace(/_s\.|_m\.|_100\.|_64x64\./i, ".");
  97. // var hrefR = aR.href.replace(/medium/i, "big");
  98. var tdR = aR.parentNode;
  99. /* var linkB = document.createElement('a');
  100. linkB.href = hrefR;
  101. linkB.target = '_blank';
  102. linkB.style.padding = '0 2px';
  103. linkB.className = '_pxlazy';
  104. linkB.appendChild(document.createTextNode('[b]'));
  105. tdR.appendChild(linkB);*/
  106. // tdR.appendChild(document.createTextNode(' '));
  107. if (!rexb.test(srcR)) {
  108. var linkS = document.createElement('a');
  109. linkS.href = srcR;
  110. linkS.target = '_blank';
  111. linkS.className = '_pxlazy _pxlazy_s';
  112. linkS.setAttribute('id', 'ill_' + imgID);
  113. linkS.appendChild(document.createTextNode('[s]'));
  114. if (tdR.tagName.toUpperCase() == 'DIV') {
  115. var targetelem = getElementsByClassName('works_display');
  116. if(targetelem.length) {
  117. targetelem[0].appendChild(linkS);
  118. }
  119. } else {
  120. tdR.appendChild(linkS);
  121. }
  122. if (rexc.test(imgR.src)||rexd.test(imgR.src)||rexe.test(imgR.src)) {
  123. //GM_log("postProcImg.push("+imgID+")"+imgR.src);
  124. postProcImg.push(imgID);
  125. }
  126. }
  127. }
  128. }
  129.  
  130. if (postProcImg.length > 0) {
  131. for (var x = 0; x < postProcImg.length; x++) {
  132. GM_xmlhttpRequest({
  133. url: 'http://spapi.pixiv.net/iphone/illust.php?' + sessID + (sessID ? '&' : '') + 'illust_id=' + postProcImg[x],
  134. method: "GET",
  135. headers: {
  136. Referer: "http://www.pixiv.net"
  137. },
  138. onload: function (response) {
  139. if (response.status == 200) {
  140. var rexb = /source.pixiv.net/;
  141. // var rexU = /\/c\//;
  142. var rexU = /_480mw\./;
  143. var rexe = /\/c\//;
  144. var vals = response.responseText.splitCSV();
  145. /*var vtxt = '';
  146. for(var x=0;x < vals.length;x++)
  147. vtxt=vtxt+x+':'+vals[x]+"\n";
  148. GM_log(vtxt);*/
  149. if (vals.length > 0) {
  150. var slnk, imgID, isRestricted;
  151. isRestricted = rexb.test(vals[6]);
  152. isUgoira = !rexU.test(vals[9]);
  153. if (!isRestricted) {
  154. //GM_log("imgID = vals[0]");
  155. imgID = vals[0];
  156. } else {
  157. //GM_log("GetImageIDFromLink("+response.finalUrl+")");
  158. imgID = GetImageIDFromLink(response.finalUrl);
  159. }
  160. slnk = document.getElementById('ill_' + imgID);
  161. if (slnk) {
  162. var goodSlink;
  163. if (vals[19].length > 0) {
  164. goodSlink = 'http://www.pixiv.net/member_illust.php?mode=manga&illust_id=' + imgID;
  165. slnk.innerHTML = '[M]';
  166. } else {
  167. var re = new RegExp('/' + vals[0] + '_.*$');
  168. if (isUgoira) {// grab zip!
  169. //GM_log("isUgoira");
  170. slnk.innerHTML = '[U]';
  171. goodSlink = vals[9].replace(/c\/\d+x\d+\/img-master/, 'img-zip-ugoira').replace(re, '/' + vals[0] + '_ugoira1920x1080.zip');
  172. } else if (rexe.test(vals[9])) {// new 480mw URL
  173. //GM_log("480mw");
  174. goodSlink = vals[9].replace(/c\/480x960\//, '').replace(/img-master/, 'img-original').replace(re, '/' + vals[0] + '_p0.' + vals[2]);
  175. } else if (!isRestricted && !rexe.test(vals[9])) {// use 480mw instead
  176. //GM_log("480mw");
  177. goodSlink = vals[9].replace(/mobile\//, '').replace(re, '/' + vals[0] + '.' + vals[2]);
  178. } else { //salvage from profile image
  179. //GM_log("salvage");
  180. re = /\/[0-9_]+\..*$/;
  181. goodSlink = vals[29].replace(/mobile\//, '').replace(/profile\//, 'img/').replace(re, '/' + imgID + '.' + vals[2]);
  182. }
  183. }
  184. slnk.href = goodSlink;
  185. slnk.title=vals[12];
  186. slnk.className = '_pxlazy _pxlazy_s _pxlazy_s_new';
  187. }
  188. }
  189. }
  190. }
  191. });
  192. }
  193. }
  194.  
  195. var links = document.getElementsByTagName('a');
  196. var tagslink = /tags\.php\?tag=/;
  197. for (var i = 0; i < links.length; i++) {
  198. if (tagslink.test(links[i].href))
  199. links[i].href = links[i].href.replace("tags.php?tag=", "search.php?s_mode=s_tag_full&word=");
  200. }
  201.  
  202. if (unsafeWindow.pixiv && unsafeWindow.pixiv.context.images) {
  203. var illustID = GetImageIDFromLink(unsafeWindow.pixiv.context.images[0]/*[0]*/);
  204. isNewManga = (illustID >= 11319936);
  205. //GM_log("illustID="+illustID);
  206. GM_xmlhttpRequest({
  207. url: 'http://spapi.pixiv.net/iphone/illust.php?' + sessID + (sessID ? '&' : '') + 'illust_id=' + illustID,
  208. method: "GET",
  209. headers: {
  210. Referer: "http://www.pixiv.net"
  211. },
  212. onload: function (response) {
  213. if (response.status == 200) {
  214. var vals = response.responseText.splitCSV();
  215. var rexe = /\/c\//;
  216. mangaFormat = vals[2];
  217. /*var vtxt = '';
  218. for(var x=0;x < vals.length;x++)
  219. vtxt=vtxt+x+':'+vals[x]+"\n";
  220. GM_log(vtxt);*/
  221. //GM_log("2nd");
  222. if (rexe.test(vals[9])) mangaFull();
  223. }
  224. }
  225. });
  226. var rexe = /\/c\//;
  227. //GM_log("isNewManga");
  228. //setTimeout(mangaFull,250);
  229. //GM_log("2nd");
  230. mangaFull();
  231. }
  232. }
  233.  
  234. function mangaFull() {
  235. Items = document.getElementsByTagName('img');
  236. var rexe = /\/img-/;
  237. for (var x = 0; x < unsafeWindow.pixiv.context.images.length; x++) {
  238. if (isNewManga) {
  239. if(rexe.test(unsafeWindow.pixiv.context.images[x]))
  240. unsafeWindow.pixiv.context.images[x]/*[0]*/ = unsafeWindow.pixiv.context.images[x]/*[0]*/.replace(/c\/1200x1200\//, '').replace(/img-master/, 'img-original').replace(/_p(\d+).*(\.[a-zA-Z\?\d]+)$/, "_p$1."+mangaFormat);
  241. else
  242. unsafeWindow.pixiv.context.images[x]/*[0]*/ = unsafeWindow.pixiv.context.images[x]/*[0]*/.replace(/_p(\d+\.[a-zA-Z\?\d]+)$/, "_big_p$1");
  243. }
  244. preloadImg.push(new Image());
  245. preloadImg[preloadImg.length - 1].src = unsafeWindow.pixiv.context.images[x]/*[0]*/;
  246. }
  247.  
  248. for (var x = 0; x < Items.length; x++) {
  249. var datasrc = Items[x].getAttribute("data-src");
  250. if (datasrc) {
  251. //GM_log("original-datasrc="+datasrc+" ,mangaFormat="+mangaFormat);
  252. if(rexe.test(datasrc))
  253. datasrc = datasrc.replace(/c\/1200x1200\//, '').replace(/img-master/, 'img-original').replace(/_p(\d+).*(\.[a-zA-Z\?\d]+)$/, "_p$1."+mangaFormat);
  254. else
  255. datasrc = datasrc.replace(/_p(\d+\.[a-zA-Z\?\d]+)$/, "_big_p$1");
  256. //GM_log("new-datasrc="+datasrc+" ,mangaFormat="+mangaFormat);
  257. }
  258. Items[x].setAttribute("data-src", datasrc);
  259. if (isNewManga) {
  260. //GM_log(x+".src="+Items[x].src);
  261. if(datasrc)
  262. Items[x].src = datasrc;
  263. else if(rexe.test(Items[x].src))
  264. Items[x].src = Items[x].src.replace(/c\/1200x1200\//, '').replace(/img-master/, 'img-original').replace(/_p(\d+).*(\.[a-zA-Z\?\d]+)$/, "_p$1$2");
  265. else
  266. Items[x].src = datasrc ? datasrc : Items[x].src.replace(/_p(\d+\.[a-zA-Z\?\d]+)$/, "_big_p$1");
  267. }
  268. }
  269. }
  270.  
  271. window.addEventListener("load", pixivlink, true);