GC Restock timer

Displays the time it took you to restock an item on grundos.cafe

  1. // ==UserScript==
  2. // @name GC Restock timer
  3. // @namespace https://github.com/windupbird144/
  4. // @version 0.5
  5. // @description Displays the time it took you to restock an item on grundos.cafe
  6. // @author github.com/windupbird144/
  7. // @match https://www.grundos.cafe/*
  8. // @match http://www.grundos.cafe/*
  9. // @grant none
  10. // @license https://unlicense.org/
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. /**
  15. * Displays a timer to show how long it took you to restock an item on grundos.cafe.
  16. * The timer is shown on the page that says "<Item> has been added to your inventory" page.
  17. * Limitation: The exact time when your restock is confirmed is only known on the grundos.cafe
  18. * server. We can only approximate it here.
  19. */
  20. "use strict";
  21. // If you change the way time is measured, update the version
  22. const timingVersion = "0";
  23. const newTimer = () => ({ start: Date.now(), stop: -1 });
  24. const stopTimer = (t) => ({ start: t.start, stop: Date.now() });
  25. const durationMs = (t) => t.stop - t.start;
  26. const save = (t) => localStorage.setItem('timer', JSON.stringify(t));
  27. const load = () => JSON.parse(localStorage.getItem('timer'));
  28. // Reset the timer and saves it. Only runs when you visit or refresh a shop
  29. function resetTimer() {
  30. const path = window.location.pathname;
  31. if (path.includes('viewshop')) {
  32. save(newTimer());
  33. return true;
  34. }
  35. }
  36. // Stops the timer and displays the duration on the page
  37. function stopAndDisplayTimer() {
  38. const target = document.querySelector(`button[onclick*='viewshop']`);
  39. if (!target) {
  40. return;
  41. }
  42. let timer = load();
  43. if (!timer) {
  44. return;
  45. }
  46. timer = stopTimer(timer);
  47. const seconds = (durationMs(timer) / 1000).toFixed(3);
  48. const msg = `You restocked this item in ${seconds} seconds.`;
  49. const html = `<div title="timing version ${timingVersion}"><br><b>${msg}</b></div>`;
  50. target.insertAdjacentHTML('afterend', html);
  51. }
  52. // If you run resetTimer, do not run stopAndDisplayTimer and vice verse
  53. resetTimer() || stopAndDisplayTimer();
  54.  
  55. })();