Steam Account Switcher

Switch multiple account on Steam

目前为 2020-09-04 提交的版本,查看 最新版本

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