Automatically switches to the Japanese translation when opening a Reddit post that supports translation.
// ==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);
})();