Steam Account Switcher

Switch multiple account on Steam

目前為 2020-09-04 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Steam Account Switcher
// @version      0.1
// @description  Switch multiple account on Steam
// @author       lzt
// @match        *://store.steampowered.com/*
// @grant 		 GM_setValue
// @grant 		 GM_getValue
// @grant 		 GM_listValues
// @grant 		 GM_deleteValue
// @grant 		 GM_cookie
// @grant		 unsafeWindow
// @grant		 window
// @namespace	 steam_account_switcher
// ==/UserScript==
(function() {
    'use strict';
    // Your code here...
    let account = document.evaluate("//a[contains(@href, 'javascript:Logout()')]", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
    if(account.snapshotLength != 1) {
    	console.log("account error");
    	unsafeWindow.account = null;
    }else{
    	unsafeWindow.account = account.snapshotItem(0).children[0].innerText;
    };

    var top = document.getElementById("global_action_menu");
    var total = document.createElement("div");
    var enter = document.createElement("span");
    var menu = document.createElement("div");;

    total.id = "switcher_total";
    total.style.display = "inline-block";
    enter.id = "switcher_pulldown";
    enter.className = "pulldown global_action_link";
    enter.innerText = "切换账号";
    enter.addEventListener("click", function(e){e.stopPropagation();reloadmenu()});
    total.appendChild(enter);
    top.insertBefore(total, top.firstElementChild);

	document.addEventListener("click", function(e){
		if (!menu.contains(e.target)) menu.style.display = "none";
	});

    function fillmenu() {
    	
    	menu = menu ? document.createElement("div") : menu;
    	menu.className = "popup_block_new account_switcher";
		menu.id = "sw_popup";
		menu.style.visibility = "visible";
		menu.style.display = "block";
		menu.style.top = enter.getBoundingClientRect().bottom;
		menu.style.left = enter.getBoundingClientRect().left;

    	var context = document.createElement("div");
    	context.className = "popup_body popup_menu account_switcher";
    	if (unsafeWindow.account != null & GM_getValue(unsafeWindow.account) == undefined) {
			let add = document.createElement("a");
			add.className = "popup_menu_item account_switcher";
			add.innerText = "添加 " + unsafeWindow.account;
			add.setAttribute("href", "#");
			add.addEventListener("click", function(e){e.stopPropagation();addaccount()});
			context.appendChild(add);
		};

		let list = GM_listValues()
		for (let i = 0; i < list.length; i++) {
			let entity = document.createElement("div");
			entity.className = "popup_menu_item account_switcher";

			let sw = document.createElement("a");
			sw.setAttribute("href", "#");
			sw.style.margin = "0px 10px 0px 0px"
			if (unsafeWindow.account == list[i]) {
				sw.innerText = "更新 " + list[i];
				sw.addEventListener("click", function(e){e.stopPropagation();addaccount();});
			}else{
				sw.innerText = "转到 " + list[i];
				sw.addEventListener("click", function(e){e.stopPropagation();swaccount(list[i])});
			};

			let del = document.createElement("a");
			del.innerText = "删除";
			del.setAttribute("href", "#");
			del.addEventListener("click", function(e){e.stopPropagation();delaccount(list[i])});

			entity.appendChild(sw);
			entity.appendChild(del);
			context.appendChild(entity);
		}

		let login = document.createElement("a");
		login.className = "popup_menu_item account_switcher";
		login.innerText = "添加新账号";
		login.setAttribute("href", "#");
		login.addEventListener("click", function(e){
			e.stopPropagation();
			let lock = 0;
			GM_cookie("list", { path: "/" }, function(cookies) {
				for(let i = 0; i < cookies.length; i++){
	    			GM_cookie("delete", {name: cookies[i]["name"]}, function(error) {
	    				console.log(error || "del " + cookies[i]["name"]);
	    				lock++;
	    				if (lock >= cookies.length) window.location.href = "https://store.steampowered.com/login/";
	    			});
	    		}
			});
		});
		context.appendChild(login);

		menu.appendChild(context);
		total.appendChild(menu);
    };

    function reloadmenu() {
    	let l = document.getElementsByClassName("account_switcher")
    	for(let i = l.length - 1; i >= 0; i--){
    		l[i].remove()
    	}
    	fillmenu()
    };

    function addaccount() {
    	console.log("add " + unsafeWindow.account);
    	GM_cookie("list", { path: "/" }, function(cookies) {
    		GM_setValue(unsafeWindow.account, JSON.stringify(cookies));
    		reloadmenu();
		});
    };

    function delaccount(id) {
    	console.log("delete " + id);
    	GM_deleteValue(id)
    	reloadmenu()
    };

    function swaccount(id) {
    	console.log("switch to " + id);
    	let l = JSON.parse(GM_getValue(id));
    	let delock = 0
		GM_cookie("list", { path: "/" }, function(cookies) {
			for(let i = 0; i < cookies.length; i++){
    			GM_cookie("delete", {name: cookies[i]["name"]}, function(error) {
    				console.log(error || "del " + cookies[i]["name"]);
    				delock++;
    				if (delock >= cookies.length) {
    					console.log("del complete")
    					let addlock = 0;
						for(let i = 0; i < l.length; i++){
							GM_cookie("set", {
								name: l[i]['name'], 
				    			value: l[i]['value'], 
				    			domain: l[i]['domain'], 
				    			path: l[i]['path'], 
				    			secure: l[i]['secure'], 
				    			httpOnly: l[i]['httpOnly'], 
				    			sameSite: l[i]['sameSite'], 
				    			expirationDate: l[i]['expirationDate'], 
				    			hostOnly: l[i]['hostOnly']
							}, function(error) {
								console.log(error || "add " + l[i]["name"]);
								addlock++;
								if (addlock >= l.length) {
									let url = window.location.href;
									if (url.search("store.steampowered.com/wishlist") != -1) {
										window.location.href = "https://store.steampowered.com/wishlist"
									}else{
										window.location.reload()
									}
								};
							});
						}
    				};
    			});
    		}
		});
    };
})();