Guest User

Untitled

a guest
Sep 6th, 2024
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         SOSAKA IMAGES
  3. // @version      1
  4. // @match        https://2ch.hk/*
  5. // @match        https://2ch.life/*
  6. // @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  7. // @grant        GM_addStyle
  8. // ==/UserScript==
  9.  
  10. GM_addStyle(`
  11. #thread-images {
  12.     display: flex;
  13.     flex-wrap: wrap;
  14. }
  15. #thread-images > .post__image {
  16.     min-width: 180px;
  17. }
  18. `);
  19.  
  20. const container = createElement(`
  21.     <div id='thread-images' style='display: none;'></div>
  22. `);
  23.  
  24. const button = createElement(`
  25.     <div style="width: 16px; height: 16px; cursor: pointer;">
  26.          <svg viewBox="0 0 24 24">
  27.              <path d="M22,16V4A2,2 0 0,0 20,2H8A2,2 0 0,0 6,4V16A2,2 0 0,0 8,18H20A2,2 0 0,0 22,16M11,12L13.03,14.71L16,11L20,16H8M2,6V20A2,2 0 0,0 4,22H18V20H4V6"
  28.                    fill="#F5F5F5"/>
  29.          </svg>
  30.     </div>
  31.     `,
  32.     { onclick: toggleImages }
  33. );
  34.  
  35. function init() {
  36.     document.querySelector('div.tn').appendChild(button);
  37.     document.querySelector('div[de-form], div#js-posts').prepend(container);
  38. } if (/^https?:\/\/2ch\.(hk|life)\/\w+\/res\/\d+\.html[\?#]*/.test(document.location.href)) init();
  39.  
  40. function toggleImages() {
  41.     container.style.display = container.style.display ? '' : 'none';
  42.  
  43.     if (!container.childNodes.length) {
  44.         document.querySelectorAll('.post__image').forEach(addImage);
  45.  
  46.         const observer = new MutationObserver(ml => {
  47.             ml.forEach(m => m.addedNodes.forEach(n => { n.dispatchEvent(new MouseEvent('mouseover')); n.querySelectorAll('.post__image').forEach(addImage) } ));
  48.         });
  49.  
  50.         observer.observe(document.querySelector('.thread'), { childList: true });
  51.         document.querySelectorAll('.post').forEach(p => p.dispatchEvent(new MouseEvent('mouseover')));
  52.     }
  53. }
  54.  
  55. function addImage(i) {
  56.     const el = i.cloneNode(true);
  57.     const originalPrev = i.querySelector('.post__file-preview');
  58.     const prev = el.querySelector('.post__file-preview');
  59.     const link = el.querySelector('.post__image-link');
  60.     prev.id = '';
  61.     prev.onclick = () => { originalPrev.click(); return false; };
  62.     link.href = '';
  63.     container.appendChild(el);
  64. }
  65.  
  66. function createElement(html, assign, attributes) {
  67.     const container = document.createElement('div');
  68.     container.innerHTML = html.trim();
  69.     const element = container.childNodes[0];
  70.     element.remove();
  71.     if (assign) Object.assign(element, assign);
  72.     if (attributes) Object.keys(attributes).forEach(k => element.setAttribute(k, attributes[k]));
  73.     return element;
  74. }
Add Comment
Please, Sign In to add comment