Reddit Auto Translator

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

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

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.

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

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

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            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);
})();