Guest User

kpop.re_thumbnail_files.user.js

a guest
Apr 25th, 2021
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @version      0.2
  3. // @description  display embed thumbnails as if they were images
  4. // @description  thanks to loopvid's kpop.re_post_id_fix.user.js for the handleNewPosts code
  5. // @match        *://kpop.re/*
  6. // ==/UserScript==
  7.  
  8. (function() {
  9.     function handlePost(p) {
  10.         let files = p.querySelector('.post-files');
  11.         if (!files) {
  12.             files = document.createElement('div');
  13.             files.className = 'post-files';
  14.             p.querySelector('.post-body').insertAdjacentElement('afterbegin', files);
  15.         }
  16.                
  17.         let embeds = p.querySelectorAll('.post-embed');
  18.         embeds.forEach(function(e) {
  19.             let thumbnail = e.getAttribute('data-thumbnail_url');
  20.             let link = e.getAttribute('href');
  21.  
  22.             let figure = document.createElement('figure');
  23.             figure.className = 'post-file';
  24.             let caption = document.createElement('figcaption');
  25.             caption.className = 'post-file-info';
  26.             if (link.length > 30)
  27.                 caption.innerText = link.substr(0, 30) + '...';
  28.             else
  29.                 caption.innerText = link;
  30.             figure.appendChild(caption);
  31.             let a = document.createElement('a');
  32.             a.className = 'post-file-link';
  33.             a.href = link;
  34.             a.target = '_blank';
  35.             figure.appendChild(a);
  36.             let img = document.createElement('img');
  37.             img.className = 'post-file-thumb';
  38.             img.src = thumbnail;
  39.             img.style.maxWidth = '200px';
  40.             img.style.maxHeight = '200px';
  41.             img.loading = 'lazy';
  42.             img.target = '_blank';
  43.             a.appendChild(img);
  44.  
  45.             p.querySelector('.post-files').insertAdjacentElement('beforeend', figure);
  46.  
  47.             let fileCount = files.querySelectorAll('.post-file').length;
  48.             if (fileCount >= 1)
  49.                 p.classList.add('post_file');
  50.             if (fileCount > 1)
  51.                 p.classList.add('post_files');
  52.         });
  53.     }
  54.  
  55.     function handleInitialPosts() {
  56.         let posts = document.querySelectorAll('.post');
  57.         posts.forEach(function(p) { handlePost(p) });
  58.     }
  59.  
  60.     function handleNewPosts(mutations) {
  61.         var i, j, newPosts, post, parentPost;
  62.         for (i=0; i < mutations.length; i++) {
  63.             if (!(mutations[i].type === 'childList' && mutations[i].addedNodes &&
  64.                 mutations[i].addedNodes.length > 0)) {
  65.                 continue;
  66.             }
  67.             newPosts = mutations[i].addedNodes;
  68.             for (j=0; j < newPosts.length; j++) {
  69.                 post = newPosts[j];
  70.                 parentPost = post ? post.parentPost : null;
  71.                 if (!(post && parentPost && post.tagName == 'ARTICLE' &&
  72.                     post.classList.contains('post'))) { continue; }
  73.                 handlePost(post);
  74.             }
  75.         }
  76.     }
  77.  
  78.     let newPostObserver = new MutationObserver(handleNewPosts);
  79.     let threadElem = document.querySelector('article.thread');
  80.     newPostObserver.observe(threadElem, {childList: true, subtree: true});
  81.  
  82.     setTimeout(() => handleInitialPosts(), 4000);
  83. })();
Advertisement
Add Comment
Please, Sign In to add comment