Charlie Fish Charlie Fish - 8 months ago 135
Javascript Question

Safari Extension Showing Popover Different Window

I'm trying to build a Safari Extension where when a user hits Command+B it will show the popover. Using the code below it works but always shows the popover on a different window not the current window/tab. I would like it to display the popover on the current window instead of switching to a different window and opening the popover there. It works perfectly if there is only one Safari window open but starts to have problems when multiple windows are open.

Any ideas?

Global Page File:

safari.application.addEventListener('message', function (e) {
if ( == 'Show Popover') {
}, false);

Injected Content:

document.addEventListener("keydown", keydown);

function keydown(event) {
if ( event.metaKey && event.keyCode == 66) {
event.preventDefault();'Show Popover', {});


This is because you are manually selecting the first toolbarItem.

You need to determine which toolbarItem the popover needs to appear on;

Something on the lines of this (untested)

var activeItem = safari.extension.toolbarItems.filter(function (button) {
    return button.identifier == toolbarItemId && button.browserWindow == safari.application.activeBrowserWindow;

You then use this variable for the showPopover function;


Hope this helps