Advertisement
Guest User

Untitled

a guest
Mar 8th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Enlarge images on iichan
  3. // @namespace    http://iichan.hk/
  4. // @license      WTFPL
  5. // @version      0.1
  6. // @description  adds image enlargement on click to iichan
  7. // @author       Cirno
  8. // @match        http://iichan.hk/*
  9. // @grant        none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13.     'use strict';
  14.     var MIN_DISPLAY_SIZE = 700;  // ширина или высота, при которой картинки будут открываться как обычно
  15.  
  16.     var thumbs = document.querySelectorAll('.thumb');
  17.  
  18.     function removeClass(el, className) {
  19.         var reg = new RegExp('(\\s|^)' + className + '(\\s|$)');
  20.         el.className = el.className.replace(reg, ' ');
  21.     }
  22.  
  23.     function hasClass(el, className) {
  24.         if(!className) return false;
  25.         return (' ' + el.className + ' ').indexOf(' ' + className + ' ') !== -1;
  26.     }
  27.  
  28.     for (var i = thumbs.length - 1; i >= 0; i--) {
  29.         var img = thumbs[i];
  30.         var a = img.parentNode;
  31.         a.addEventListener('click', function(e) {
  32.             if (screen.width < MIN_DISPLAY_SIZE || screen.height < MIN_DISPLAY_SIZE) {
  33.                 return;
  34.             }
  35.             e.preventDefault();
  36.             console.log(this);
  37.             var thumb = this.querySelector('.thumb');
  38.  
  39.             if (hasClass(thumb, 'iichan-image-fullsize')) {
  40.                 thumb.src = thumb.originalSrc;
  41.                 thumb.width = thumb.originalWidth;
  42.                 thumb.height = thumb.originalHeight;
  43.                 removeClass(thumb, 'iichan-image-fullsize');
  44.                 return;
  45.             }
  46.             var imageSrc = this.href;
  47.             var thumbSrc = thumb.src;
  48.             thumb.originalSrc = thumb.src;
  49.             thumb.originalWidth = thumb.width;
  50.             thumb.originalHeight = thumb.height;
  51.             thumb.removeAttribute('width');
  52.             thumb.removeAttribute('height');
  53.             thumb.className += ' iichan-image-fullsize';
  54.             thumb.src = imageSrc;
  55.         });
  56.     }
  57.  
  58.     function appendCSS() {
  59.         var css = document.createElement('style');
  60.         css.type = 'text/css';
  61.         css.innerHTML =  '\
  62. .iichan-image-fullsize {\
  63. max-width: 97%;\
  64. max-height: 97%;\
  65. }\
  66. ';
  67.         document.body.appendChild(css);
  68.     }
  69.  
  70.     appendCSS();
  71. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement