MobyGames screenshot carousel

Browse screenshots without browser history entries

  1. // ==UserScript==
  2. // @name MobyGames screenshot carousel
  3. // @version 1.0
  4. // @description Browse screenshots without browser history entries
  5. // @author raina
  6. // @license GPLv3
  7. // @namespace raina
  8. // @match https://www.mobygames.com/game/*/*/screenshots/gameShotId,*
  9. // @grant none
  10. // ==/UserScript==
  11. (function() {
  12.  
  13. const wrapper = document.getElementById(`wrapper`);
  14. let prev, next;
  15.  
  16.  
  17. const flipPage = function(ev) {
  18.  
  19. let href = ev.currentTarget.href;
  20.  
  21. if (ev.shiftKey || ev.ctrlKey) return true;
  22.  
  23. ev.preventDefault();
  24.  
  25. if (/#$/.test(href)) return false;
  26.  
  27. fetch(href)
  28. .then(response => response.text())
  29. .then(content => {
  30.  
  31. history.replaceState({}, ``, href);
  32. content = content.replace(/.*<div id="wrapper"[^\n]*>/s, ``).replace(/<\/div>\s*<div id="footer.*/s, ``);
  33. wrapper.innerHTML = content;
  34. rigClicks();
  35.  
  36. });
  37. };
  38.  
  39.  
  40. const rigClicks = function() {
  41.  
  42. prev = document.querySelector(`li.previous a`);
  43. next = document.querySelector(`li.next a`);
  44.  
  45. [prev, next].forEach(butt => butt.addEventListener("click", flipPage));
  46.  
  47. };
  48.  
  49.  
  50. rigClicks();
  51.  
  52. addEventListener("keyup", ev => {
  53.  
  54. if ("ArrowLeft" != ev.key && "ArrowRight" != ev.key) return true;
  55.  
  56. let click = new MouseEvent("click", {button: 0, cancelable: true});
  57.  
  58. if ("ArrowLeft" == ev.key) prev.dispatchEvent(click);
  59. else next.dispatchEvent(click);
  60.  
  61. });
  62.  
  63. }());