开发者

Synchronous message passing in a Safari Extension

开发者 https://www.devze.com 2023-01-07 01:13 出处:网络
I\'m working on a safari extension and I was wondering if there is a way to do synchronous message passing in a safari extension.

I'm working on a safari extension and I was wondering if there is a way to do synchronous message passing in a safari extension.

I want to send a message from my injected javascript to the global page have the injected javascript wait until a result is returned. Having to split my code into another function that 开发者_运维百科receives a message from the global page just seems overly complicated.


You can use the "special" canLoad message. Technically, it's intended to send a message and return a value related to whether an element on the page can load, but it's really just a synchronous message that's answered by the global HTML page the same way as any other. You'd just look for the message named 'canLoad' instead of passing a custom message name:

// 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 message in the development guide.


There was someone else who asked the same question on the Safari developer forum. In short, you can't. (Besides, in my humble opinion, the canLoad function is purely stupid.)

You may, however, use closures to make it as painless as possible.

0

精彩评论

暂无评论...
验证码 换一张
取 消