Guest User

Untitled

a guest
Oct 22nd, 2022
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name     2ch replacement
  3. // @version  1
  4. // @grant    none
  5. // @include  https://2ch.hk/po/*
  6. // @include  https://2ch.life/po/*
  7. // ==/UserScript==
  8.  
  9. const replacements = new Map([
  10.  ['img[title="Коммунизм"] + img[src="/flags/A1.png"]', 'Ко-ко-ко!'],
  11. ]);
  12.  
  13. function processPost(postEl) {
  14.   for (const [query, replace] of replacements) {
  15.     if (postEl.querySelector(query)) {
  16.       postEl.querySelector('.post__message').textContent = replace;
  17.       return;
  18.     }
  19.   }
  20. }
  21.  
  22. function processThread(thrEl) {
  23.   for (const el of thrEl.querySelectorAll('.post_type_reply')) {
  24.     processPost(el);
  25.   }
  26.   if (document.URL.includes('/res/')) {
  27.     const observer = new MutationObserver((mutationList, observer) => {
  28.           console.log('mutation');
  29.       for (const mutation of mutationList) {
  30.         for (const el of mutation.addedNodes) {
  31.           if (el.classList.contains('post_type_reply')) {
  32.                     processPost(el);
  33.           }
  34.         }
  35.       }
  36.     });
  37.     observer.observe(thrEl, {childList: true});
  38.   }
  39. }
  40.  
  41. function runScript() {
  42.   for (const el of document.querySelectorAll('.thread')) {
  43.     processThread(el);
  44.   }
  45. }
  46.  
  47. if(document.readyState !== 'loading') {
  48.   runScript();
  49. } else {
  50.   document.addEventListener('DOMContentLoaded', runScript);
  51. }
Add Comment
Please, Sign In to add comment