Skip to content

Using runtime.onInstalled.addListener #31

Description

@flosse

Adding a event listener with

let closure: Closure<dyn Fn(JsValue)> = Closure::new(move |js: JsValue| {
  let reason = js.as_string().expect("reason string"); 
  // ...
});
web_extensions_sys::chrome
  .runtime()
  .on_installed()
  .add_listener(closure.as_ref().unchecked_ref());
closure.forget();

but the event is never fired because the JS API runtime.onInstalled.addListener has to be called synchronously in the background script. And because WASM is asynchronously loaded, the execution of the web-extensions-sys wrapper is called too late

import init from './pkg/background.js';
// here we have to call runtime.onInstalled.addListener
init()
  .then(() => {
    // but it's called by the WASM module here
  })
  .catch(console.error)

There is a method that can init the module synchronously but the fetch is still asynchronous:

fetch("./pkg/background_bg.wasm")
   .then((response) => response.arrayBuffer())
   .then((bytes) => {
     wasm.initSync(bytes);
   })
   .catch(console.error);

So does anybody have an idea how to solve this problem?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is neededquestionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions