Playing around with browser cookies in Scala.js

Photo by Laura James from Pexels

I spent this weekend playing around with Scala.js and learning how to manipulate browser cookies with it.

Check out the live demo at cookies.awwsmm.com.

The source is available at github.com/awwsmm/CookiesScalaJS.


This content originally appeared on DEV Community and was authored by DEV Community

Photo by Laura James from Pexels

I spent this weekend playing around with Scala.js and learning how to manipulate browser cookies with it.

Check out the live demo at cookies.awwsmm.com.

The source is available at github.com/awwsmm/CookiesScalaJS.

There are a few cool Scala flairs here, particularly this bit which pattern matches on a regular expression

  def clearAllCookies(): Unit = {
    val Cookie = "([^=]+)=.+".r
    document.cookie.split("; ").foreach {
      case Cookie(key) => clearCookie(key)
      case other => err.println(s"Couldn't parse '$other' as a key=value cookie pair")
    }
  }

In order to ensure that users don't include the ; or = characters in their cookie keys and values (which confuses the parser), I've also added these two little safeguards, as well

    // prevent the user from typing ';' or '=' into the input
    input.addEventListener("keypress", (e: dom.KeyboardEvent) => {
      if (e.key == ";" || e.key == "=") {
        e.preventDefault()
      }
    })

    // prevent the user from pasting ';' or '=' into the input
    input.addEventListener("paste", (e: dom.ClipboardEvent) => {
      val text = e.clipboardData.getData("text")
      if (text.contains(";") || text.contains("=")) {
        e.preventDefault()
      }
    })

What do you think? Anyone else on DEV doing anything cool with Scala.js?


This content originally appeared on DEV Community and was authored by DEV Community


Print Share Comment Cite Upload Translate Updates
APA

DEV Community | Sciencx (2021-11-21T20:06:28+00:00) Playing around with browser cookies in Scala.js. Retrieved from https://www.scien.cx/2021/11/21/playing-around-with-browser-cookies-in-scala-js/

MLA
" » Playing around with browser cookies in Scala.js." DEV Community | Sciencx - Sunday November 21, 2021, https://www.scien.cx/2021/11/21/playing-around-with-browser-cookies-in-scala-js/
HARVARD
DEV Community | Sciencx Sunday November 21, 2021 » Playing around with browser cookies in Scala.js., viewed ,<https://www.scien.cx/2021/11/21/playing-around-with-browser-cookies-in-scala-js/>
VANCOUVER
DEV Community | Sciencx - » Playing around with browser cookies in Scala.js. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/21/playing-around-with-browser-cookies-in-scala-js/
CHICAGO
" » Playing around with browser cookies in Scala.js." DEV Community | Sciencx - Accessed . https://www.scien.cx/2021/11/21/playing-around-with-browser-cookies-in-scala-js/
IEEE
" » Playing around with browser cookies in Scala.js." DEV Community | Sciencx [Online]. Available: https://www.scien.cx/2021/11/21/playing-around-with-browser-cookies-in-scala-js/. [Accessed: ]
rf:citation
» Playing around with browser cookies in Scala.js | DEV Community | Sciencx | https://www.scien.cx/2021/11/21/playing-around-with-browser-cookies-in-scala-js/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.