Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

4.4 KB
Raw Blame
// Xiaohongshu desktop recipe tweaks:
// 1. On creator.xiaohongshu.com, clicking the top-left logo returns to /explore.
// 2. On www.xiaohongshu.com, auto-closes the unsolicited Diandian AI chat drawer
//    while preserving the search filter button; manual clicks on the AI entry
//    still open it normally. Remove the second block if you prefer AI expanded.
(() => {
  const host = location.hostname;

  if (host === "creator.xiaohongshu.com") {
    const HOME = "https://www.xiaohongshu.com/explore";
    const MARK = "pakeHome";

    const findLogo = () => {
      for (const svg of document.querySelectorAll("svg")) {
        const box = svg.getBoundingClientRect();
        if (box.width === 0 || box.top > 80 || box.left > 160) continue;
        for (
          let group = svg.parentElement, depth = 0;
          group && depth < 4;
          group = group.parentElement, depth++
        ) {
          const width = group.getBoundingClientRect().width;
          if (width > 420) break;
          if (group.textContent.includes("创作服务平台")) return group;
        }
        return svg.parentElement;
      }
      return null;
    };

    let marked = null;
    const mark = () => {
      if (marked?.isConnected) return;
      const logo = findLogo();
      if (!logo || logo.dataset[MARK]) return;
      marked = logo;
      logo.dataset[MARK] = "1";
      logo.style.cursor = "pointer";
      logo.addEventListener("click", (event) => {
        if (
          event.button !== 0 ||
          event.metaKey ||
          event.ctrlKey ||
          event.shiftKey ||
          event.altKey
        )
          return;
        event.preventDefault();
        event.stopPropagation();
        location.href = HOME;
      });
    };

    const start = () => {
      mark();
      new MutationObserver(mark).observe(document.body, {
        childList: true,
        subtree: true,
      });
    };
    if (document.body) start();
    else document.addEventListener("DOMContentLoaded", start, { once: true });
    return;
  }

  if (host !== "xiaohongshu.com" && !host.endsWith(".xiaohongshu.com")) return;

  const CLOSE_SELECTOR =
    'button[aria-label="关闭 AI 对话"], button[aria-label="收起 AI 对话"]';
  const AI_TRIGGER_RE = /点点|\bAI\b|问一问/i;
  let manualOpen = false;
  let lastHref = location.href;

  const syncRoute = () => {
    if (location.href !== lastHref) {
      lastHref = location.href;
      manualOpen = false;
    }
  };

  const isAiTrigger = (target) => {
    if (!(target instanceof Element)) return false;
    if (target.closest(CLOSE_SELECTOR)) return false;
    if (target.closest("input, textarea, [contenteditable='true']"))
      return false;
    for (
      let node = target, depth = 0;
      node && node !== document.body && depth < 5;
      node = node.parentElement, depth++
    ) {
      const label = [
        node.getAttribute("aria-label") || "",
        node.getAttribute("title") || "",
      ]
        .join(" ")
        .trim();
      if (label && AI_TRIGGER_RE.test(label)) return true;
      const text = (node.textContent || "").trim();
      if (text.length > 0 && text.length <= 16 && AI_TRIGGER_RE.test(text)) {
        return true;
      }
    }
    return false;
  };

  document.addEventListener(
    "pointerdown",
    (event) => {
      syncRoute();
      if (
        event.target instanceof Element &&
        event.target.closest(CLOSE_SELECTOR)
      ) {
        manualOpen = false;
        return;
      }
      if (isAiTrigger(event.target)) {
        manualOpen = true;
      }
    },
    true,
  );

  const dismissAutoAi = (root = document) => {
    syncRoute();
    if (manualOpen) return false;
    const button = root.matches?.(CLOSE_SELECTOR)
      ? root
      : root.querySelector?.(CLOSE_SELECTOR);
    if (!button || button.dataset.pakeAiClosed === "1") return false;
    button.dataset.pakeAiClosed = "1";
    button.click();
    return true;
  };

  const start = () => {
    dismissAutoAi();
    new MutationObserver((mutations) => {
      syncRoute();
      if (manualOpen) return;
      for (const mutation of mutations) {
        for (const node of mutation.addedNodes) {
          if (node instanceof Element && dismissAutoAi(node)) return;
        }
      }
    }).observe(document.body, { childList: true, subtree: true });
  };

  if (document.body) start();
  else document.addEventListener("DOMContentLoaded", start, { once: true });
})();