// ==UserScript==
// @name Unlock region restrictions
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Bypass some kind of region restrictions.
// @author CharRun
// @grant GM_xmlhttpRequest
// @include https://store.steampowered.com/app/*
// @include https://store.steampowered.com/agecheck/app/*
// @run-at document-body
// ==/UserScript==
(()=>{
let DEFAULT_COUNTRY = '美国';
let messageBox;
const SupportCountryList = {
"中国":"cn",
"美国":"us",
"加拿大":"ca",
"俄罗斯":"ru",
"英国":"gb",
"澳大利亚":"au",
"法国":"fr",
"香港":"hk",
"印度":"in",
"日本":"jp",
"韩国":"kr"
};
function controlBar(){
DEFAULT_COUNTRY = localStorage.getItem('CR_DEFAULT_COUNTRY') || DEFAULT_COUNTRY;
let postion = document.getElementById('global_action_menu');
let menu = document.createElement('div');
let style = document.createElement('style');
style.innerHTML = `#cr-control-lable{position:relative;background-color:#171a21;color:#b8b6b4;width:150px;border:2px solid gray;border-radius:5px;}#cr-control-lable ul{margin:15px 0;padding:10px 0;box-sizing:border-box;border:1px solid #171a21;border-radius:5px;max-height:300px;min-width:100px;width:auto;overflow:auto;display:none;position:absolute;background-color:#171a21}#cr-control-lable li{padding:0 15px;list-style:none;}#cr-control-lable .icon{position:absolute;width:7.5px;height:7.5px;box-sizing:border-box;border:7.5px solid;top:5px;left:15px;z-index:1;}#cr-control-lable .icon.on{border-color:#fff transparent transparent;}#cr-control-lable .icon.close{border-color:transparent transparent transparent #b8b6b4;}#cr-control-lable #cr-input{width:100%;text-align:center;padding-left:20px;color:#b8b6b4;background-color:#171a21;box-sizing:border-box;border-color:transparent;}#cr-control-lable #cr-input::placeholder{color:#b8b6b4;}#cr-control-lable #cr-input:focus::placeholder{color:#fff;}#cr-control-lable #cr-input:focus{outline:none;color:#fff;}#cr-control-lable button{outline:none;color:#b8b6b4;background-color:#171a21;border:none;text-align:center;box-sizing:border-box;padding:5 auto;}#cr-control-lable button:hover:active{color:#b8b6b4;}#cr-control-lable button:hover{color:#fff;cursor:pointer;}#cr-control-lable li:hover{color:#fff;cursor:pointer;}`;
// menu.style.display = "inline-block"
document.getElementsByTagName('head')[0].appendChild(style)
menu.innerHTML = `<div id='cr-control-lable'><div class="infobox"><div style="position: relative;"><span id="cr-icon"class="icon close"></span><input id="cr-input"type="text"name=""placeholder="输入/选择国家"autocomplete="off"></div><div style="text-align: center;"><button id="cr-check"style="width:32%;">访问</button><button id="cr-default"style="width:64%;">设置默认</button></div><ul id="cr-select-country"></ul></div></div>`
postion.appendChild(menu)
let input = document.getElementById("cr-input");
let ul = document.getElementById('cr-select-country');
let check = document.getElementById('cr-check');
let setDefault = document.getElementById('cr-default');
input.value = DEFAULT_COUNTRY
for(let i in SupportCountryList){
ul.innerHTML += `<li>${i}</li>`
}
ul.addEventListener('mousedown',function(e){
input.value = e.target.innerHTML
e.preventDefault();
},true);
input.onfocus = function(){
document.getElementById('cr-icon').className = "icon on"
document.getElementById('cr-select-country').style.display = "block"
}
input.onblur = function(){
document.getElementById('cr-icon').className = "icon close"
document.getElementById('cr-select-country').style.display = "none"
}
input.onkeypress = function(e){
if (e.keyCode == 13 || e.which == 13){
check.onclick()
}
}
check.onclick = function(){
region = input.value == '' ? DEFAULT_COUNTRY : input.value
messageBox = ShowBlockingWaitDialog(`正在跨区访问`, `${region} 区域匿名数据获取中...`)
anonymousRegion(region);
}
setDefault.onclick = function(){
DEFAULT_COUNTRY = input.value;
localStorage.setItem('CR_DEFAULT_COUNTRY', DEFAULT_COUNTRY);
}
}
function allowR18(){
let date = new Date()
date.setTime(date.getTime() + 1000 * 3600 * 24 * 365)
let expires = `expires=${date.toUTCString()};`
let path = `path=/;`
document.cookie= "wants_mature_content=1;" + path + expires;
document.cookie= "birthtime=-729000000;" + path + expires;
messageBox && messageBox.Dismiss()
location = location.href.replace('/agecheck','')
}
function cookieParse(){
let cookieString = document.cookie;
let cookieArray = cookieString.split(';')
let cookies = {};
for(let i = 0 , l = cookieArray.length; i < l ; i++){
let cookie = cookieArray[i].trim();
if(cookie == ''){
continue;
}else{
let c = cookie.split('=')
cookies[c[0]] = c[1]
}
}
return cookies
}
function anonymousRegion(region) {
region = region ? SupportCountryList[region] ? SupportCountryList[region] : region : 'us';
accountCountry = g_oSuggestParams ? g_oSuggestParams.cc.toLowerCase() : '';
if(accountCountry == region){
messageBox && messageBox.Dismiss()
ShowAlertDialog('访问取消','访问区域与当前区域一致','确认').done(setTimeout(()=>location.reload(),1500))
return
}
let url = location.origin + location.pathname + `?cc=${region}`;
GM_xmlhttpRequest({
method:'GET',
url:url,
headers:{
},
anonymous:true,
onload: ifarme
})
}
function ifarme(res){
let virtualDom = document.createElement('html');
virtualDom.innerHTML = res.response;
if(/"error_box"/ig.exec(res.response)){
messageBox && messageBox.Dismiss()
ShowAlertDialog('访问失败','暂不支持R18跨区访问或访问区域受限','确认').done();
return
}
virtualDom.getElementsByTagName('head')[0].innerHTML += '<base target="_blank">'
virtualDom.getElementsByTagName('body')[0].innerHTML = virtualDom.getElementsByClassName('responsive_page_template_content')[0].innerHTML;
let content = `<html class="responsive">${virtualDom.innerHTML}</html>`
let iframe = document.createElement("iframe");
let rhtml = document.getElementsByClassName('responsive_page_template_content')[0];
rhtml.innerHTML = ''
rhtml.appendChild(iframe)
iframe.style.width = '100%';
iframe.style.border = 'none';
iframe.style.height = '0px';
iframe.style.overflow = 'hidden';
iframe.scrolling = 'no';
iframe.frameborder = 'no';
let frameWin = iframe;
if(iframe.contentWindow){
frameWin = iframe.contentWindow;
}
frameWin.document.open();
frameWin.document.writeln(content);
frameWin.document.close();
virtualDom = content = null
frameWin.onload = function(){
messageBox && messageBox.Dismiss()
iframe.style.height = frameWin.document.documentElement.scrollHeight + 'px';
t = setInterval(()=>{
iframe.style.height = frameWin.document.documentElement.scrollHeight + 'px';
},500)
setTimeout(()=>clearInterval(t),3000)
}
}
function autoCheck(){
let restricted = document.getElementById('error_box')
if(restricted){
let cookies = cookieParse();
let mature_content = cookies.wants_mature_content ? cookies.wants_mature_content : '0';
messageBox && messageBox.Dismiss()
if(mature_content == '1'){
messageBox = ShowBlockingWaitDialog('检测到访问限制', `${DEFAULT_COUNTRY} 区域匿名数据获取中...`)
anonymousRegion(DEFAULT_COUNTRY)
}else{
messageBox = ShowBlockingWaitDialog('R18解除尝试', '设置Cookies中...')
allowR18()
}
}
}
(function main(){
autoCheck()
controlBar()
})()
// function insert(res){
// let rhtml = document.getElementsByClassName('responsive_page_template_content')[0];
// let resContainer = document.createElement('html')
// resContainer.innerHTML = /<head>([\s\S])*<\/body>/ig.exec(res.response);
// let nhtml = resContainer.getElementsByClassName('responsive_page_template_content')[0];
// const MISS_CSS_LIST = [
// 'https://steamstore-a.akamaihd.net/public/shared/css/user_reviews.css?v=fRl7Zc8pKTl-',
// 'https://steamstore-a.akamaihd.net/public/shared/css/store_game_shared.css?v=ZIBBVLz7y4BI',
// 'https://steamstore-a.akamaihd.net/public/css/v6/game.css?v=WxFCdr52XB55',
// 'https://steamstore-a.akamaihd.net/public/css/v6/recommended.css?v=AOAvIZ99nrgu',
// 'https://steamstore-a.akamaihd.net/public/css/applications/store/main.css?v=OxxrwLUZ2E2x',
// 'https://steamstore-a.akamaihd.net/public/css/applications/store/store.css?v=zJBnwu5HDcJI',
// 'https://steamstore-a.akamaihd.net/public/shared/css/apphub.css?v=dunRbO3rtVVd'
// ]
// cssInject()
// if(rhtml&&nhtml){
// rhtml.innerHTML = nhtml.innerHTML;
// let scriptTags = resContainer.getElementsByTagName('script');
// scriptInject(scriptTags)
// }
// }
// function cssInject(){
// for(let i = 0, l = MISS_CSS_LIST.length;i < l;i++){
// let newStyle = document.createElement('link');
// newStyle.href = MISS_CSS_LIST[i];
// newStyle.type = "text/css"
// newStyle.rel = "stylesheet"
// document.head.appendChild(newStyle);
// }
// }
// function scriptInject(scriptNode){
// setTimeout(()=>{
// for(let i = 0, l = scriptNode.length; i < l; i++){
// try{
// let newScript = document.createElement('script');
// newScript.setAttribute('index',i)
// newScript.innerHTML = scriptNode[i].innerHTML;
// let src = scriptNode[i].getAttribute('src');
// src && newScript.setAttribute('src', src);
// document.head.appendChild(newScript);
// }
// catch(e){console.log(e)}
// }
// },1000)
// }
})()