2ch delete post with rerply

try to take over the world!

Устаревшая версия на 09.09.2020. Перейти к последней версии.

// ==UserScript==
// @name         2ch delete post with rerply
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  try to take over the world!
// @author       You
// @include https://2ch.hk/*/res/*
// @include https://2ch.hk/*/res/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

const post = document.querySelectorAll('.post');

function createButton() {
  let button = document.createElement('button');
  button.innerText = 'Вырезать рак c метастазами 🗯';
  button.id = '__remove_mraz';

  button.style.marginLeft = '8px';
  button.style.cursor = 'pointer';
  button.style.border = '1px solid #bbb';
  button.style.font = 'bold 80% arial, helvetica, sans-serif';
  button.style.color = '#555';
  button.style.borderRadius = '10px';

  return button;
}

for (let i = 1; i < post.length; i++) {
  let button = createButton();
  post[i].firstElementChild.append(button);
}

function del(post) {
  let replyPost = document.querySelector(`#post-${post}`);
  let reply = replyPost.querySelectorAll('.post__refmap > .post-reply-link');
  //рекурсией проходимся по всем постам с ответами
  for (let i = 0; i < reply.length; i++) {
    del(reply[i].innerText.match(/\d+/)[0]);
  }
  //удаляем элементы ответа в постах
  const allReplyElem = document.querySelectorAll(`.post-reply-link[data-num="${post}"]`);
  for (let i = 0; i < allReplyElem.length; i++) {
    allReplyElem[i].remove();
  }

  replyPost.remove();
}

function deletePostWithResponse() {
  const { target } = event;
  if (target.id === '__remove_mraz') {
    const post = target.offsetParent.querySelector('.postbtn-reply-href').innerText;
    del(post);
  }
}

document.querySelector('#posts-form').onclick = deletePostWithResponse;

})();