Reddit Auto Translator

Automatically switches to the Japanese translation when opening a Reddit post that supports translation.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name            Reddit Auto Translator
// @name:ja         Reddit自動翻訳
// @namespace       https://greasyfork.org/users/1324207
// @match           https://www.reddit.com/*
// @version         1.0
// @author          Lark8037
// @description     Automatically switches to the Japanese translation when opening a Reddit post that supports translation.
// @description:ja  翻訳対応済みのRedditの投稿を開いた際に自動で日本語翻訳版に切り替えます。
// @run-at          document-start
// @license         MIT
// @icon            https://reddit.com/favicon.ico
// ==/UserScript==

(()=>{
  'use strict';
  let l='';
  setInterval(()=>{
    let h=location.href;
    if(h===l)return;
    l=h;
    let u=new URL(h),p=u.pathname;
    if(!/^\/r\/[^/]+\/comments\//.test(p))return;
    let k='r_tl_'+p,s=sessionStorage,st=s.getItem(k),tl=u.searchParams.get('tl');
    if(tl=='ja'){
      if(+st>0&&Date.now()-st>10000)s.removeItem(k);
    }else if(st!='failed'){
      if(+st>0&&Date.now()-st<10000){
        s.setItem(k,'failed');
      }else{
        s.setItem(k,Date.now());
        u.searchParams.set('tl','ja');
        location.replace(u);
      }
    }
  },200);
})();