Pixiv Lazy plus

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

当前为 2014-11-28 提交的版本,查看 最新版本

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