36RAIN_打开链接并点击收藏按钮

打开多个链接,并在每个页面点击 #k_favorite 按钮

// ==UserScript==
// @name         36RAIN_打开链接并点击收藏按钮
// @namespace    http://tampermonkey.net/
// @version      2024-12-02
// @description  打开多个链接,并在每个页面点击 #k_favorite 按钮
// @author       冰红茶真好喝
// @match        https://rain36-vip.japaneast.cloudapp.azure.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=azure.com
// @license MIT
// @grant        none
// ==/UserScript==

(function () {
  "use strict";

  // 添加按钮的函数
  function addButton() {
    const container = document.querySelector("#nv_forum > header > div.hd_xin.stick-me.not-sticking > div");
    if (!container) return;

    const button = document.createElement("button");
    button.textContent = "批量收藏";
    button.className = "favorite-button";
    button.addEventListener("click", showCustomPrompt);

    container.appendChild(button);
  }

  // 显示自定义文本框的函数
  function showCustomPrompt() {
    const overlay = createOverlay();
    const promptBox = createPromptBox();

    overlay.appendChild(promptBox);
    document.body.appendChild(overlay);

    document.getElementById("submitBtn").addEventListener("click", handleSubmit);
    document.getElementById("cancelBtn").addEventListener("click", handleCancel);
  }

  // 生成遮罩层
  function createOverlay() {
    const overlay = document.createElement("div");
    overlay.className = "overlay";
    return overlay;
  }

  // 生成提示框
  function createPromptBox() {
    const promptBox = document.createElement("div");
    promptBox.className = "prompt-box";
    promptBox.innerHTML = `
      <label for="urlsInput">请输入每行一个要收藏的链接:</label>
      <textarea id="urlsInput" rows="6" style="width: 100%; margin-bottom: 10px;"></textarea>
      <div>
        <button id="submitBtn">确定</button>
        <button id="cancelBtn">取消</button>
      </div>
    `;
    return promptBox;
  }

  // 提交按钮的事件处理函数
  async function handleSubmit() {
    const linksInput = document.getElementById("urlsInput").value;
    const links = linksInput.split("\n").map(link => link.trim()).filter(Boolean);

    if (links.length > 0) {
      await openLinks(links);
    }

    removeOverlay();
  }

  // 取消按钮的事件处理函数
  function handleCancel() {
    removeOverlay();
  }

  // 移除遮罩层和提示框
  function removeOverlay() {
    const overlay = document.querySelector(".overlay");
    if (overlay) document.body.removeChild(overlay);
  }

  // 打开链接并点击 #k_favorite 按钮的函数
  async function openLinks(links) {
    for (let i = 0; i < links.length; i++) {
      const link = links[i];

      // 使用当前窗口打开新链接
      const currentWindow = window.open(link, "_blank");

      // 等待页面加载完成并点击收藏按钮
      await waitForPageLoad(currentWindow);
      await clickFavoriteButton(currentWindow);

      // 等待3秒钟后关闭窗口
      await delay(3000);
      if (!currentWindow.closed) currentWindow.close();
    }
  }

  // 等待页面加载完成
  function waitForPageLoad(currentWindow) {
    return new Promise(resolve => {
      const interval = setInterval(() => {
        if (currentWindow.document.readyState === "complete") {
          clearInterval(interval);
          resolve();
        }
      }, 500);
    });
  }

  // 点击收藏按钮
  function clickFavoriteButton(currentWindow) {
    return new Promise(resolve => {
      const favoriteButton = currentWindow.document.querySelector("#k_favorite");
      if (favoriteButton) {
        favoriteButton.click();
        resolve();
      } else {
        resolve();
      }
    });
  }

  // 延迟函数
  function delay(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
  }

  // 页面加载完成后初始化按钮
  addButton();
})();

// CSS样式
const style = document.createElement("style");
style.textContent = `
  .favorite-button {
    padding: 10px 20px;
    font-size: 16px;
    background-color: #ff6f3d;
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    transition: background-color 0.3s ease;
  }
  .favorite-button:hover {
    background-color: #cf5b35;
  }

  .overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 9999;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .prompt-box {
    background-color: white;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    max-width: 500px;
    width: 100%;
    text-align: center;
  }

  .prompt-box label {
    font-size: 14px;
    margin-bottom: 10px;
    display: block;
  }

  .prompt-box button {
    padding: 8px 16px;
    margin: 10px 5px;
    font-size: 14px;
    cursor: pointer;
    background-color: #ff6f3d;
    color: white;
    border: none;
    border-radius: 5px;
    transition: background-color 0.3s ease;
  }

  .prompt-box button:hover {
    background-color: #cf5b35;
  }

  .prompt-box textarea {
    padding: 8px;
    font-size: 14px;
    border: 1px solid #ccc;
    border-radius: 4px;
    width: 100%;
  }
`;
document.head.appendChild(style);