4chan-auto-watcher

a script to automatically watch 4chan threads matching a pattern

目前為 2023-11-22 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        4chan-auto-watcher
// @namespace   github.com/diegostafa/userscripts
// @match       https://boards.4chan.org/*/catalog
// @match       https://boards.4channel.org/*/catalog
// @version     3
// @author      Diego <[email protected]> (github.com/diegostafa)
// @description a script to automatically watch 4chan threads matching a pattern
// @run-at      document-end
// ==/UserScript==

const currentBoard = window.location.pathname.split("/").filter((s) => s !== "")[0];

let boardsAndFilters = [
  ["", /^[^a-z]*$/],
];

const buildUi = () => {

  const createButton = (text) => {
    let button = document.createElement("div");
    button.innerHTML = text;
    button.style.backgroundColor = "#444444";
    button.style.color = "white";
    button.style.textAlign = "center";
    button.style.border = "none";
    button.style.padding = "2px";
    button.style.cursor = "pointer";
    return button;
  };

  const createAutowatchButton = () => {
    let button = createButton("autowatch");
    button.addEventListener("click", () => { autoWatch(); });
    return button;
  };

  const createConfigButton = () => {
    let button = createButton("config");

    button.addEventListener("click", () => {
      var win = window.open("", "Autowatch config", "toolbar=no");
      win.document.body.innerHTML = `TODO`;

    });

    return button;
  };

  let threadWatcher = document.getElementById("threadWatcher");
  let btnContainer = document.createElement("div");

  btnContainer.style.display = "grid";
  btnContainer.style.gridTemplateColumns = "100%";
  btnContainer.style.gridGap = "2px";
  // btnContainer.appendChild(createConfigButton());
  btnContainer.appendChild(createAutowatchButton());

  threadWatcher.appendChild(btnContainer);
};

const isThreadMatching = (filters) => (thread) => {
  const teaser = thread.querySelector('.teaser');

  if (!teaser) return false;

  return filters.reduce(
    (acc, [board, filter]) =>
      acc = acc || (filter.test(teaser.textContent) && (board === "" || currentBoard === board)), false);
};

const autoWatch = () => {
  let threads = Array.from(document.querySelectorAll('#threads .thread'));

  threads
    .filter(isThreadMatching(boardsAndFilters))
    .map((thread) => thread.querySelector('span.watchIcon'))
    .filter((watchIcon) => watchIcon !== null)
    .forEach((watchIcon) => watchIcon.click());
};

buildUi();