Reddit Auto Translator

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

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

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