Remix.run Logo
ekipan 2 days ago

Cannot stand robot code. Thanks for the genuinely cool thing though. I hope you don't mind me rewriting, if only for my own satisfaction.

  {
    const s = window.scroller = {}
    const press = (key, code) => () =>
      document.dispatchEvent(new KeyboardEvent('keydown', {
        key: key, keyCode: code,
        which: key, code: code, bubbles: true
      }));
    const step = () => {
      const div = document.querySelector('.animal-name')
      const name = div?.textContent.trim()
      if (name === 'Pando Clone') s.dir = s.left
      if (name === 'DNA') s.dir = s.right
      s.dir()
    }
    s.left = press(37, 'ArrowLeft')
    s.right = s.dir = press(39, 'ArrowRight')
    s.start = (ms) => s.ival = setInterval(step, ms)
    s.stop = () => clearInterval(s.ival)
  }
  scroller.start(5000)
kayge 2 days ago | parent [-]

I don't mind at all, your rewrite looks much more elegant. Thanks!

ekipan 2 days ago | parent [-]

You could try posting both at the LLM and ask it to explain the stuff I changed, if you're interested in learning more Javascript. Assuming you aren't already deep in the JS trenches and only vibed bc you couldn't be bothered.