开发者

Safari extension message to injected.js

I try to send a message from to global page to my injected.js on the moment that a s开发者_开发技巧etting 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??


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...
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜