DeepFloodbeta

【欢愉】论坛优化油猴脚本,一键订阅论坛里“雨姐”文章,从此再也不用担心错过了追番小文

这几天论坛里总有半夜友友发癫,但是怕早起错过发癫小文怎么办?

不用担心!花火大人来!帮!忙!emote

有了这个脚本,再也不用担心错过订阅推送了,论坛左边栏直接跳转,花火亲自设计的LOGO,花了一个系统时纯手搓脚本
好脚本不必多说,直接上图,速用:

image

image

丢到油猴脚本里使用即可,友友还有什么要想的,直接告诉我,或者稍微改一下脚本里的跳转链接,追番从未如此轻松过~
emote

// ==UserScript==
// @name         DeepFlood x 我想雨姐了
// @namespace    https://欢愉
// @version      0.0.1
// @description   酒神看到眼前一黑的优化脚本
// @match        https://www.deepflood.com/*
// @match        https://deepflood.com/*
// @run-at       document-idle
// @grant        none
// @author       Sparkle
// @icon        https://cdn.nodeimage.com/i/QgISXVhan8PH3f52hfe6M3jqZTojUHuX.png
// ==/UserScript==

(function () {
  'use strict';

  const LABELS = ['人工智能', '摸鱼闲聊', '情感八卦'];
  const INJECT_TEXT = '我想雨姐';
  const INJECT_HREF = 'https://www.deepflood.com/search?q=%E6%83%B3%E9%9B%A8%E5%A7%90';
  const LOGO_URL = 'https://cdn.nodeimage.com/i/jptcK3LNB8ZNV84yPsMAqWBnNvQC8dRQ.png';
  const FLAG = 'tmWyjInjected';

  function waitFor(conditionFn, { interval = 200, timeout = 10000 } = {}) {
    return new Promise((resolve, reject) => {
      const t0 = Date.now();
      const timer = setInterval(() => {
        try {
          const result = conditionFn();
          if (result) {
            clearInterval(timer);
            resolve(result);
          } else if (Date.now() - t0 > timeout) {
            clearInterval(timer);
            reject(new Error('waitFor timeout'));
          }
        } catch (err) {
          clearInterval(timer);
          reject(err);
        }
      }, interval);
    });
  }

  function findTagContainer(root = document) {
    const anchors = Array.from(root.querySelectorAll('a'));
    const hits = anchors.filter(a => LABELS.includes(a.textContent.trim()));
    if (hits.length === 0) return null;

    const candidates = ['ul', 'nav', 'section', 'div', '.category', '.tags', '.pure-menu'];
    for (const hit of hits) {
      const container = hit.closest(candidates.join(','));
      if (container && LABELS.every(lbl => container.textContent.includes(lbl))) {
        return container;
      }
    }
    return hits[0].parentElement;
  }

  function alreadyInjected(container) {
    return container && container.dataset[FLAG] === '1';
  }

  function markInjected(container) {
    if (container) container.dataset[FLAG] = '1';
  }

  // 生成一个与原 a 标签一致的结构
  function createAlignedTag(sampleAnchor) {
    const link = document.createElement('a');
    link.href = INJECT_HREF;
    link.target = '_blank';
    link.rel = 'noopener';
    link.textContent = INJECT_TEXT;

    // ✅ 继承 class 和样式
    if (sampleAnchor) {
      link.className = sampleAnchor.className || '';
      const style = getComputedStyle(sampleAnchor);
      link.style.cssText = style.cssText;
    }

    // 创建 logo(在左边)
    const logo = document.createElement('img');
    logo.src = LOGO_URL;
    logo.alt = '文约雨姐 logo';
    logo.style.width = '18px';
    logo.style.height = '18px';
    logo.style.verticalAlign = 'middle';
    logo.style.marginRight = '4px';
    logo.style.cursor = 'pointer';

    // 点击 logo 也跳相同链接
    logo.addEventListener('click', () => window.open(INJECT_HREF, '_blank'));

    // ✅ 插入 logo 到 a 标签内部最前面
    link.prepend(logo);

    return link;
  }

  function inject(container) {
    if (!container || alreadyInjected(container)) return false;

    const sampleAnchor = container.querySelector('a');
    const newLink = createAlignedTag(sampleAnchor);

    // 如果原结构是 <ul><li><a></a></li>,保持一致
    if (sampleAnchor && sampleAnchor.closest('li')) {
      const li = document.createElement('li');
      li.appendChild(newLink);
      li.className = sampleAnchor.closest('li').className || '';
      container.appendChild(li);
    } else {
      // 否则直接追加 a
      container.appendChild(newLink);
    }

    markInjected(container);
    return true;
  }

  async function mainOnce(root = document) {
    try {
      await waitFor(() => {
        const c = findTagContainer(root);
        return c && LABELS.every(lbl => c.textContent.includes(lbl)) ? c : null;
      }, { interval: 200, timeout: 15000 });

      const container = findTagContainer(root);
      if (!container) return;
      inject(container);
    } catch (e) {}
  }

  mainOnce();
  const mo = new MutationObserver(() => mainOnce());
  mo.observe(document.documentElement, { childList: true, subtree: true });
})();
12
12