8janny

8chan janny tools

// ==UserScript==
// @name        8janny
// @description 8chan janny tools
// @version     0.1
// @license     MIT
// @namespace   9e7f6239-592e-409b-913f-06e11cc5e545
// @match       https://8chan.moe/*/res/*
// @match       https://8chan.se/*/res/*
// @grant       unsafeWindow
// @run-at      document-idle
// ==/UserScript==

// Overwrite deleteSinglePost to stay in thread when deleting posts by an IP
unsafeWindow.postingMenu.deleteSinglePost = function(boardUri, threadId, post, fromIp, unlinkFiles, wipeMedia, innerPart, forcedPassword, onThread, trash) {
  let key = `${boardUri}/${threadId}`;
  if (post !== threadId) key += `/${post}`;

  const postingPasswords = JSON.parse(localStorage["postingPasswords"] || "{}");
  const password = postingPasswords[key] || localStorage["deletionPassword"] || document.getElementById("deletionFieldPassword")

  let action;
  if (fromIp) action = onThread ? "thread-ip-deletion" : "ip-deletion";
  else action = trash ? "trash" : "delete";

  const req = {
    confirmation: true,
    password,
    deleteUploads: unlinkFiles,
    deleteMedia: wipeMedia,
    action
  };

  const reqKey = key.replaceAll("/", "-");
  req[reqKey] = true;

  api.formApiRequest("contentActions", req, (status, data) => {
    if (status !== "ok") {
      alert(`[${action}] FAILED : ${JSON.stringify(data)}`);
      return;
    }

    // data is undefined if you try to delete an already deleted post (or something?)
    const removed = !data || data["removedThreads"] || data["removedPosts"];
    if (removed) {
      if (unlinkFiles) {
       innerPart.querySelector(".panelUploads")[0].remove();
      } else if (fromIp) {
        const posterId = innerPart.querySelector(".labelId").innerText;
        unsafeWindow.posting.idsRelation[posterId].forEach((innerPost) => {
          innerPost.parentNode.remove();
        });
      } else if (data["removedThreads"]) {
        window.location.pathname = `/${boardUri}`;
      } else {
        let post = innerPart.parentNode;
        if (typeof(reports) !== "undefined") post = post.parentNode;
        post.remove();
      }
    } else {
      alert("Did not delete anything. Probably due to incorrect deletion password.");
    }
  });
}