Steam Account Switcher

Switch multiple account on Steam and community

  1. // ==UserScript==
  2. // @name Steam Account Switcher
  3. // @version 0.3
  4. // @description Switch multiple account on Steam and community
  5. // @author lzt
  6. // @match *://store.steampowered.com/*
  7. // @match *://*.steamcommunity.com/*
  8. // @grant GM_setValue
  9. // @grant GM_getValue
  10. // @grant GM_listValues
  11. // @grant GM_deleteValue
  12. // @grant GM_cookie
  13. // @grant unsafeWindow
  14. // @grant window
  15. // @namespace steam_account_switcher
  16. // ==/UserScript==
  17. (function() {
  18. 'use strict';
  19. // Your code here...
  20. let account = document.evaluate("//a[contains(@href, 'javascript:Logout()')]", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  21. if(account.snapshotLength != 1) {
  22. console.log("account error");
  23. unsafeWindow.account = null;
  24. }else{
  25. unsafeWindow.account = account.snapshotItem(0).children[0].innerText;
  26. };
  27. init();
  28.  
  29. let top = document.getElementById("global_action_menu");
  30. let total = document.createElement("div");
  31. let enter = document.createElement("span");
  32. let menu = document.createElement("div");;
  33.  
  34. total.id = "switcher_total";
  35. total.style.display = "inline-block";
  36. enter.id = "switcher_pulldown";
  37. enter.className = "pulldown global_action_link";
  38. enter.innerText = "切换账号";
  39. enter.addEventListener("click", function(e){e.stopPropagation();reloadmenu()});
  40. total.appendChild(enter);
  41. top.insertBefore(total, top.firstElementChild);
  42.  
  43. document.addEventListener("click", function(e){
  44. if (!menu.contains(e.target)) menu.style.display = "none";
  45. });
  46.  
  47. function init() {
  48. //add label
  49. if (!GM_getValue("community")) {
  50. GM_setValue("community", "{}");
  51. }
  52. if (!GM_getValue("store")) {
  53. GM_setValue("store", "{}");
  54. }
  55. //upgrade old data from v0.2 to v0.3
  56. let list = GM_listValues();
  57. let data = JSON.parse(GM_getValue("store"));
  58. for (let i = 0; i < list.length; i++) {
  59. if (list[i] != "community" && list[i] != "store") {
  60. data[list[i]] = JSON.parse(GM_getValue(list[i]));
  61. console.log("add ", list[i], " from old data of v0.2");
  62. GM_deleteValue(list[i]);
  63. }
  64. }
  65. GM_setValue("store", JSON.stringify(data));
  66. }
  67.  
  68. function fillmenu() {
  69. menu = menu ? document.createElement("div") : menu;
  70. menu.className = "popup_block_new account_switcher";
  71. menu.id = "sw_popup";
  72. menu.style.visibility = "visible";
  73. menu.style.display = "block";
  74. menu.style.top = enter.getBoundingClientRect().bottom;
  75. menu.style.left = enter.getBoundingClientRect().left;
  76.  
  77. let list, loginURL;
  78. if (window.location.href.search("steamcommunity.com") > -1) {
  79. list = JSON.parse(GM_getValue("community"));
  80. loginURL = "https://steamcommunity.com/login/";
  81. } else if (window.location.href.search("steampowered.com") > -1) {
  82. list = JSON.parse(GM_getValue("store"));
  83. loginURL = "https://store.steampowered.com/login/";
  84. }
  85.  
  86. var context = document.createElement("div");
  87. context.className = "popup_body popup_menu account_switcher";
  88. if (unsafeWindow.account != null & list[unsafeWindow.account] === undefined) {
  89. let add = document.createElement("a");
  90. add.className = "popup_menu_item account_switcher";
  91. add.innerText = "添加 " + unsafeWindow.account;
  92. add.setAttribute("href", "#");
  93. add.addEventListener("click", function(e){e.stopPropagation();addaccount()});
  94. context.appendChild(add);
  95. };
  96.  
  97. for (let id in list) {
  98. let entity = document.createElement("div");
  99. entity.className = "popup_menu_item account_switcher";
  100.  
  101. let sw = document.createElement("a");
  102. sw.setAttribute("href", "#");
  103. sw.style.margin = "0px 10px 0px 0px"
  104. if (unsafeWindow.account == id) {
  105. sw.innerText = "更新 " + id;
  106. sw.addEventListener("click", function (e) { e.stopPropagation(); addaccount(); });
  107. } else {
  108. sw.innerText = "转到 " + id;
  109. sw.addEventListener("click", function (e) { e.stopPropagation(); swaccount(id) });
  110. };
  111.  
  112. let del = document.createElement("a");
  113. del.innerText = "删除";
  114. del.setAttribute("href", "#");
  115. del.addEventListener("click", function (e) { e.stopPropagation(); delaccount(id) });
  116.  
  117. entity.appendChild(sw);
  118. entity.appendChild(del);
  119. context.appendChild(entity);
  120. }
  121.  
  122. let login = document.createElement("a");
  123. login.className = "popup_menu_item account_switcher";
  124. login.innerText = "添加新账号";
  125. login.setAttribute("href", "#");
  126. login.addEventListener("click", function(e){
  127. e.stopPropagation();
  128. let lock = 0;
  129. GM_cookie("list", { path: "/" }, function(cookies) {
  130. if (cookies) {
  131. for(let i = 0; i < cookies.length; i++){
  132. GM_cookie("delete", {name: cookies[i]["name"]}, function(error) {
  133. console.log(error || "del " + cookies[i]["name"]);
  134. lock++;
  135. if (lock >= cookies.length) window.location.href = loginURL;
  136. });
  137. }
  138. } else {
  139. window.location.href = loginURL;
  140. }
  141. });
  142. });
  143. context.appendChild(login);
  144. menu.appendChild(context);
  145. total.appendChild(menu);
  146. };
  147.  
  148. function reloadmenu() {
  149. let l = document.getElementsByClassName("account_switcher")
  150. for(let i = l.length - 1; i >= 0; i--){
  151. l[i].remove()
  152. }
  153. fillmenu()
  154. };
  155.  
  156. function addaccount() {
  157. console.log("add " + unsafeWindow.account);
  158. GM_cookie("list", { path: "/" }, function(cookies) {
  159. let c = []
  160. for(let i = 0; i < cookies.length; i++){
  161. if (cookies[i]["name"] == "browserid") c.push(cookies[i]);
  162. if (cookies[i]["name"] == "sessionid") c.push(cookies[i]);
  163. if (cookies[i]["name"] == "steamLoginSecure") c.push(cookies[i]);
  164. if (cookies[i]["name"] == "steamRememberLogin") c.push(cookies[i]);
  165. if (cookies[i]["name"] == "bGameHighlightAutoplayDisabled") c.push(cookies[i]);
  166. if (cookies[i]["name"] == "lastagecheckage") c.push(cookies[i]);
  167. if (cookies[i]["name"] == "mature_content") c.push(cookies[i]);
  168. if (cookies[i]["name"] == "wants_mature_content") c.push(cookies[i]);
  169. if (cookies[i]["name"] == "birthtime") c.push(cookies[i]);
  170. if (cookies[i]["name"].search("steamMachineAuth") != -1) c.push(cookies[i]);
  171. }
  172. let list;
  173. if (window.location.href.search("steamcommunity.com") > -1) {
  174. list = JSON.parse(GM_getValue("community"));
  175. list[unsafeWindow.account] = c;
  176. GM_setValue("community", JSON.stringify(list));
  177. } else if (window.location.href.search("steampowered.com") > -1) {
  178. list = JSON.parse(GM_getValue("store"));
  179. list[unsafeWindow.account] = c;
  180. GM_setValue("store", JSON.stringify(list));
  181. }
  182. reloadmenu();
  183. });
  184. };
  185.  
  186. function delaccount(id) {
  187. console.log("delete " + id);
  188. let list;
  189. if (window.location.href.search("steamcommunity.com") > -1) {
  190. list = JSON.parse(GM_getValue("community"));
  191. delete list[id];
  192. GM_setValue("community", JSON.stringify(list));
  193. } else if (window.location.href.search("steampowered.com") > -1) {
  194. list = JSON.parse(GM_getValue("store"));
  195. delete list[id];
  196. GM_setValue("store", JSON.stringify(list));
  197. }
  198. reloadmenu()
  199. };
  200.  
  201. function swaccount(id) {
  202. console.log("switch to " + id);
  203. let cookies, list;
  204. if (window.location.href.search("steamcommunity.com") > -1) {
  205. list = JSON.parse(GM_getValue("community"));
  206. cookies = list[id];
  207. } else if (window.location.href.search("steampowered.com") > -1) {
  208. list = JSON.parse(GM_getValue("store"));
  209. cookies = list[id];
  210. }
  211. let delock = 0;
  212. GM_cookie("list", { path: "/" }, function(c) {
  213. for(let i = 0; i < c.length; i++){
  214. GM_cookie("delete", {name: c[i]["name"]}, function(error) {
  215. console.log(error || "del " + c[i]["name"]);
  216. delock++;
  217. if (delock >= c.length) {
  218. console.log("del complete")
  219. let addlock = 0;
  220. for(let i = 0; i < cookies.length; i++){
  221. GM_cookie("set", {
  222. name: cookies[i]['name'],
  223. value: cookies[i]['value'],
  224. domain: cookies[i]['domain'],
  225. path: cookies[i]['path'],
  226. secure: cookies[i]['secure'],
  227. httpOnly: cookies[i]['httpOnly'],
  228. sameSite: cookies[i]['sameSite'],
  229. expirationDate: cookies[i]['expirationDate'],
  230. hostOnly: cookies[i]['hostOnly']
  231. }, function(error) {
  232. console.log(error || "add " + cookies[i]["name"]);
  233. addlock++;
  234. if (addlock >= cookies.length) {
  235. let url = window.location.href;
  236. if (url.search("store.steampowered.com/wishlist") != -1) {
  237. window.location.href = "https://store.steampowered.com/wishlist"
  238. }else{
  239. window.location.reload()
  240. }
  241. };
  242. });
  243. }
  244. };
  245. });
  246. }
  247. });
  248. };
  249. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址