I'm trying to build a simple safari app extension. On about:blank
I load a html page from resources folder of extension. Following is the code for that.
//script.js - injected script
if(window.location.href=="about:blank"){
window.location.href = safari.extension.baseURI + "page.html";
}
I want communication between this loaded page and safari app. Apparently injected script script.js
is not available on loaded html page.
I tried linking the script.js
to html page inline but the safari
object itself is not available for safari.extension.dispatchMessage
or safari.self.addEventListener
.
EDIT:
with this ( injected script script.js is not available on loaded html page/tab the page is loaded in) I mean that on opening web inspector in resources tab we can't see any extension scripts
If I understand your question correctly, you need to communicate between the page's javascript and the Safari app extension's native host app. The host app can only be contacted using API safari.extension.dispatchMessage which is accessible only from the injected script.js script but not from the page's JS.
You can solve this by sending and a request event from the page and registering the response event. Your HTML page would contain script:
Your script.js will catch this event by registering the event listener and can then pass the data over to the extension's native host app. After native host's response is obtained, response event can be dispatched back to the page.