跳到正文

个人记录

骚扰别人(慎用)

RainMeng666约 7 分钟
原文
文章信息与版本

文章信息

发布
2026年10月21日
收录
2026年10月21日
更新
2026年11月18日
篇幅
约 7 分钟

版本

  1. v1当前
JavaScript
(() => {
  'use strict';

  const CFG = {
    textarea: '.editor textarea',
    sendBtn: '.editor .send button',
    interval: 800,   // 每条消息之间的间隔(ms)
    gap: 300,        // 轮询重试间隔(ms)
    maxRetry: 20
  };

  const sleep = ms => new Promise(r => setTimeout(r, ms));

  const setValue = (el, value) => {
    const proto = el instanceof HTMLTextAreaElement
      ? HTMLTextAreaElement.prototype
      : HTMLInputElement.prototype;
    Object.getOwnPropertyDescriptor(proto, 'value').set.call(el, value);
    el.dispatchEvent(new Event('input',  { bubbles: true, composed: true }));
    el.dispatchEvent(new Event('change', { bubbles: true }));
  };

  const waitFor = async (sel, tries = CFG.maxRetry) => {
    for (let i = 0; i < tries; i++) {
      const el = document.querySelector(sel);
      if (el) return el;
      await sleep(CFG.gap);
    }
    return null;
  };

  const disabled = btn =>
    btn.disabled ||
    btn.classList.contains('disabled') ||
    btn.getAttribute('aria-disabled') === 'true';

  async function sendOne(text) {
    const ta = await waitFor(CFG.textarea);
    if (!ta) return false;

    ta.focus();
    setValue(ta, text);

    for (let i = 0; i < CFG.maxRetry; i++) {
      const btn = document.querySelector(CFG.sendBtn);
      if (btn && !disabled(btn) && ta.value.trim()) {
        btn.click();
        return true;
      }
      await sleep(CFG.gap);
    }

    ta.dispatchEvent(new KeyboardEvent('keydown', {
      key: 'Enter', code: 'Enter', keyCode: 13, which: 13,
      ctrlKey: true, bubbles: true, cancelable: true
    }));
    return true;
  }

  // ---- 状态控制 ----
  let running = false;
  let stopFlag = false;

  window.__autoSendStop = () => {
    stopFlag = true;
    running = false;
    console.log('[autoSend] 已请求停止循环');
  };

  // ---- 单次发送(原有) ----
  window.__autoSend = async (list) => {
    const arr = Array.isArray(list) ? list : [list];
    for (const t of arr) {
      console.log('[autoSend] ->', t);
      if (!(await sendOne(t))) {
        console.warn('[autoSend] 找不到输入框,已停止');
        break;
      }
      await sleep(CFG.interval);
    }
  };

  // ---- 循环发送 ----
  // 用法:
  // __autoSendLoop("消息")                          // 无限循环发送单条
  // __autoSendLoop(["A", "B"], 1000)                // 循环发送列表,每条间隔 1000ms
  // __autoSendLoop(["A", "B"], 1000, 5)             // 最多循环 5 轮
  // __autoSendLoop("消息", 500, Infinity, 2000)     // 每条间隔500ms,无限轮,每轮后等2000ms
  window.__autoSendLoop = async (messages, interval = CFG.interval, maxLoops = Infinity, loopDelay = 0) => {
    if (running) {
      console.warn('[autoSend] 已有循环正在运行,请先调用 __autoSendStop()');
      return;
    }
    running = true;
    stopFlag = false;

    const arr = Array.isArray(messages) ? messages : [messages];
    let loopCount = 0;

    while (!stopFlag && loopCount < maxLoops) {
      loopCount++;
      console.log(`[autoSend] 第 ${loopCount} 轮开始`);
      for (const t of arr) {
        if (stopFlag) break;
        console.log('[autoSend] ->', t);
        const ok = await sendOne(t+"x"+loopCount);
        if (!ok) {
          console.warn('[autoSend] 发送失败,循环终止');
          stopFlag = true;
          break;
        }
        await sleep(interval);
      }
      if (stopFlag) break;
      if (loopDelay > 0) {
        console.log(`[autoSend] 本轮结束,等待 ${loopDelay}ms 后继续...`);
        await sleep(loopDelay);
      }
    }

    running = false;
    console.log('[autoSend] 循环结束');
  };

  console.log('[autoSend] 注入完成。');
  console.log('单次发送: __autoSend("你好") 或 __autoSend(["消息1","消息2"])');
  console.log('循环发送: __autoSendLoop("消息") 或 __autoSendLoop(["A","B"], 1000, 5)');
  console.log('停止循环: __autoSendStop()');
})();
原文