Search code examples
javascriptsafarisafari-extension

Safari extension, open new tab with HTML page, pass parameters


In my extension I want to open a new tab when a toolbar button is clicked (works), display a static HTML page with JavaScript on the tab (works) and pass data (URL from the originating page) to the new tab (does not work). I tried:

  • Using query parameters like myTab.url = safari.extension.baseURI + 'page.html?' + params, but the target page does not seem to have a location assigned (location.search giving no result).

  • myTab.page.dispatchMessage("url", "someUrl"); after opening the tab, but the message never arrives in the new tab (I suspect, it's already "through", when the tab has opened).

Any suggestions?


Solution

  • I parsed document.URL in opened page for specific parameter and it worked for me. E.g.

    function __onLoad()
    {
        var p = $.url(document.URL);
        alert(p.param("url"));
    }
    

    And two possible reasons for missed message:

    • You did not add message event listener on your page
    • You called dispatchMessage before event listener was added

    Check Safari development doc at https://developer.apple.com/library/archive/documentation/Tools/Conceptual/SafariExtensionGuide/MessagesandProxies/MessagesandProxies.html