Gustav Bylund

+46 (0)73 026 26 86 hello@gustavbylund.se maistho

Autofill Squirdle filters

Squirdle is a fun wordle-like with a Pokémon theme. This bookmarklet makes typing in the filters quicker :)

Bookmarklet

Drag the following link to your bookmark bar!

Autofill Squirdle

Or add this code to a bookmark:

javascript:(()=>{(function(){const u=document.querySelector("#guess"),o=Array.from(document.querySelectorAll("#guesses [id^=guess]")).map(e=>{const r=Array.from(e.querySelectorAll(".emoji")).map(t=>t.src.match(/\/(\w+)\.png$/)[1]),s=pokedex[e.querySelector(".guess").textContent];return{emojis:r,values:s}}).reduce((e,r)=>(r.emojis.forEach((s,t)=>{s=="wrong"?(e[t][s]==null&&(e[t][s]=new Set),e[t][s].add(r.values[t]||"None")):(s==="correct"||e[t][s]==null||s==="up"&&r.values[t]>e[t][s]||s==="down"&&r.values[t]<e[t][s]||s==="wrongpos")&&(e[t][s]=r.values[t])}),e),[{},{},{},{},{}]),n=["gen","type","type","height","weight"],l=Array.from(new Set(o.map((e,r)=>{if(n[r]==="type"){const s=[];return e.wrongpos!=null&&s.push(`${n[r]}:${e.wrongpos||"None"}`),e.correct!=null&&s.push(`${n[r]}:${e.correct||"None"}`),e.wrong&&s.push(...Array.from(e.wrong).map(t=>`${n[r]}!${t}`)),s}else return e.correct!=null?[`${n[r]}:${e.correct}`]:Object.entries(e).map(([s,t])=>{switch(s){case"up":return`${n[r]}>${t}`;case"down":return`${n[r]}<${t}`}}).flat()}).flat()));u.value=l.join(" "),u.dispatchEvent(new Event("input",{bubbles:!0,cancelable:!0}))})();})();

Full code available below:

type GuessMap = {
  correct?: string | number
  wrongpos?: string
  wrong?: Set<string>
  up?: number
  down?: number
}
declare const pokedex: Record<string, [number, string, string, number, number]>
;(function () {
  const guessEl: HTMLInputElement = document.querySelector('#guess')
  const guesses = Array.from(
    document.querySelectorAll('#guesses [id^=guess]'),
  ).map((el) => {
    const emojis = Array.from(
      el.querySelectorAll<HTMLImageElement>('.emoji'),
    ).map((em) => em.src.match(/\/(\w+)\.png$/)[1])

    const values = pokedex[el.querySelector('.guess').textContent]

    return { emojis, values }
  })

  const reduced = guesses.reduce(
    (acc, curr) => {
      curr.emojis.forEach((emoji, index) => {
        if (emoji == 'wrong') {
          if (acc[index][emoji] == undefined) {
            acc[index][emoji] = new Set()
          }
          acc[index][emoji].add((curr.values[index] as string) || 'None')
        } else if (emoji === 'correct' || acc[index][emoji] == undefined) {
          acc[index][emoji] = curr.values[index]
        } else if (emoji === 'up' && curr.values[index] > acc[index][emoji]) {
          acc[index][emoji] = curr.values[index] as number
        } else if (emoji === 'down' && curr.values[index] < acc[index][emoji]) {
          acc[index][emoji] = curr.values[index] as number
        } else if (emoji === 'wrongpos') {
          acc[index][emoji] = curr.values[index] as string
        }
      })
      return acc
    },
    <GuessMap[]>[{}, {}, {}, {}, {}],
  )

  const names = ['gen', 'type', 'type', 'height', 'weight'] as const

  const values = Array.from(
    new Set(
      reduced
        .map((res, index) => {
          if (names[index] === 'type') {
            const arr = []
            if (res.wrongpos != undefined) {
              arr.push(`${names[index]}:${res.wrongpos || 'None'}`)
            }
            if (res.correct != undefined) {
              arr.push(`${names[index]}:${res.correct || 'None'}`)
            }
            if (res.wrong) {
              arr.push(
                ...Array.from(res.wrong).map(
                  (type) => `${names[index]}!${type}`,
                ),
              )
            }
            return arr
          } else if (res.correct != undefined) {
            return [`${names[index]}:${res.correct}`]
          } else {
            return Object.entries(res)
              .map(([k, v]) => {
                switch (k) {
                  case 'up':
                    return `${names[index]}>${v}`
                  case 'down':
                    return `${names[index]}<${v}`
                }
              })
              .flat()
          }
        })
        .flat(),
    ),
  )

  guessEl.value = values.join(' ')

  guessEl.dispatchEvent(
    new Event('input', {
      bubbles: true,
      cancelable: true,
    }),
  )
})()