Search code examples
safarisafari-extension

Reload popover on event


Is there a way to reload the popover every time the Toolbar Item is clicked? I've been looking for something like, and going through documentation, but I've come up short. It seems so simple, but I cannot find a resolution for it at the moment.


Solution

  • Not sure what you mean by reloading a toolbar item (which is just a button), but you can reload a popover by calling window.location.reload(), just like any other window. Assuming you have just one popover, from a global page script you can use

    safari.extension.popovers[0].contentWindow.location.reload();
    

    Now, in order to make the popover reload when you click its associated toolbar item, you'll need to specify a command for the toolbar item (see the docs). Then you can use something like this, assuming you have just one toolbar item:

    safari.application.addEventListener('command', function (evt) {
        // assume your command is 'reloadPopover'
        if (evt.command == 'reloadPopover') {
            // evt.target is the toolbar item
            evt.target.popover.contentWindow.location.reload();
        }
    }, false);
    

    Note: If you do this, the popover will show a nasty sort of flashing/resizing effect when you open it. One way to avoid that might be to delay showing the popover until the reload is finished. If you want to do that, you will need to disassociate the popover from the toolbar item in the Extension Builder, and instead use the showPopover method of the toolbar item to show the popover when you are ready to.