GC Restock timer

Displays the time it took you to restock an item on neopetsclassic

目前为 2022-09-04 提交的版本。查看 最新版本

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