lucidd/scala-js-chrome

chrome.storage.Storage.sync.set and get not working

dportabella opened this issue · 4 comments

After executing this scalajs code:

chrome.storage.Storage.sync.set(Map("key1" -> "value1"))

this javascript code

chrome.storage.sync.get(undefined, function(result) { console.log(JSON.stringify(result)); });

shows {"value1$5":"value1"} instead of {"key1":"value1"}
is this a bug?

also,

chrome.storage.Storage.sync.get(js.undefined).foreach(println)

throws an Error. why?

raquo commented

The facade was incorrect, it was fixed in 9518752

I don't know why the latest version is not published in Maven though.

@raquo , I don't manage to build and use a version of scala-js-chrome with the fix you mention. See #48
Any idea?

raquo commented

@dportabella Not super sure, but looks like you need to upgrade the versions of sbt plugins in this project, at the very least scalajs and scalafmt. Watch out for any breaking changes though. Also you might need to drop support for scala 2.10 in build.sbt to achieve that, that's a very old version.

Oh and better start from the master branch, it's one year newer than the diff I linked to.

Really that's a question for the maintainer, I was only passing by, don't know the details of this project.

Thanks @raquo .

I just gave up on building scala-js-chrome after spending two days on this.
Here it is my workaround in case someone is interested:

import chrome.utils.ErrorHandling.lastErrorOrValue

import scala.concurrent.{Future, Promise}
import scala.scalajs.js
import scala.scalajs.js.annotation.JSGlobal

object ChromeStorageSyncWrapper {
  def set(items: js.Dynamic): Future[Unit] = {
    val promise = Promise[Unit]()
    ChromeStorageSyncNative.set(items, js.Any.fromFunction0(() => {
      promise.complete(lastErrorOrValue(()))
    }))
    promise.future
  }

  def get(keys: js.UndefOr[js.Any] = js.undefined): Future[js.Dynamic] = {
    val promise = Promise[js.Dynamic]()
    ChromeStorageSyncNative.get(keys, (results: js.Dynamic) => {
      promise.complete(lastErrorOrValue(results))
    })
    promise.future
  }
}

@js.native
@JSGlobal("chrome.storage.sync")
object ChromeStorageSyncNative extends js.Object {
  def set(items: js.Dynamic, callback: js.UndefOr[js.Function0[_]] = js.undefined): Unit = js.native
  def get(keys: js.UndefOr[js.Any] = js.undefined, callback: js.Function1[js.Dynamic, _]): Unit = js.native
}

// Use:
ChromeStorageSyncWrapper.set(js.Dynamic.literal("key1" -> "value1", "key2" -> "value2"))
ChromeStorageSyncWrapper.get(js.Array("key1", "key2"))