开发者

SafariExtension messageEvent

i'm trying to make a SafariExtension but i have some issue with the messaging Api, in fact the example in the doc don't work, this: Safari Message Proxies

so What work and what don't work ? I can call the function on the global page from a injected script. I can't send a response to the injected script.

here what i have:

injected:

开发者_JAVA技巧
safari.self.tab.dispatchMessage("foo", "bar");

no need more, the bug is in the global html.

global:

safari.application.addEventListener("message", function ( e ) {
 if (e.name != 'foo')
   return false;
 e.target.page.dispatchMessage("bar", 'foo'); <-- Undefined on page ...
},false);

as i mention the 4th line on the global page always fails, so i can't send back an answer to the injected script ...

no clue on the documentation since this is almost extracted form the doc.


You need to include a listener in the target page. For example...

safari.self.addEventListener("message", function(e) {
    if(e.name == 'bar') {
        alert(e.message);
        // 'foo' would be alerted to the user, as it is the message content.
    };
}, false);

Messages are one-way transactions, so sending a message to the global page and receiving a message on the target page are two completely separate events. A listener is needed on both sides of the exchange.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜