Synchronous message passing in the Safari extension

I am working on a safari extension and I was wondering if there is a way to make a synchronous message going through in a safari extension.

I want to send a message from my nested javascript to a global page so that the nested javascript is waiting for the result to return. The need to split my code into another function that receives a message from a global page seems too complicated.

+3
source share
2 answers

You can use the "special" message canLoad. Technically, it is intended to send a message and return a value related to the ability to load an element on a page, but in fact it is just a synchronous message that responds to a global HTML page just like any other. You would simply look for a message with a name 'canLoad'instead of passing the name of an arbitrary message:

// injected script
var myVar = safari.self.tab.canLoad( event );

// global HTML file
<!DOCTYPE html>

<script type="text/javascript" charset="utf-8">
  safari.application.addEventListener( 'message', listen, true );

  function listen( msgEvent ) {
    switch( msgEvent.name ) {
      case 'canLoad':
        msgEvent.message = 'My return value';
        break;
    }
  }
</script>

You can read more about the canLoad post in the development guide.

+4
source

There was someone else who asked the same question in the Safari developer forum. In short, you cannot. (Also, in my humble opinion, the function is canLoadabsolutely stupid.)

, .

0

Source: https://habr.com/ru/post/1754258/


All Articles