🎉 欢迎访问GreasyFork.Org 镜像站!本镜像站由公众号【爱吃馍】搭建,用于分享脚本。联系邮箱📮

Greasy fork 爱吃馍镜像

Greasy Fork is available in English.

📂 缓存分发状态(共享加速已生效)
🕒 页面同步时间:2026/01/06 02:52:07
🔄 下次更新时间:2026/01/06 03:52:07
手动刷新缓存

Phantom-Render 0-ms Translate (CN)

首帧即中文:抢占 Cookie + 并行脚本 + 幽灵渲染 + 启发式完成检测。

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

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

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

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

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

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

// ==UserScript==
// @name         Phantom-Render 0-ms Translate (CN)
// @namespace    BlackHat.Tech/PhantomRender
// @version      1.0
// @description  首帧即中文:抢占 Cookie + 并行脚本 + 幽灵渲染 + 启发式完成检测。
// @author       ￴
// @match        *://*/*
// @run-at       document-start
// @grant        none
// ==/UserScript==

(() => {
  /* 0  · 若本身含大量中文直接退出 */
  const zh = /[\u4e00-\u9fff]/;
  if ( zh.test(document.title) ||
       zh.test((document.documentElement.innerText||'').slice(0,4096)) ) return;

  /* 1 · 抢占:写 googtrans,欺骗服务器"必须中文" */
  const LANG = '/auto/zh-CN';
  document.cookie          = `googtrans=${LANG};path=/;max-age=63072000`;
  localStorage.googtrans   = LANG;
  sessionStorage.googtrans = LANG;

  /* 2 · 基础占位 + CSS 隐身,避免英文闪烁 */
  document.head.insertAdjacentHTML('afterbegin', `
    <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
    <link rel="dns-prefetch" href="//translate.google.com">
    <link rel="preconnect"  href="https://translate.google.com" crossorigin>
    <link rel="dns-prefetch" href="//translate.googleapis.com">
    <link rel="preconnect"  href="https://translate.googleapis.com" crossorigin>
    <style id="phantom-css">
      body{visibility:hidden!important;opacity:0!important;
           transition:opacity .3s;top:0!important}
      body.phantom-show{visibility:visible!important;opacity:1!important}
      .skiptranslate,#google_translate_element,
      .goog-te-banner-frame,.goog-te-balloon-frame,
      .goog-logo-link,.goog-te-gadget span{display:none!important}
      
      /* 保留 Markdown 链接原始颜色 */
      a font[color] {
        color: inherit !important;
      }
      .markdown-body a font,
      .markdown a font,
      [class*="markdown"] a font,
      article a font,
      .content a font,
      .post a font,
      .article-content a font,
      main a font {
        color: inherit !important;
      }
      a {
        color: revert !important;
      }
      .markdown-body a,
      .markdown a,
      [class*="markdown"] a,
      article a,
      .content a,
      .post a,
      .article-content a,
      main a {
        color: revert !important;
      }
    </style>
  `);
  const phDiv = document.createElement('div');
  phDiv.id = 'google_translate_element';
  phDiv.style.display='none';
  document.documentElement.appendChild(phDiv);

  /* 3 · Google 回调 */
  window.__phInit = () => new google.translate.TranslateElement({
      pageLanguage:'auto',
      includedLanguages:'zh-CN',
      autoDisplay:true,
      layout:google.translate.TranslateElement.InlineLayout.SIMPLE
  }, 'google_translate_element');

  /* 4 · 并行拉脚本(带缓存)*/
  const SRC = 'https://translate.google.com/translate_a/element.js?cb=__phInit';
  const sc  = document.createElement('script');
  sc.src   = SRC;
  sc.defer = sc.async = true;
  document.head.appendChild(sc);

  /* 5 · 启发式"翻译完成"检测 + Reveal */
  const reveal = () => {
    document.body.classList.add('phantom-show');
    addRestoreBtn();
    fixLinkColors();
    obs && obs.disconnect();
  };
  let timer, obs;
  const waitBody = new MutationObserver((m, o)=>{
    if(!document.body) return;
    o.disconnect();
    obs = new MutationObserver(()=>{
      clearTimeout(timer);
      timer = setTimeout(reveal, 250);   // 250 ms 静默:判定完成
    });
    obs.observe(document.body,{childList:true,subtree:true,characterData:true});
    setTimeout(reveal, 5000);            // 5 s 保险
  });
  waitBody.observe(document.documentElement,{childList:true,subtree:true});

  /* 修复链接颜色 */
  function fixLinkColors() {
    // 移除 Google 翻译添加的 font 标签的 color 属性
    const fonts = document.querySelectorAll('a font[color]');
    fonts.forEach(font => {
      font.removeAttribute('color');
    });
  }

  /* 6 · 恢复原文按钮 */
  function addRestoreBtn(){
    const btn = document.createElement('div');
    btn.textContent='原文';
    Object.assign(btn.style,{
      position:'fixed',right:'20px',bottom:'28px',padding:'6px 14px',
      background:'#ff4d4f',color:'#fff',borderRadius:'6px',
      fontSize:'13px',fontFamily:'sans-serif',cursor:'pointer',
      zIndex:'2147483647',userSelect:'none',opacity:0.6,
      boxShadow:'0 2px 6px rgba(0,0,0,.25)',transition:'opacity .2s'
    });
    btn.onmouseenter=()=>btn.style.opacity=1;
    btn.onmouseleave=()=>btn.style.opacity=0.6;
    btn.onclick=()=>{
      /* 清 cookie → 刷新即回英文;再次点击可再翻译 */
      const exp='Thu, 01 Jan 1970 00:00:00 GMT';
      document.cookie=`googtrans=;expires=${exp};path=/;`;
      localStorage.removeItem('googtrans');
      sessionStorage.removeItem('googtrans');
      location.reload();
    };
    document.body.appendChild(btn);
  }
})();