views:

35

answers:

1

Hi,

I try to send a message from to global page to my injected.js on the moment that a setting changes:

global.html

    function settingChanged(event) {
            if(event.key == 'open') {
                    safari.self.tab.dispatchMessage('openChanged', safari.extension.settings['open']);
            }
        }

        safari.extension.settings.addEventListener("change", settingChanged, false);

injected.js

        // Message Event Handler
        function handleMessage(e) {
            if(e.name == 'openChanged') {
                console.log('%o', e);
                oi = e.message;
                resetNSL();
            }
        }

        // Message Event Listener
        safari.self.addEventListener('message', handleMessage, false);

I don't receive the message in injected.js, what am I doing wrong??

+1  A: 

In your global page, you cannot use safari.self.tab because the global page is not associated to any tab. You have access to all tabs, and you must decide which one's the good one. If all are good of if only the active one is good, it shouldn't be too hard:

// the active tab
safari.application.activeBrowserWindow.activeTab.page.dispatchMessage...

// all tabs
for (var i = 0; i < safari.application.browserWindows.length; i++)
{
    var browserWindow = safari.application.browserWindows[i];
    for (var j = 0; j < browserWindow.tabs.length; j++)
    {
        var tab = browserWindow.tabs[j];
        tab.page.dispatchMessage...
    }
}
zneak
Ok, thanks! This is what I needed ;)
dododedodonl