alright2

Hello2

  1. // ==UserScript==
  2. // @name alright2
  3. // @version 1.1
  4. // @description Hello2
  5. // @namespace starve.io
  6. // @author noone
  7. // @match https://starve.io/
  8. // @run-at document-start
  9. // ==/UserScript==
  10.  
  11. function inject() {
  12. var $ = window.$;
  13. var client = window.client;
  14.  
  15. var modeSelected = false;
  16. function setDefault(key, value) {
  17. if (!Object.prototype.hasOwnProperty.call(localStorage, key)) {
  18. localStorage.setItem(key, value);
  19. }
  20. }
  21.  
  22. function get(key) {
  23. return JSON.parse(localStorage.getItem(key));
  24. }
  25.  
  26. setDefault("servers", JSON.stringify(["starve.sixserver.pl:1000"]));
  27. setDefault("debug.hide", JSON.stringify([0]));
  28. setDefault("debug.log.send", false);
  29. setDefault("debug.log.receive", false);
  30.  
  31. var ws = window.WebSocket;
  32. window.WebSocket = function () {
  33. var that = new ws(...arguments);
  34. that.addEventListener("open", function () {
  35. console.log("connected: " + that.url);
  36. });
  37. that.addEventListener("close", function () {
  38. console.log("disconnected");
  39. });
  40. that.addEventListener("message", function (event) {
  41. var data = event.data;
  42. if (data instanceof ArrayBuffer) {
  43. data = new Uint8Array(data);
  44. if (get("debug.hide").includes(data[0])) {
  45. return;
  46. }
  47. }
  48. if (get("debug.log.receive")) {
  49. console.log("received: " + data);
  50. }
  51. });
  52. return that;
  53. };
  54. window.WebSocket.prototype = ws.prototype;
  55.  
  56. WebSocket.prototype.send = function (send) {
  57. return function (data) {
  58. var parsed = JSON.parse(data);
  59. if (parsed instanceof Array) {
  60. if (typeof parsed[0] == "string") {
  61. if (modeSelected) {
  62. parsed.push($("#password").val());
  63. }
  64. }
  65. data = JSON.stringify(parsed)
  66. }
  67. if (get("debug.log.send")) {
  68. console.log("sending: " + data);
  69. }
  70.  
  71. return send.apply(this, arguments);
  72. };
  73. }(WebSocket.prototype.send);
  74.  
  75. var serverSelect = document.getElementById('servselect');
  76. var serverSelectVisible = false;
  77.  
  78. var initialTops = {};
  79. var initialLefts = {};
  80. function updateCss() {
  81. serverSelectVisible = serverSelect.style.left != "" && serverSelect.style.display != "none";
  82. $(".ps-panel").css("display", function () {
  83. return modeSelected && serverSelectVisible ? "" : "none";
  84. });
  85. $("#mode-button").css("display", serverSelectVisible ? "" : "none");
  86.  
  87. function pxToNumber(px) {
  88. return Number.parseFloat(px.replace("px"));
  89. }
  90.  
  91. function watchStyle(styleName, dict) {
  92. $(".ps").css(styleName, function () {
  93. var key = this.id || this;
  94. if (isNaN(dict[key]))
  95. dict[key] = pxToNumber(this.style[styleName]);
  96. return pxToNumber(serverSelect.style[styleName]) + dict[key];
  97. });
  98. }
  99.  
  100. $(".clicky:active").css("top", function () {
  101. return pxToNumber(this.style.top) + 10;
  102. });
  103.  
  104. watchStyle("top", initialTops);
  105. watchStyle("left", initialLefts);
  106. modeSelected ? $("#mode-button").addClass("active") : $("#mode-button").removeClass("active");
  107. }
  108.  
  109. var observer = new MutationObserver(function (mutations) {
  110. mutations.forEach(function (mutationRecord) {
  111. updateCss();
  112. $("#trevda").hide();
  113. });
  114. });
  115.  
  116. observer.observe(serverSelect, {
  117. attributes: true,
  118. attributeFilter: ['style']
  119. });
  120.  
  121. var oldClient = client;
  122. window[oldClient.keys.client] = new Proxy(oldClient, {
  123. set: function (target, key, value) {
  124. oldClient[key] = value;
  125.  
  126. if (key === client.keys.selectedMode || key === "selectedMode") {
  127. modeSelected = value == 6;
  128. updateCss();
  129. }
  130. return true;
  131. }
  132. });
  133.  
  134. $("body").append(/*html*/`
  135. <img id="mode-button" class="ps-cursor ps" style="display: none; left: -160px; top: 75px;">
  136. <div class="ps-panel" style="display: none;">
  137. <input id="ip" type="text" placeholder="ip/host" class="md-select ps-input ps" style="left: -5px; top: 54px;">
  138. <input id="password" type="password" placeholder="password" class="md-select ps-input ps" style="left: -5px; top: 108px;">
  139. <button id="add-button" class="ps-button ps ps-cursor clicky" style="left: 254px; top: 65px;">Add</button>
  140. </div>
  141. `);
  142.  
  143. function addServer(ip) {
  144. $.ajaxSetup({ timeout: 1500 });
  145. $.get("https://" + ip + "/info").done(function (data) {
  146. data.info = ip;
  147. setMode(6, data)
  148. }).fail(function () {
  149. var data = { a: ip, nu: "offline" }
  150. data.info = ip;
  151. data.offline = true;
  152. setMode(6, data)
  153. });
  154. }
  155.  
  156. for (const server of get("servers")) {
  157. addServer(server);
  158. }
  159.  
  160. setTimeout(function() {
  161. setMode(0);
  162. }, 2000);
  163.  
  164. function setMode(i = 6, data) {
  165. client.serversList[i] = client.serversList[i] || [{}];
  166. if (client.serversList[i].length <= 0)
  167. client.serversList[i].push({});
  168. client.selectedServer[i] = client.selectedServer[i] || 0;
  169. if (data) {
  170. if (client.serversList[i][0].info == undefined)
  171. client.serversList[i] = [];
  172.  
  173. var server = client.serversList[i].find(x => x.info == data.info);
  174. if (server) {
  175. client.serversList[i][client.serversList[i].indexOf(server)] = data;
  176. } else {
  177. client.serversList[i].push(data);
  178. }
  179. }
  180. if (client.selectMode)
  181. client.selectMode(i);
  182. $("#ul-id.md-whiteframe-z1 li:first-child").text("Choose a server");
  183. $("#ul-id.md-whiteframe-z1 li:last-child").hide();
  184.  
  185. var servers = $("#servselect #ul-id.md-whiteframe-z1 li:not(#selectDisabled)");
  186. if (JSON.stringify(client.serversList[i]) == "[{}]") {
  187. var msg = "Not added any servers";
  188. $("#servselect button.ng-binding").html(msg);
  189. servers.html(msg);
  190. } else {
  191. var index = 0;
  192. servers.html(function () {
  193. var data = client.serversList[i][index];
  194. var text = $(this).html();
  195.  
  196. if (data && data.nu === "offline") {
  197. text = data.info + " [offline]"
  198. this.id = "selectDisabled";
  199. }
  200.  
  201. if ($(this).hasClass("active")) {
  202. $("#servselect button.ng-binding").html(text);
  203. }
  204.  
  205. var html = text + /*html*/`
  206. <div class="server-buttons" data-index="${index}">
  207. <button class="refresh-button">⟳</button>
  208. <button class="delete-button">×</button>
  209. </div>
  210. `;
  211. index++;
  212. return html;
  213. });
  214.  
  215. if ($("#servselect #ul-id.md-whiteframe-z1 li").toArray().every(x => x.id == "selectDisabled")) {
  216. $("#servselect button.ng-binding").html("All servers are offline ;(");
  217. }
  218.  
  219. $(".refresh-button").click(function () {
  220. addServer(client.serversList[i][$(this).parent().data("index")].info);
  221. setMode();
  222. });
  223.  
  224. $(".delete-button").click(function () {
  225. client.serversList[i].splice($(this).parent().data("index"), 1)
  226. client.selectedServer[i]--;
  227. setMode();
  228. });
  229.  
  230. servers.filter(":not(#selectDisabled)").click(function () {
  231. client.selectedServer[i] = $(this).find(".server-buttons").data("index");
  232. setMode();
  233. });
  234. }
  235. }
  236.  
  237. $("#ip").val(localStorage.getItem('ip'));
  238. $("#add-button").click(function () {
  239. var ip = $("#ip").val();
  240. localStorage.setItem('ip', ip);
  241. addServer(ip);
  242. });
  243.  
  244. $("#mode-button").click(function () {
  245. setMode()
  246. });
  247. }
  248.  
  249. var link = document.createElement( "link" );
  250. link.href = "https://gitcdn.link/repo/privatestarving/privatestarving.script/master/privatestarving.css"
  251. link.type = "text/css";
  252. link.rel = "stylesheet";
  253.  
  254. document.head.appendChild(link);
  255.  
  256. var script = document.createElement('script');
  257. script.appendChild(document.createTextNode('(' + inject + ')();'));
  258.  
  259. function checkForClient() {
  260. if (unsafeWindow.client == undefined) {
  261. setTimeout(checkForClient, 50);
  262. } else {
  263. (document.body || document.head || document.documentElement).appendChild(script);
  264. }
  265. }
  266. checkForClient();