AHOHNMYC

vk.com/docs gallery

Jan 27th, 2018
2,130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         vk.com/docs gallery
  3. // @namespace    vk.com
  4. // @version      0.0.7
  5. // @description  gallery mode for VK docs
  6. // @author       AHOHNMYC
  7. // @match        https://vk.com/*
  8. // @supportURL   https://pastebin.com/u/AHOHNMYC
  9. // @grant        GM_registerMenuCommand
  10. // ==/UserScript==
  11.  
  12. /*   Установка: сначала устанавливаешь TamperMonkey, открываешь его настройки,
  13.  *   там создаёшь новый скрипт, куда и копируешь то, что сейчас читаешь.
  14.  *
  15.  *   На страницe с документами скрипт делает подобие галереи, информация о файле
  16.  *   показывается по наведению курсора. В окошке информации добавляются ссылки для
  17.  *   быстрого перехода в Профиль, Альбомы и Фотки пользователя-владельца документа.
  18.  *   Также эти ссылки добавлены в виде кнопок на страницу с документом.
  19.  *
  20.  *   На страницы с отдельными документами (даже удалёнными) скрипт добавляет несколько хоткеев:
  21.  *   Alt+P — профиль
  22.  *   Alt+A — альбомы юзера
  23.  *   Alt+F — фотографии юзера
  24.  *   Alt+Стрелка_влево, Alt+Стрелка_вправо — следущий и предыдущий документы
  25.  *      (но, конечно, то, будут ли они доступны для просмотра или нет, никому неизвестно)
  26.  *
  27.  *   Также есть возможность узнать User ID. Это нужно для некоторых майнеров.
  28.  *   Для этого нужно открыть профиль пользователя, а затем нажать на иконку TamperMonkey и кликнуть
  29.  *   на пункт 'Показать User ID'.
  30.  */
  31.  
  32. const docsGalleryStyle = `
  33. .docs_item_actions {display: none}
  34. .docs_item:not(:hover) .docs_item_thumb + * + .docs_item_cont {display: none;}
  35.  
  36. .docs_item_thumb + * + .docs_item_cont {position: absolute; margin-left: 130px; z-index:2; background: white; min-height: 50px}
  37. .docs_item_icon  + * + .docs_item_cont {margin-left: 40px; width: 225px}
  38.  
  39. .docs_item {padding: 1px 0 0 6px !important; float: left; border: none}
  40. .docs_item_thumb_wrap {margin-left: 0; width: auto; height: 100px; max-width: 130px}
  41. .docs_item_thumb_img, .docs_item_thumb {max-height: none; width: auto; height: auto}
  42. .docs_wrap {padding: 0}
  43.  
  44. #docs_search_more {display: block !important}
  45. `;
  46.  
  47. /* Если мы попали на страницу с отдельным документом */
  48. if ( location.pathname.match(/doc\d+_\d+/) ) {
  49.     addStyle ('.can_zoom {max-height: 800px}');
  50.     let linkPrefix = location.href.match(/[^_]+/)[0];
  51.     let prevDocLink = linkPrefix +'_'+ (+location.href.match(/_(\d+)/)[1]-1);
  52.     let nextDocLink = linkPrefix +'_'+ (+location.href.match(/_(\d+)/)[1]+1);
  53.  
  54.     addButton('Профиль', linkPrefix.replace('doc',   'id'  ));
  55.     addButton('Альбомы', linkPrefix.replace('doc', 'albums'));
  56.     addButton( 'Фотки',  linkPrefix.replace('doc', 'photos'));
  57.  
  58.     const ARROW_LEFT = 37, ARROW_RIGHT = 39, KEY_A = 97, KEY_F = 102, KEY_P = 112;
  59.     addEventListener('keypress', e=>{
  60.         if (!e.altKey) return;
  61.         if (e.keyCode == ARROW_LEFT ) location.href = prevDocLink;
  62.         if (e.keyCode == ARROW_RIGHT) location.href = nextDocLink;
  63.         if (e.charCode == KEY_P) location.href = linkPrefix.replace('doc',   'id'  );
  64.         if (e.charCode == KEY_A) location.href = linkPrefix.replace('doc', 'albums');
  65.         if (e.charCode == KEY_F) location.href = linkPrefix.replace('doc', 'photos');
  66.     });
  67. } else {
  68. /* Иначе, в общем случае */
  69.     if ( location.pathname.match('/docs') ) {
  70.         addEventListener('mousemove', addLinksToDocs);
  71.         addStyle(docsGalleryStyle);
  72.     } else {
  73.         GM_registerMenuCommand('Показать User ID', ()=>document.querySelector('.page_name').textContent = 'User ID: ' + cur.options.user_id);
  74.     }
  75. }
  76.  
  77. /* Вспомогательные функции */
  78.  
  79. function addStyle(content) {document.head.appendChild(document.createElement('style')).textContent = content;}
  80.  
  81. function addLinksToDocs() {
  82.     if (document.querySelectorAll('.docs_item_cont')[document.querySelectorAll('.docs_item_cont').length-1].querySelector('.gallery_script_link')) return;
  83.     document.querySelectorAll('.docs_item_cont').forEach(container => {
  84.         if (container.querySelector('.gallery_script_link')) return;
  85.         let linkPrefix = container.querySelector('.docs_item_name').href.match(/[^_]+/)[0];
  86.         addLinkToDescription(container, 'Профиль', linkPrefix.replace('doc',   'id'  ));
  87.         addLinkToDescription(container, 'Альбомы', linkPrefix.replace('doc', 'albums'));
  88.         addLinkToDescription(container,  'Фотки',  linkPrefix.replace('doc', 'photos'));
  89.     });
  90. }
  91.  
  92. function addLinkToDescription(parent, text, link) {
  93.     parent.appendChild(document.createTextNode(' '));
  94.     let a = parent.appendChild(document.createElement('a'));
  95.     a.textContent = text;
  96.     a.href = link;
  97.     a.className = 'gallery_script_link';
  98. }
  99.  
  100. function addButton(text, href) {
  101.     if ( !document.querySelector('.clear_fix') ) return;
  102.     let but = document.createElement('button');
  103.     but.className = 'flat_button fl_r';
  104.     but.textContent = text;
  105.     but.href = href;
  106.     but.addEventListener('click', e=>location.href=e.target.href);
  107.     document.querySelector('.clear_fix').appendChild(but);
  108. }
Advertisement
Add Comment
Please, Sign In to add comment