Internet Roadtrip - Vote Failed Notification

Replace the "Voted" with a "Vote Failed" image when the vote endpoint returns a non-success result on neal.fun/internet-roadtrip

  1. // ==UserScript==
  2. // @name Internet Roadtrip - Vote Failed Notification
  3. // @namespace me.netux.site/user-scripts/neal-internet-roadtrip/vote-failed-notification
  4. // @match https://neal.fun/*
  5. // @icon https://cloudy.netux.site/neal_internet_roadtrip/vote-failed%20logo.png
  6. // @version 1.2.0
  7. // @author netux
  8. // @license MIT
  9. // @grant unsafeWindow
  10. // @grant GM_addStyle
  11. // @grant GM_getValue
  12. // @run-at document-start
  13. // @require https://cdn.jsdelivr.net/npm/internet-roadtrip-framework@0.4.1-beta
  14. // @description Replace the "Voted" with a "Vote Failed" image when the vote endpoint returns a non-success result on neal.fun/internet-roadtrip
  15. // ==/UserScript==
  16.  
  17. /* globals IRF, VM */
  18.  
  19. (async () => {
  20. const containerVDOM = await IRF.vdom.container;
  21.  
  22. GM_addStyle(`
  23. body.last-vote-failed .voted .voted-icon {
  24. content: url("https://cloudy.netux.site/neal_internet_roadtrip/vote-failed.png");
  25. }
  26. `);
  27.  
  28. const debugSettings = GM_getValue("DEBUG", {});
  29.  
  30. containerVDOM.state.vote = new Proxy(containerVDOM.methods.vote, {
  31. apply(ogVote, thisArg, args) {
  32. document.body.classList.toggle('last-vote-failed', false);
  33.  
  34. const ogFetch = unsafeWindow.fetch;
  35. unsafeWindow.fetch = new Proxy(ogFetch, {
  36. apply(ogFetch, thisArg, args) {
  37. if (!(args[0]?.includes?.('/vote') ?? true)) {
  38. return ogFetch.apply(thisArg, args);
  39. }
  40.  
  41. if (debugSettings.forceFail) {
  42. const body = JSON.parse(args[1].body);
  43. delete body.token;
  44. args[1].body = JSON.stringify(body);
  45. }
  46.  
  47. const result = ogFetch.apply(thisArg, args);
  48.  
  49. result.then((res) => {
  50. const isError = Math.floor(res.status / 100) !== 2;
  51. document.body.classList.toggle('last-vote-failed', isError);
  52.  
  53. if (isError) {
  54. setTimeout(() => {
  55. containerVDOM.state.showVotedAnim = false;
  56. containerVDOM.state.voted = false;
  57. //containerVDOM.state.$forceUpdate();
  58. }, 500);
  59. }
  60. });
  61.  
  62. return result;
  63. }
  64. });
  65.  
  66. const result = ogVote.apply(thisArg, args);
  67.  
  68. unsafeWindow.fetch = ogFetch;
  69.  
  70. return result;
  71. }
  72. })
  73. })();