Guest User

2ch.sortbyreply.user.js

a guest
May 12th, 2021
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         2ch.hk | Сортировка по ответам
  3. // @version      0.1
  4. // @match        ://2ch.hk/*/res/*.html
  5. // @icon         https://2ch.hk/favicon.ico
  6. // @grant        none
  7. // ==/UserScript==
  8.  
  9. (function () {
  10.   "use strict";
  11.  
  12.   function reply_count($post) {
  13.     return $post.querySelector(".post__refmap").childElementCount;
  14.   }
  15.  
  16.   if (document.querySelector("#de-panel-info")) {
  17.     document
  18.       .querySelector(".de-parea")
  19.       .children[0].insertAdjacentHTML(
  20.         "beforeend",
  21.         '[<span><input type="checkbox" id="sort-checkbox"> Сортировать по ответам</span>]'
  22.       );
  23.   } else {
  24.     document
  25.       .querySelector(".thread-nav__inner")
  26.       .insertAdjacentHTML(
  27.         "beforeend",
  28.         ' | <span><input type="checkbox" id="sort-checkbox"> Сортировать по ответам</span>'
  29.       );
  30.   }
  31.  
  32.   const $thread = document.querySelector(".thread");
  33.   //$thread.style.display = "flex";
  34.   $thread.style["flex-direction"] = "column";
  35.   $thread.children[0].style.order = -1000; // fix op-post
  36.  
  37.   document.querySelector("#sort-checkbox").addEventListener("change", (el) => {
  38.     if (el.target.checked) {
  39.       $thread.style.display = "flex";
  40.       const posts = document.querySelectorAll(".thread__post");
  41.       posts.forEach(($post) => {
  42.         $post.style.order = -reply_count($post);
  43.       });
  44.     } else {
  45.       $thread.style.display = "block";
  46.     }
  47.   });
  48. })();
Add Comment
Please, Sign In to add comment