Discord Status Animator (Manual edit/Non-UI)

Automatically changes your Discord status

当前为 2021-06-14 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Discord Status Animator (Manual edit/Non-UI)
  3. // @namespace https://github.com/Hakorr/status-animator
  4. // @run-at document-start
  5. // @version 1.0
  6. // @description Automatically changes your Discord status
  7. // @author HKR
  8. // @match https://discord.com/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. //Welcome! Don't be scared by the code, I was too lazy to do an UI for this.
  14. //All you have to edit is the statusanimation function's code (Around the line 40)
  15. var name = "Status Animator";
  16. var version = "V1.0";
  17. var run = true;
  18.  
  19. //A Cookie will be made with this name, feel free to edit it
  20. var cookie_name = "StatusToken";
  21. var delete_cookie_after_a_week = true;
  22. //Your status will be changed to these after you close the Discord tab
  23. var default_status_text = "";
  24. var default_status_emoji = "";
  25. //Animation blocks////////////////
  26. /*
  27. 1. status(emoji,text);
  28. 2. await delay(ms);
  29. */async function statusanimation() {
  30. ////////////////////////////////////
  31. status("👐","This");
  32. await delay(500);
  33. status("👀","Is");
  34. await delay(500);
  35. status("😶","A");
  36. await delay(500);
  37. status("✨","Test");
  38. await delay(500);
  39. status("","");
  40. await delay(2000);
  41. /////////////////////////////
  42. if (run) statusanimation(); }
  43. //Do not edit after this line (If you don't know what you're doing)
  44. ///////////////////////////////////////////////////////////////////
  45. //Function to read the saved cookie
  46. window.getCookie = function(name) {
  47. var match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)'));
  48. if (match) return match[2];
  49. }
  50. //Set the Discord Token as a Cookie for future use of the script
  51. if(document.cookie.indexOf(cookie_name + "=") == -1) {
  52. if(confirm("\"" + cookie_name + "\" cookie not found. Refreshing Discord to get it.\n\n- " + name + version)) {
  53. location.reload();
  54. var i = document.createElement('iframe');
  55. document.body.appendChild(i);
  56. //Get Token from localStorage
  57. var token = i.contentWindow.localStorage.token
  58. token = token.slice(1, -1);
  59. if(delete_cookie_after_a_week) {
  60. //Save the encrypted Token to a Cookie - The cookie will be deleted after a week
  61. document.cookie = cookie_name + "=" + token + "; secure=true; max-age=604800; path=/";
  62. } else {
  63. document.cookie = cookie_name + "=" + token + "; secure=true; path=/";
  64. }
  65. } else { throw new Error("[Not an actually uncaught] User stopped the Status Animator. \n\nNo cookie was found and user decided not to continue."); }
  66. }
  67. var status_text = "";
  68. var status_emoji = "";
  69. //Function that changes the status variables (Saves up a bit space)
  70. function status(emoji,text) {
  71. if(run) {
  72. status_text = text;
  73. status_emoji = emoji;
  74. setstatus();
  75. }
  76. }
  77. //Get Discord Token from saved Cookie
  78. var token = getCookie(cookie_name);
  79. //HTTP Request's URL address
  80. var url = "https://discord.com/api/v9/users/@me/settings";
  81. //Function that handles the HTTP request for the status change
  82. function setstatus() {
  83.  
  84. //Set up a HTTP request to change the status
  85. var request = new XMLHttpRequest();
  86. request.open("PATCH", url);
  87. request.setRequestHeader("Accept", "*/*" );
  88. request.setRequestHeader("Content-Type", "application/json");
  89. request.setRequestHeader("Authorization", token);
  90. //Send the HTTP request to change the status
  91. request.send(JSON.stringify({"custom_status":{"text":status_text,"emoji_name":status_emoji}}));
  92. //If request failed
  93. request.onreadystatechange = () => {
  94. if (request.status != 200) {
  95. run = false;
  96. throw new Error("[Not an actually uncaught] Failed to update status. \n\nThe HTTP request failed. Most likely because the authorization token is incorrect.");
  97. }
  98. };
  99. }
  100. //Simple delay function for animation
  101. function delay(t) {
  102. return new Promise(function(resolve) {
  103. setTimeout(resolve, t)
  104. });
  105. }
  106. //Start the animation for the first time
  107. if (run) statusanimation();
  108. //Edit (Clear by default) status before exiting
  109. window.onbeforeunload = function () {
  110. run = false;
  111. status_text = default_status_text;
  112. status_emoji = default_status_emoji;
  113. setstatus();
  114. return "";
  115. };
  116. })();