CF - keep mobile and locale unchanged

Prevent accidentally modifying the mobile or locale on Codeforces

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         CF - keep mobile and locale unchanged
// @version      0.0.0
// @description  Prevent accidentally modifying the mobile or locale on Codeforces
// @match        *://codeforces.com
// @match        *://codeforces.com/*
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_deleteValue
// @run-at       document-start
// @namespace    https://greasyfork.org/users/410786
// ==/UserScript==

(function(){
	let url=new URL(location.href)
	let changed=false
	for(let param of ['mobile','locale']){
		const expected=GM_getValue('lastParam_'+param)
		if(expected!==undefined&&
			url.searchParams.has(param)&&
			url.searchParams.get(param)!==expected
		){
			changed=true
			url.searchParams.set(param,expected)
		}
	}
	if(changed){
		location.href=url.toString()
		return
	}

	window.addEventListener('DOMContentLoaded',function(){
		document.querySelectorAll('a[href^="?locale="],a[href^="?mobile="]').forEach(function(elem){
			elem.addEventListener('click',function(){
				for(let param of ['mobile','locale'])
					GM_deleteValue('lastParam_'+param)
			})
		})

		GM_setValue('lastParam_locale',document.documentElement.lang)
		GM_setValue('lastParam_mobile',document.getElementsByClassName('switchToMobile').length ? "false" : "true")
	})
})()