SHARE
TWEET

Pixiv Display All Images - fix for "artworks" URL change

hjUfsz5 Oct 18th, 2019 (edited) 33 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Pixiv Display All Images
  3. // @namespace    superschwul
  4. // @version      2.5b
  5. // @description  Display all original images automatically: no need to click the medium image.
  6. // @homepageURL  https://greasyfork.org/en/scripts/36886-pixiv-display-all-images
  7. // @author       Superschwul
  8. // @author       anon
  9. // @match        https://www.pixiv.net/*stacc*
  10. // @match        https://www.pixiv.net/*member*
  11. // @match        https://www.pixiv.net/*bookmark*
  12. // @match        https://www.pixiv.net/*artworks*
  13. // @run-at       document-end
  14. // ==/UserScript==
  15.  
  16. // ===============================================================================
  17.  
  18. // USER OPTIONS
  19. var FIT_IMAGES_TO_SCREEN_WIDTH_AND_HEIGHT = true;
  20. var LOAD_MASTER_IMAGES_INSTEAD_OF_ORIGINAL = false;
  21. var SHOW_MINI_PIXIV_ADS = true;
  22.  
  23. // ===============================================================================
  24.  
  25. // CHANGELOG
  26. // 2.5b (unofficial) 2019-10-22 no longer reload entire page when exit from artwork page to non-artwork page.
  27. // 2.5a (unofficial) 2019-10-18 fixed pixiv url change, added a lazy workaround to reactivate the script on ajax request
  28. //  avatar replacement and staccfeed page (RSS/timeline) support are deprecated because i don't even know what they do
  29. // 2.5 2019-01-17 added firefox extension link
  30. // 2.4 2019-01-17 abort on id detection
  31. // 2.3 2018-10-23 refactoring, fixed multiple images support
  32. // 2.2 2018-09-18 better original url support
  33. // 2.1 2018-09-18 refactoring, bigger fixed avatar
  34. // 2.0 2018-09-14 better ajax support
  35. // 1.8 2018-09-13 added new member page support
  36. // 1.7 2018-09-11 added ajax thumbs support
  37. // 1.6 2018-09-02 fixed missing like button
  38. // 1.5 2018-08-29 improved image src identification
  39. // 1.4 2018-08-26 added hi-res option, improved avatar timing
  40. // 1.3 2018-08-16 removed feedback button
  41. // 1.2 2018-08-16 disabled sticky bar, added fit-to-screen option
  42. // 1.1 2018-06-21 fixed image src url
  43. // 1.0 2018-06-17 show original instead of master manga images
  44. // 0.8 2018-06-15 added fixed avatar
  45. // 0.7 2018-06-13 added link to works page at illustration page
  46. // 0.6 2018-06-12 updated to work with the new pixiv layout
  47. // 0.5 2018-01-07 hide buttons over thumbnails at works page
  48. // 0.4 2018-01-04 enlarge thumbnails at works page
  49. // 0.3 2018-01-03 replaced gm_xmlhttprequest by xmlhttprequest
  50. // 0.2 2017-12-30 added support for rtl manga pages
  51. // 0.1 2017-12-30 initial code
  52.  
  53. // ===============================================================================
  54.  
  55. /*
  56. function addMemberPageLink() {
  57.     var interval = myInterval(function() {
  58.         var usernames = document.getElementsByClassName('stacc_ref_illust_user_name');
  59.         if(usernames == undefined) return;
  60.         clearInterval(interval);
  61.         for(var username of usernames) {
  62.             username.style.display = 'block';
  63.             var usernameLink = username.getElementsByTagName('a')[0];
  64.             var memberPageLink = document.createElement('a');
  65.             memberPageLink.href = usernameLink.href.replace('member', 'member_illust').replace(/&from_sid=\d+/, '');
  66.             memberPageLink.target = '_blank';
  67.             memberPageLink.style.display = 'block';
  68.             memberPageLink.appendChild(document.createTextNode("More works"));
  69.             username.appendChild(memberPageLink);
  70.         }
  71.     });
  72. }
  73. */
  74.  
  75. function processIllustrationPage() {
  76.     getFigure();
  77.     //getAvatar();
  78. }
  79.  
  80. function getFigure() {
  81.     var interval = myInterval(function() {
  82.         var figure = document.getElementsByTagName('figure')[0];
  83.         if(figure == undefined) return;
  84.         clearInterval(interval);
  85.         addGallery();
  86.         var figureObserver = new MutationObserver(addGallery);
  87.         figureObserver.observe(figure, {childList: true});
  88.     });
  89. }
  90.  
  91. function removeGallery() {
  92.     var gallery = document.getElementById('pil_gallery');
  93.     if(gallery == undefined) return;
  94.     gallery.parentNode.removeChild(gallery);
  95. }
  96.  
  97. function addGallery() {
  98.     var interval = myInterval(function() {
  99.         if(!exists("document.getElementsByTagName('figure')[0].children[0].children[0].children[0]")) return;
  100.         clearInterval(interval);
  101.         removeGallery();
  102.         var gallery = document.createElement('div');
  103.         gallery.id = 'pil_gallery';
  104.         // "Mini Pixiv" ads
  105.         if(SHOW_MINI_PIXIV_ADS) {
  106.             gallery.innerHTML = `
  107.                 <div id="pil_extension">
  108.                     Try the Mini Pixiv extension for
  109.                     <a href="https://addons.mozilla.org/addon/mini-pixiv/">Firefox</a>
  110.                     and
  111.                     <a href="https://chrome.google.com/webstore/detail/mini-pixiv/lclakckbkbcigmpngmmchenoabiolall">Chrome</a>
  112.                 </div>
  113.             `;
  114.         }
  115.         console.log(gallery);
  116.         var header = document.getElementsByTagName('header')[0];
  117.         header.parentNode.insertBefore(gallery, header.nextSibling);
  118.         removeTitle();
  119.         getImagesForGallery();
  120.         //addFixedAvatar();
  121.     });
  122. }
  123.  
  124. function removeTitle() {
  125.     var interval = myInterval(function() {
  126.         var outer = document.getElementsByTagName('figure')[0].children[1];
  127.         if(outer == undefined) return;
  128.         var action = outer.getElementsByTagName('section')[0];
  129.         if(action == undefined) return;
  130.         clearInterval(interval);
  131.         outer.appendChild(action);
  132.         outer.removeChild(outer.children[0]);
  133.     });
  134. }
  135.  
  136. function getImagesForGallery() {
  137.     var outer = document.getElementsByTagName('figure')[0].children[0].children[0].children[0];
  138.     if(outer.tagName == 'CANVAS') {
  139.         return; //gif
  140.     }
  141.     if(outer.getAttribute('role') != 'presentation') {
  142.         getMultipleImages(); //multiple
  143.         return;
  144.     }
  145.     var interval = myInterval(function() {
  146.         if(outer.children[0].tagName != 'A') return;
  147.         if(outer.getElementsByTagName('img')[0] == undefined) return;
  148.         if(outer.getElementsByTagName('img')[0].src == document.location.href) return;
  149.         clearInterval(interval);
  150.         getSingleImage(); //single
  151.     });
  152. }
  153.  
  154. function getSingleImage() {
  155.     var images = [];
  156.     var thumb = document.getElementsByTagName('figure')[0].children[0].children[0].children[0].children[0];
  157.     var image = {original: thumb.href};
  158.     image.master = thumb.getElementsByTagName('img')[0].src;
  159.     images.push(image);
  160.     fillGallery(images);
  161. }
  162.  
  163. function getMultipleImages() {
  164.     var pathnames = location.pathname.split('/');
  165.     var id = pathnames[pathnames.indexOf('artworks') + 1];
  166.     var p = fetch('/ajax/illust/'+id, { credentials: 'same-origin' })
  167.     .then(r => r.json())
  168.     .then(r => r.body)
  169.     .then(data => {
  170.         var images = [];
  171.         for (let i = 0; i < data.pageCount; i++) {
  172.             var image = {master: data.urls.regular.replace('p0', 'p'+i),
  173.                          original: data.urls.original.replace('p0', 'p'+i)};
  174.             images.push(image);
  175.         }
  176.         fillGallery(images);
  177.     });
  178. }
  179.  
  180. function fillGallery(images) {
  181.     var gallery = document.getElementById('pil_gallery');
  182.     var i = 0;
  183.     for(var item of images) {
  184.         //legend
  185.         var legend = document.createElement('p');
  186.         var legendText = document.createTextNode('Image ' + ++i + ' of ' + images.length);
  187.         legend.appendChild(legendText);
  188.         gallery.appendChild(legend);
  189.         //container
  190.         var container = document.createElement('div');
  191.         container.className = 'img';
  192.         var image = document.createElement('img');
  193.         if(LOAD_MASTER_IMAGES_INSTEAD_OF_ORIGINAL) {
  194.             image.src = item.master;
  195.             var originalLink = document.createElement('a');
  196.             originalLink.href = item.original;
  197.             originalLink.appendChild(image);
  198.             container.appendChild(originalLink);
  199.         } else {
  200.             image.src = item.original;
  201.             container.appendChild(image);
  202.         }
  203.         gallery.appendChild(container);
  204.     }
  205. }
  206.  
  207. /*
  208. function getAvatar() {
  209.     var interval = myInterval(function() {
  210.         if(!exists("document.getElementsByTagName('aside')[1].children[0].children[0].children[0].href")) return;
  211.         var avatarLink = document.getElementsByTagName('aside')[1].children[0].children[0].children[0];
  212.         clearInterval(interval);
  213.         var avatarObserver = new MutationObserver(addFixedAvatar);
  214.         avatarObserver.observe(avatarLink, {attributeFilter: ['href']});
  215.         addFixedAvatar();
  216.     });
  217. }
  218.  
  219. function addFixedAvatar() {
  220.     var gallery = document.getElementById('pil_gallery');
  221.     if(gallery == null) return;
  222.     if(!exists("document.getElementsByTagName('aside')[1].children[0].children[0].children[0].href")) return;
  223.     var avatar = document.getElementsByTagName('aside')[1].children[0].children[0];
  224.     //add link to member page
  225.     var pilMemberLink = document.getElementById('pil_memberLink');
  226.     if(pilMemberLink != null) {
  227.         pilMemberLink.parentNode.removeChild(pilMemberLink);
  228.     }
  229.     var memberLink = document.createElement('a');
  230.     memberLink.id = 'pil_memberLink';
  231.     memberLink.href = avatar.children[0].href.replace('member', 'member_illust');
  232.     memberLink.appendChild(document.createTextNode('More works'));
  233.     avatar.parentNode.insertBefore(memberLink, avatar.nextSibling);
  234.     //add fixed avatar
  235.     var pilFixedAvatar = document.getElementById('pil_fixedAvatar');
  236.     if(pilFixedAvatar != null) {
  237.         pilFixedAvatar.parentNode.removeChild(pilFixedAvatar);
  238.     }
  239.     var fixedAvatar = document.createElement('div');
  240.     fixedAvatar.id = 'pil_fixedAvatar';
  241.     fixedAvatar.appendChild(avatar.cloneNode(true));
  242.     var links = fixedAvatar.getElementsByTagName('a');
  243.     for(var link of links) {
  244.         link.href = link.href.replace('member', 'member_illust');
  245.         if(link.style != undefined) {
  246.             link.style.backgroundImage = link.style.backgroundImage.replace('_50.', '_170.') + ', ' + link.style.backgroundImage;
  247.         }
  248.     }
  249.     gallery.appendChild(fixedAvatar);
  250. }
  251. */
  252.  
  253. function setStyles() {
  254.     var style = document.createElement('style');
  255.     style.type = 'text/css';
  256.     var styleCode = `
  257.         #pil_main + div {
  258.             display: none;
  259.         }
  260.         #pil_gallery {
  261.             padding: 0.3em 3em 1em 3em;
  262.             text-align: center;
  263.             color: #b3c1d2;
  264.             border-bottom: solid 1px white;
  265.             margin-bottom: 4em;
  266.             background: #ededed;
  267.         }
  268.         #pil_extension {
  269.             width: 970px;
  270.             margin: 0 auto;
  271.             text-align: right;
  272.         }
  273.         #pil_extension a {
  274.             color: #8699ba;
  275.         }
  276.         #pil_gallery .img {
  277.             padding-bottom: 4em;
  278.         }
  279.         #pil_gallery img {
  280.             max-width: 100%;
  281.             height: auto;
  282.             background: #dcdbdb;
  283.     `;
  284.     if(FIT_IMAGES_TO_SCREEN_WIDTH_AND_HEIGHT) {
  285.         styleCode += 'max-height: 90vh';
  286.     }
  287.     styleCode += `
  288.         }
  289.         figure > div:first-child > div {
  290.             background: rgb(250,250,250) !important;
  291.         }
  292.         figure > div:nth-child(2) > div {
  293.             position: unset !important;
  294.         }
  295.         .gtm-illust-recommend-zone {
  296.             z-index: 999;
  297.             position: relative;
  298.             background: #f5f5f5;
  299.         }
  300.     `;
  301.     style.innerHTML = styleCode;
  302.     document.getElementsByTagName('head')[0].appendChild(style);
  303. }
  304.  
  305. function getMainDiv() {
  306.     var interval = myInterval(function() {
  307.         if(!exists("document.getElementsByTagName('header')[0].nextElementSibling")) return;
  308.         var main = document.getElementsByTagName('header')[0].nextElementSibling;
  309.         clearInterval(interval);
  310.         if(main.id == ''){
  311.             setStyles();
  312.             main.id = 'pil_main';
  313.         }
  314.         if(main.id == 'pil_main' || main.id == 'pil_gallery'){
  315.             processPage();
  316.             var mainObserver = new MutationObserver(processPage);
  317.             mainObserver.observe(main, {childList: true});
  318.         }
  319.     });
  320. }
  321.  
  322. function processPage() {
  323.     if(document.getElementById('pil_main').children[0].style.display == 'flex') return;
  324.     removeGallery();
  325.     // stacc page
  326.     /*
  327.     if(document.location.href.search('stacc') != -1) {
  328.         addMemberPageLink();
  329.     }
  330.     */
  331.     // illustration page
  332.     if(document.location.href.search('artworks') != -1) {
  333.         processIllustrationPage();
  334.     }
  335. }
  336.  
  337. function myInterval(callback) {
  338.     var intervalCount = 0;
  339.     var interval = setInterval(function() {
  340.         intervalCount++;
  341.         if(intervalCount >= 100) {
  342.             clearInterval(interval);
  343.             return;
  344.         }
  345.         callback();
  346.     }, 100);
  347.     return interval;
  348. }
  349.  
  350. function exists(string) {
  351.     var el = '';
  352.     for(var sub of string.split('.')) {
  353.         (!el) ? el += sub : el += '.' + sub;
  354.         if(eval(el) == undefined) return false;
  355.     }
  356.     return true;
  357. }
  358.  
  359. (function() {
  360.     'use strict';
  361.     if (window.top != window.self) return;
  362.     var lastPathStr = "";
  363.     var pageURLCheckTimer = setInterval (
  364.         function () {
  365.             if (lastPathStr != location.pathname) {
  366.                 if (lastPathStr.indexOf('artworks') != -1 && location.href.search('artworks') == -1){
  367.                     lastPathStr = location.pathname;
  368.                     removeGallery();
  369.                 }
  370.                 else {
  371.                     lastPathStr = location.pathname;
  372.                     getMainDiv();
  373.                 }
  374.             }
  375.         }, 250);
  376.     //getMainDiv();
  377. })();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top