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.
source
share