Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name 4chan Advanced Hide replies keybind
- // @namespace b4k
- // @match *://boards.4chan.org/*
- // @match *://boards.4channel.org/*
- // @run-at document-start
- // ==/UserScript==
- (() => {
- "use strict";
- document.addEventListener("4chanXInitFinished", () => {
- let cPressed = false;
- // Key press check for 'c'
- document.addEventListener("keydown", (event) => {
- if (event.key.toLowerCase() === 'c') {
- cPressed = true;
- }
- });
- document.addEventListener("keyup", (event) => {
- if (event.key.toLowerCase() === 'c') {
- cPressed = false;
- }
- });
- // Apply to all existing posts in the thread
- document.querySelectorAll(".postContainer.replyContainer").forEach(initPost);
- // Apply to all new replies
- document.addEventListener("ThreadUpdate", (e) => {
- if (e.detail[404] === true) return;
- if (e.detail.newPosts.length > 0) {
- e.detail.newPosts.forEach(uID => {
- const post = document.getElementById(`pc${uID.split(".").pop()}`);
- if (post) initPost(post);
- });
- }
- });
- // Function to initialize each post
- function initPost(post) {
- const container = post.querySelector(".post.reply");
- container.addEventListener("click", function(e) {
- if (!cPressed) return;
- e.preventDefault();
- e.stopPropagation(); // Stop event bubbling to prevent unwanted actions
- // Open the dropdown menu
- const menuButton = post.querySelector(".menu-button");
- if (menuButton) {
- menuButton.click();
- // Wait for the menu to populate then access the Hide submenu
- setTimeout(() => {
- // Select the .hide-reply-link element
- const hideLink = post.querySelector(".hide-reply-link");
- if (hideLink) {
- // Dispatch a click event on the hideLink to open the submenu
- hideLink.dispatchEvent(new Event('click'));
- // Short delay for submenu to be visible then check and click checkboxes
- setTimeout(() => {
- // Correctly select the checkboxes within the submenu
- const submenu = post.querySelector(".dialog.submenu"); // Select the submenu container
- if (!submenu) return; // Exit if submenu not found
- const thisPostCheckbox = submenu.querySelector('label:nth-child(2) input[type="checkbox"]');
- const repliesCheckbox = submenu.querySelector('label:nth-child(3) input[type="checkbox"]');
- const makeStubCheckbox = submenu.querySelector('label:nth-child(4) input[type="checkbox"]');
- const byIdCheckbox = submenu.querySelector('label:nth-child(5) input[type="checkbox"]');
- // Function to check and click a checkbox if it's not already checked
- function checkAndClick(checkbox) {
- if (checkbox && !checkbox.checked) {
- checkbox.click();
- }
- }
- // Function to uncheck
- function unCheck(checkbox) {
- if (checkbox && checkbox.checked) {
- checkbox.click();
- }
- }
- checkAndClick(thisPostCheckbox);
- checkAndClick(repliesCheckbox);
- checkAndClick(makeStubCheckbox); // Always check "Make stub"
- unCheck(byIdCheckbox); // Always uncheck "By poster id"
- // Apply the hide settings. Find the Apply button within the submenu.
- const applyButton = submenu.querySelector('a.entry'); // Directly find in the submenu
- if(applyButton) {
- applyButton.click();
- }
- }, 50); // Small delay for submenu to appear
- }
- }, 50); // Small delay for menu to appear
- }
- });
- }
- }, false);
- })();
Advertisement
Add Comment
Please, Sign In to add comment