Advertisement
AHOHNMYC

Stupid image grabber

Jan 4th, 2017
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name           Stupid image grabber
  3. // @namespace      Anon still delivers
  4. // @description    Open images under cursor in new tab or print link in console. Activates by pressing Ctrl+Shift+V (non used in Firefox)
  5. // @name:ru        Захватчик изображений
  6. // @description:ru Открывает все изображения в новых вкладках или печатает ссылки в консоль. Работает по нажатию Ctrl+Shift+V
  7. // @include        *
  8. // @version        0.2.7
  9. // @grant          GM_openInTab
  10. // ==/UserScript==
  11.  
  12. // Значения behavior:
  13. // 1. Запись в консоль ( доступна по Ctrl+Shift+K )
  14. // 2. Всплывающее окошечко
  15. // 3. Изображение открывается в новой вкладке
  16.  
  17. (function(){
  18.     const stimagr = {
  19.         behavior: 3,
  20.         debug   : false,
  21.         mouse   : {x:0,y:0},
  22.         start   : function() {
  23.  
  24.             listChild(document.body);
  25.  
  26.             // Рекурсивный обход страницы
  27.             function listChild(el) {
  28.                 // Перебор всех дочерних элементов
  29.                 for (let el1 of el.children) {
  30.                     if (isUnderCursor(el1)) {
  31.                         if( getImgLink(el1) ) {
  32.                             openImage( getImgLink(el1) );
  33.                         }
  34.                     }
  35.                     listChild(el1);
  36.                 }
  37.             }
  38.  
  39.             function isUnderCursor(el) {
  40.                 const mouse = stimagr.mouse;
  41.                 el = el.getBoundingClientRect();
  42.                 if (mouse.x > el.left && mouse.x < el.left + el.width && mouse.y > el.top && mouse.y < el.top + el.height) {
  43.                     return true;
  44.                 }
  45.                 return false;
  46.             }
  47.  
  48.             function openImage(link) {
  49.                 switch (stimagr.behavior) {
  50.                     case 1:
  51.                         console.log(link);
  52.                         break;
  53.                     case 2:
  54.                         alert(link);
  55.                         break;
  56.                     case 3:
  57.                         GM_openInTab(link);
  58.                         break;
  59.                 }
  60.             }
  61.  
  62.             function getImgLink(el) {
  63.                 // Получаем значение backgroundImage элемента
  64.                 let img = window.getComputedStyle(el).backgroundImage;
  65.                 if (img.slice(0,3) == 'url') {
  66.                     return img.slice(5,-2);
  67.                 }
  68.  
  69.                 // Если не удалось, проверяем, вдруг этот элемент — картинка в теге img, или ещё что-то содержащее атрибут src?
  70.                 if (el.nodeName == 'IMG' || el.hasAttribute('src') ) {
  71.                     return el.src;
  72.                 }
  73.  
  74.                 // А вдруг это canvas?
  75.                 if (el.nodeName == 'CANVAS') {
  76.                     return el.toDataURL();
  77.                 }
  78.  
  79.                 // Логгируем все странные элементы (обычно выводит всякие хитрые градиенты)
  80.                 if (stimagr.debug && img !== 'none') console.log(img);
  81.                 return false;
  82.             }
  83.         }
  84.     };
  85.  
  86.     // Схоронение координат мыши
  87.     document.addEventListener('mousemove', function (e) {
  88.         stimagr.mouse.x = e.clientX;
  89.         stimagr.mouse.y = e.clientY;
  90.     });
  91.  
  92.     // Обработка нажатия клавиш
  93.     document.addEventListener('keyup', function (e) {
  94.         // Если получили Ctrl+Shift+V, начинаем обход страницы в поисках картинок
  95.         if (e.ctrlKey && e.shiftKey && e.keyCode == 86) {
  96.             stimagr.start();
  97.         }
  98.     });
  99. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement