Pixiv Lazy plus

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

当前为 2022-08-16 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Pixiv Lazy plus
  3. // @namespace pixivlazyplus
  4. // @description provide a direct link to original image ([s] link).
  5. // @license Public Domain
  6. // @version 0.9.0.22
  7. // @include http://www.pixiv.net/*
  8. // @include https://www.pixiv.net/*
  9. // @include https://accounts.pixiv.net/*
  10. // @grant GM_getValue
  11. // @grant GM_setValue
  12. // @grant GM_xmlhttpRequest
  13. // @grant GM_log
  14. // @grant GM_registerMenuCommand
  15. // ==/UserScript==
  16. // version 0.9.0.22 - go back to probing due to low rate limit of ajax api (it doesn't probe ugoira properly at the moment)
  17. // version 0.9.0.21 - switch to ajax api (https://github.com/danbooru/danbooru/blob/39cc3ed5cf913499093d2f641d70d7682a14fa42/app/logical/pixiv_ajax_client.rb)
  18. // version 0.9.0.20 - tried to take care refresh_token, and overhual non-app-api version of parsing code (as urgency relief)
  19. // version 0.9.0.19 - followup https://github.com/upbit/pixivpy/issues/83#issuecomment-527232396
  20. // version 0.9.0.18 - fix medium page neighbor illust direct links
  21. // version 0.9.0.17 - fix medium page list after server side version up
  22. // version 0.9.0.16 - fix medium page after server side version up
  23. // version 0.9.0.15 - fix fetching preview urls, add a button for manual generating source links
  24. // version 0.9.0.14 - use data-src and always post-processing
  25. // version 0.9.0.13 - fix OAuth login detection URL, turn pixiv-dic URL into tag-searching URL
  26. // version 0.9.0.12 - try to auto login with tags.php ifreame reloading
  27. // version 0.9.0.11 - fix ranking [s] link attachment
  28. // version 0.9.0.10 - resize images in manga view
  29. // version 0.9.0.9 - fix for elimination of public API me.json
  30. // version 0.9.0.8 - add Auto Login menu item
  31. // version 0.9.0.7 - more fix of cookie issue
  32. // version 0.9.0.6 - fix cookie issue
  33. // version 0.9.0.5 - fix ugoira link again
  34. // version 0.9.0.4 - restore MangaFull function
  35. // version 0.9.0.3 - fix clearing accessToken logic, add function to check if accessToken is still valid
  36. // version 0.9.0.2 - fix ugoira link
  37. // version 0.9.0.1 - disable MangaFull since pixiv checks referer for full size manga pages now.
  38. // version 0.9 - upgrade to OAuth API + Pixiv Public API
  39. // version 0.8.14.1 - fix new medium page layout
  40. // version 0.8.13 - fix image response lazy link position
  41. // version 0.8.12 - fix mypage right side lazy links
  42. // version 0.8.11 - fix double links in medium page
  43. // version 0.8.10 - fix double links in medium page
  44. // version 0.8.9 - append [U] link after canvas
  45. // version 0.8.8 - fix loading big manga URL bug introduced in 0.8.7
  46. // version 0.8.7 - fix loading big manga URL in with PNG format
  47. // version 0.8.6 - fix modifying big manga URL in manga page
  48. // version 0.8.5 - fix preloading new manga URL format
  49. // version 0.8.4 - fix detecting URL new format
  50. // version 0.8.3 - fix detecting URL new format
  51. // version 0.8.2 - fix detecting URL new format
  52. // version 0.8.1 - fix detecting ugoira in prev/next
  53. // version 0.8.0 - add ability for downloading ugoira as zip
  54. // version 0.7.9 - fix for no profile image
  55. // version 0.7.8 - fix for spapi return value changes
  56. // version 0.7.7 - change [s] to [M](go directly to manga page) for manga links
  57. // version 0.7.6 - fix prev/next [s] link
  58. // version 0.7.5 - fix new manga page
  59. // version 0.7.4 - fix modified images
  60. // version 0.7.3 - fix missing session ID when it is not the end of cookie
  61. // version 0.7.2 - fix comma in tags breaking parsing logic
  62. // version 0.7.1 - fix iPhone API by supplying session ID
  63. // version 0.7 - work with new sample images with iPhone API, fix old manga
  64. // version 0.6.1 - preload manga images
  65. // version 0.6 - change manga mode to big images
  66. // version 0.5 - remove [b] link, add stylish style class
  67. // version 0.4 - updated to filter new thumbnails
  68. // version 0.3 - fix a bug, hat-tip to syosyo
  69. // version 0.2 - updated on 2008-06-25
  70. var pixivlink_run = 0;
  71. var pixivlink_reloadRequested = 0;
  72. var isNewManga = 1;
  73. var postProcImg = new Array();
  74. var preloadImg = new Array();
  75. var Imgs = new Array();
  76. //var sessID1 = (/PHPSESSID=[^;]*?(?=;|$)/.exec(document.cookie) || "");
  77. var sessID1 = "";
  78. var sessID2 = (/PHPSESSID=[0-9a-f]*?(?=;|$)/.exec(document.cookie) || ""); // should be unused
  79. var mangaFormat = 'jpg';
  80. var pixivlink_imgs = 0;
  81.  
  82. /*
  83. String.prototype.splitCSV = function(sep) {
  84. for (var foo = this.split(sep = sep || ","), x = foo.length - 1, tl; x >= 0; x--) {
  85. if (foo[x].replace(/"\s+$/, '"').charAt(foo[x].length - 1) == '"') {
  86. if ((tl = foo[x].replace(/^\s+"/, '"')).length > 1 && tl.charAt(0) == '"') {
  87. foo[x] = foo[x].replace(/^\s*"|"\s*$/g, '').replace(/""/g, '"');
  88. } else if (x) {
  89. foo.splice(x - 1, 2, [foo[x - 1], foo[x]].join(sep));
  90. } else foo = foo.shift().split(sep).concat(foo);
  91. } else foo[x].replace(/""/g, '"');
  92. } return foo;
  93. };*/
  94.  
  95. function _e(id) { return document.getElementById(id); }
  96.  
  97. function getElementsByClassName(matchClass) {
  98. var clselems = new Array();
  99. var elems = document.getElementsByTagName('*'), i;
  100. for (i in elems) {
  101. if((' ' + elems[i].className + ' ').indexOf(' ' + matchClass + ' ')
  102. > -1) {
  103. clselems.push(elems[i]);
  104. }
  105. }
  106. return clselems;
  107. }
  108.  
  109. function createCookie(cookietext,days, domain) {
  110. if (days) {
  111. var date = new Date();
  112. date.setTime(date.getTime()+(days*24*60*60*1000));
  113. var expires = "; expires="+date.toGMTString();
  114. }
  115. else var expires = "";
  116. if (domain) domain = "; domain="+domain;
  117. else domain = "";
  118. document.cookie = cookietext+expires+domain+"; path=/";
  119. }
  120.  
  121. function GetImageIDFromLink(imageLink) {
  122. var imgID = 0; // If lower 11319936 it means Manga does not have Big version
  123.  
  124. var re = /\d+([_a-z0-9]+)?\.(jpe?g|gif|png)\??.*$|id=[0-9]+$/;
  125. var s = re.exec(imageLink);
  126. if (s && s.length > 0) {
  127. re = /\d+/;
  128. imgID = re.exec(s[0])[0];
  129. }
  130. return imgID;
  131. }
  132.  
  133. function pixivlink() {
  134. //alert(pixivlink_run);
  135. if (!pixivlink_run) pixivlink_run = 1;
  136. else return;
  137. var Items = document.getElementsByTagName('img');
  138. var rexa = /\?mode\=(medium|manga)\&illust_id|i\.pximg\.net|\/artworks\//;
  139. var rexb = /source.pixiv.net/;
  140. var rexc = /\/img-inf\//;
  141. var rexd = /\/mobile\//;
  142. var rexe = /\/c\//;
  143. for (var i = 0; i < Items.length; i++) {
  144. var imgR = Items[i];
  145. var aR = imgR.parentNode.parentNode;
  146. var aR2 = imgR.parentNode;
  147. var isThumbnail = ((' ' + aR2.className + ' ').indexOf(' _layout-thumbnail ') > -1) //&& ((' ' + aR2.className + ' ').indexOf(' ui-modal-trigger ') > -1)
  148. //GM_log('aR2.className = "'+aR2.className+'", imgR.src='+imgR.src);
  149. //GM_log('isThumbnail = '+isThumbnail);
  150. if (rexa.test(aR2.href)/* || isThumbnail*/) {
  151. aR = aR2;
  152. }
  153. if (rexa.test(aR.href) || isThumbnail) {
  154. if(imgR.hasAttribute("data-src")) {
  155. var imgID = GetImageIDFromLink(imgR.getAttribute("data-src"));
  156. var srcR = imgR.getAttribute("data-src").replace(/(_s|_m|_100|_64x64|_master1200|_custom1200|_square1200)\./i, ".");
  157. } else {
  158. var imgID = GetImageIDFromLink(imgR.src);
  159. var srcR = imgR.src.replace(/(_s|_m|_100|_64x64|_master1200|_custom1200|_square1200)\./i, ".");
  160. }
  161. srcR = srcR.replace(/\/c\/\d+x\d+(_\d+)?(_[a-z]\d+)?(_[a-z]\d+)?\/(img-master|custom-thumb)\//i, "/img-original/");
  162. srcR = srcR.replace(/\/img-master\//i, "/img-original/");
  163. srcR = srcR.replace(/(\d{2,})\.jpg$/, "$1_p0.jpg"); // fix ID-only URLs
  164. // var hrefR = aR.href.replace(/medium/i, "big");
  165. var tdR = aR.parentNode;
  166. //GM_log('srcR = "'+srcR);
  167. /* var linkB = document.createElement('a');
  168. linkB.href = hrefR;
  169. linkB.target = '_blank';
  170. linkB.style.padding = '0 2px';
  171. linkB.className = '_pxlazy';
  172. linkB.appendChild(document.createTextNode('[b]'));
  173. tdR.appendChild(linkB);*/
  174. // tdR.appendChild(document.createTextNode(' '));
  175. if (!rexb.test(srcR) && Imgs.indexOf(imgID) == -1 && !_e('ill_' + imgID)) {
  176. var linkS = document.createElement('a');
  177. linkS.href = srcR;
  178. linkS.target = '_blank';
  179. linkS.className = '_pxlazy _pxlazy_s';
  180. linkS.setAttribute('id', 'ill_' + imgID);
  181. linkS.appendChild(document.createTextNode('[s]'));
  182. if (aR2.tagName.toUpperCase() == 'DIV' && imgR.className.toLowerCase().indexOf('thumbnail') == -1) {
  183. var targetelem = getElementsByClassName('works_display');
  184. if(targetelem.length) {
  185. targetelem[0].appendChild(linkS);
  186. } else {
  187. tdR.appendChild(linkS);
  188. }
  189. } else {
  190. tdR.appendChild(linkS);
  191. }
  192. if (1/*rexc.test(imgR.src)||rexd.test(imgR.src)||rexe.test(imgR.src)*/) {
  193. //GM_log("postProcImg.push("+imgID+")"+imgR.src);
  194. postProcImg.push({id:imgID, url:srcR});
  195. }
  196. Imgs.push(imgID);
  197. }
  198. }
  199. }
  200.  
  201. var ItemsDiv = document.getElementsByTagName('div');
  202. //GM_log("ItemsDiv.length="+ItemsDiv.length);
  203. for (var i = 0; i < ItemsDiv.length; i++) {
  204. var divR = ItemsDiv[i];
  205. var aR = divR.parentNode.parentNode;
  206. var rexf = /\/img-master\//;
  207. var imgSrc;
  208. try{
  209. imgSrc=divR.style.backgroundImage.substr(5);
  210. imgSrc=imgSrc.substr(0,imgSrc.length-2);
  211. }catch(e) {}
  212. if(!imgSrc && divR.getAttribute("role")=='presentation') {
  213. try{
  214. imgSrc=window.getComputedStyle(divR,null).getPropertyValue('background-image').substr(5);
  215. imgSrc=imgSrc.substr(0,imgSrc.length-2);
  216. }catch(e) {}
  217. }
  218. var isThumbnail = ((' ' + divR.className + ' ').indexOf(' lazyloaded ') > -1)
  219. if(isThumbnail || rexf.test(imgSrc)) {
  220. /*var imgID = GetImageIDFromLink(divR.style.backgroundImage);
  221. var imgSrc=divR.style.backgroundImage.substr(5);
  222. imgSrc=imgSrc.substr(0,imgSrc.length-2);*/
  223. var imgID = GetImageIDFromLink(imgSrc);
  224. //GM_log("imgSrc="+imgSrc);
  225. var srcR = imgSrc.replace(/_s\.|_m\.|_100\.|_64x64|_master1200|_custom1200|_square1200\./i, ".");
  226. if (rexf.test(imgSrc) && !_e('ill_' + imgID)) {
  227. // GM_log("style.backgroundImage="+imgSrc);
  228.  
  229. var linkS = document.createElement('a');
  230. linkS.href = srcR;
  231. linkS.target = '_blank';
  232. linkS.className = '_pxlazy _pxlazy_s';
  233. linkS.setAttribute('id', 'ill_' + imgID);
  234. linkS.appendChild(document.createTextNode('[s]'));
  235.  
  236. if(aR.tagName.toUpperCase() == 'A') aR = aR.parentNode;
  237. aR.appendChild(linkS);
  238. postProcImg.push({id:imgID, url:imgSrc});
  239. }
  240. }
  241. }
  242.  
  243. if (postProcImg.length > 0) {
  244. for (var x = 0; x < postProcImg.length; x++) {
  245. if(0) { // ajax api
  246. GM_xmlhttpRequest({
  247. url: 'https://www.pixiv.net/ajax/illust/' + postProcImg[x].id,
  248. method: "GET",
  249. headers: {
  250. Referer: "http://www.pixiv.net",
  251. "Cookie": ''+sessID1
  252. },
  253. onload: function (response) {
  254. if (response.status == 200) {
  255. var rexb = /source.pixiv.net/;
  256. // var rexU = /\/c\//;
  257. var rexU = /_ugoira/;
  258. var rexe = /\/c\//;
  259. var vals = JSON.parse(response.responseText);
  260. /*var vtxt = '';
  261. for(var x=0;x < vals.length;x++)
  262. vtxt=vtxt+x+':'+vals[x]+"\n";
  263. GM_log(vtxt);*/
  264. if (!vals.error) {
  265. var slnk, imgID, isRestricted;
  266. isRestricted = vals.body.restrict;
  267. isUgoira = rexU.test(vals.body.urls.original);
  268. // if (!isRestricted) {
  269. //GM_log("imgID = vals[0]");
  270. imgID = vals.body.illustId;
  271. /* } else {
  272. //GM_log("GetImageIDFromLink("+response.finalUrl+")");
  273. imgID = GetImageIDFromLink(response.finalUrl);
  274. }*/
  275. slnk = _e('ill_' + imgID);
  276. if (slnk) {
  277. var goodSlink;
  278. if (vals.body.pageCount > 1) {
  279. goodSlink = 'http://www.pixiv.net/member_illust.php?mode=manga&illust_id=' + imgID;
  280. slnk.innerHTML = '[M]';
  281. } else {
  282. var re = new RegExp('/' + imgID + '_.*$');
  283. if (isUgoira) {// grab zip!
  284. //GM_log("isUgoira");
  285. slnk.innerHTML = '[U]';
  286. goodSlink = vals.body.urls.original.replace(/img-original/, 'img-zip-ugoira').replace(re, '/' + imgID + '_ugoira1920x1080.zip');
  287. /* } else if (rexe.test(vals[9])) {// new 480mw URL
  288. //GM_log("480mw");
  289. goodSlink = vals[9].replace(/c\/480x960\//, '').replace(/img-master/, 'img-original').replace(re, '/' + vals[0] + '_p0.' + vals[2]);
  290. } else if (!isRestricted && !rexe.test(vals[9])) {// use 480mw instead
  291. //GM_log("480mw");
  292. goodSlink = vals[9].replace(/mobile\//, '').replace(re, '/' + vals[0] + '.' + vals[2]);
  293. } else { //salvage from profile image
  294. //GM_log("salvage");
  295. re = /\/[0-9_]+\..*$/;
  296. goodSlink = vals[29].replace(/mobile\//, '').replace(/profile\//, 'img/').replace(re, '/' + imgID + '.' + vals[2]);
  297. */
  298. } else {
  299. goodSlink = vals.body.urls.original;
  300. }
  301. }
  302. slnk.href = goodSlink;
  303.  
  304. // convert UTC time to ISO date string in GMT+9
  305. var datestamp=(new Date((new Date(vals.body.uploadDate)).getTime()+(3600000*9))).toISOString();
  306. slnk.title=datestamp.substring(0,10)+" "+datestamp.substring(11,19); // "date time"
  307. slnk.className = '_pxlazy _pxlazy_s _pxlazy_s_new';
  308. }
  309. }
  310. } else if (response.status == 400) {
  311. // accessToken ="";
  312. // GM_setValue("accessToken",accessToken);
  313. }
  314. }
  315. });
  316. } else {
  317. //GM_log("check="+JSON.stringify(postProcImg[x], null, 4));
  318. //GM_log("check url="+(postProcImg[x]).url);
  319. GM_xmlhttpRequest({
  320. url: postProcImg[x].url,
  321. method: "HEAD",
  322. headers: {
  323. Referer: "http://www.pixiv.net",
  324. "Cookie": ''+sessID1
  325. },
  326. onload: function (response) {
  327. var slnk, imgID, isRestricted;
  328. imgID = GetImageIDFromLink(response.finalUrl);
  329. slnk = _e('ill_' + imgID);
  330. //GM_log("response="+JSON.stringify(response, null, 4));
  331. if (response.status == 200) {
  332. //GM_log("url="+response.finalUrl+" exists");
  333. } else /* assume error */ {
  334. GM_log("url="+response.finalUrl+" NOT exist!");
  335. slnk.href = slnk.href.replace(/\.jpg$/,".png"); // dumb replacing jpg to png
  336. }
  337. }
  338. });
  339. }
  340. }
  341. }
  342.  
  343. /* var links = document.getElementsByTagName('a');
  344. var tagslink = /tags\.php\?tag=/;
  345. for (var i = 0; i < links.length; i++) {
  346. if (tagslink.test(links[i].href))
  347. links[i].href = links[i].href.replace("tags.php?tag=", "search.php?s_mode=s_tag&word=");
  348. }*/
  349.  
  350. if (unsafeWindow.pixiv && unsafeWindow.pixiv.context.images) {
  351. var illustID = GetImageIDFromLink(unsafeWindow.pixiv.context.images[0]/*[0]*/);
  352. isNewManga = (illustID >= 11319936);
  353. //GM_log("illustID="+illustID);
  354. GM_xmlhttpRequest({
  355. url: 'https://www.pixiv.net/ajax/illust/' + illustID + '/pages',
  356. method: "GET",
  357. headers: {
  358. Referer: "http://www.pixiv.net",
  359. "Cookie": ''+sessID1
  360. },
  361. onload: function (response) {
  362. if (response.status == 200) {
  363. //GM_log(response.responseText);
  364. var vals = JSON.parse(response.responseText);
  365. var rexe = /\/c\//;
  366. if(!vals.error) {
  367. mangaFormat = vals.body[0].urls.original.split('.').pop();
  368. //GM_log(mangaFormat);
  369. /*var vtxt = '';
  370. for(var x=0;x < vals.length;x++)
  371. vtxt=vtxt+x+':'+vals[x]+"\n";
  372. GM_log(vtxt);*/
  373. //GM_log("2nd");
  374. /*if (rexe.test(vals[9]))*/ mangaFull();
  375. }
  376. } else if (response.status == 400) {
  377. // accessToken ="";
  378. // GM_setValue("accessToken",accessToken);
  379. }
  380. }
  381. });
  382. var rexe = /\/c\//;
  383. //GM_log("isNewManga");
  384. //setTimeout(mangaFull,250);
  385. //GM_log("2nd");
  386. // mangaFull();
  387. }
  388. }
  389.  
  390. var vp_w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0)
  391. var vp_h = Math.max(document.documentElement.clientHeight, window.innerHeight || 0)
  392. function resizeImg(obj){
  393. //console.log("resizeImg");
  394. var imgW=obj.width;
  395. var imgH=obj.height;
  396. var imgAR=imgW/imgH;
  397.  
  398. obj.alt = obj.src;
  399.  
  400. /* set min size to 16x16px */
  401. obj.style.minWidth = '16px';
  402. obj.style.minHeight = '16px';
  403.  
  404. if(imgW > vp_w || imgH > vp_h) {
  405. if(imgAR < 1.0) {
  406. obj.height = vp_h*0.95;
  407. obj.width = obj.height*imgAR;
  408. } else {
  409. obj.width = vp_w*0.88;
  410. obj.height = obj.width/imgAR;
  411. }
  412. }
  413. }
  414.  
  415. function mangaFull() {
  416. Items = document.getElementsByTagName('img');
  417. var rexe = /\/img-/;
  418. for (var x = 0; x < unsafeWindow.pixiv.context.images.length; x++) {
  419. if (isNewManga) {
  420. if(rexe.test(unsafeWindow.pixiv.context.images[x]))
  421. 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);
  422. else
  423. unsafeWindow.pixiv.context.images[x]/*[0]*/ = unsafeWindow.pixiv.context.images[x]/*[0]*/.replace(/_p(\d+\.[a-zA-Z\?\d]+)$/, "_big_p$1");
  424. }
  425. preloadImg.push(new Image());
  426. preloadImg[preloadImg.length - 1].src = unsafeWindow.pixiv.context.images[x]/*[0]*/;
  427. }
  428.  
  429. for (var x = 0; x < Items.length; x++) {
  430. var datasrc = Items[x].getAttribute("data-src");
  431. if (datasrc) {
  432. //GM_log("original-datasrc="+datasrc+" ,mangaFormat="+mangaFormat);
  433. if(rexe.test(datasrc))
  434. datasrc = datasrc.replace(/c\/1200x1200\//, '').replace(/img-master/, 'img-original').replace(/_p(\d+).*(\.[a-zA-Z\?\d]+)$/, "_p$1."+mangaFormat);
  435. else
  436. datasrc = datasrc.replace(/_p(\d+\.[a-zA-Z\?\d]+)$/, "_big_p$1");
  437. //GM_log("new-datasrc="+datasrc+" ,mangaFormat="+mangaFormat);
  438. }
  439. Items[x].setAttribute("data-src", datasrc);
  440. Items[x].addEventListener('load', function(e) {resizeImg(e.target);},true);
  441. if (isNewManga) {
  442. //GM_log(x+".src="+Items[x].src);
  443. if(datasrc)
  444. Items[x].src = datasrc;
  445. else if(rexe.test(Items[x].src))
  446. Items[x].src = Items[x].src.replace(/c\/1200x1200\//, '').replace(/img-master/, 'img-original').replace(/_p(\d+).*(\.[a-zA-Z\?\d]+)$/, "_p$1$2");
  447. else
  448. Items[x].src = datasrc ? datasrc : Items[x].src.replace(/_p(\d+\.[a-zA-Z\?\d]+)$/, "_big_p$1");
  449. }
  450. }
  451. }
  452.  
  453. /*if(!autoLogin) {
  454. GM_registerMenuCommand("Enable Auto Login", function(){
  455. GM_setValue("autoLogin",true);
  456. });
  457. } else {
  458. GM_registerMenuCommand("Disable Auto Login", function(){
  459. GM_setValue("autoLogin",false);
  460. });
  461. }*/
  462. window.addEventListener("load", pixivlink, true);
  463.  
  464. // create button for manually trigger source link generation
  465. tmp = document.createElement ('input');
  466. tmp.id = 'pxlink';
  467. tmp.type = 'button';
  468. tmp.value='P';
  469. tmp.accessKey='x';
  470. tmp.style.position = "fixed";
  471. tmp.style.bottom = "0px";
  472. tmp.style.left = "0px";
  473. tmp.style.backgroundColor="#fdd";
  474.  
  475. tmp.addEventListener("click", function(e){postProcImg = new Array();pixivlink_run = 0;pixivlink();}, false);
  476. document.body.appendChild(tmp);
  477.  
  478. if(location.pathname != "/")
  479. setTimeout(function() {
  480. document.getElementById('pxlink').click();
  481. }, 2000);