Crossrider扩展中的按需弹出窗口

时间:2015-01-07 04:49:36

标签: crossrider

我正在寻找一种在Crossrider扩展中显示按需弹出的方法,与Javascript'确认'对话框一样。我在资源中有一个HTML页面,我想在弹出窗口中使用它,每当调度某个消息时都会显示该弹出窗口。我意识到在Crossrider中有显示弹出窗口的功能(appAPI.browserAction.setPopup),但是我想按需显示自定义弹出窗口,而不是简单的JS'确认'对话框。有没有办法做到这一点?谢谢。

1 个答案:

答案 0 :(得分:1)

您可以使用appAPI.browserAction.setPopupappAPI.browserAction.clearPopup的组合来控制弹出窗口。在以下示例中,扩展范围代码根据访问的页面确定需要哪个弹出窗口并相应地设置它:

<强> extension.js

appAPI.ready(function($) {
    if (appAPI.isMatchPages("*google", "*msn")) {
        // Send message to background to set the popup based on the page url
        appAPI.message.toBackground({
            request: 'set-popup',
            popup: (location.hostname.indexOf('google') !== -1)
                ? 'google.html'
                : 'msn.html'
        });
    } else {
        // Send message to background to clear the popup for other pages
        appAPI.message.toBackground({
            request: 'clear-popup'
        });
    }
});

<强> background.js

appAPI.ready(function($) {
    appAPI.browserAction.setResourceIcon('icon.jpg');
    appAPI.message.addListener(function(msg) {
        switch(msg.request) {
            case 'set-popup':
                // When setting the page, first clear the existing popup
                appAPI.browserAction.clearPopup();
                // then set the new popup
                appAPI.browserAction.setPopup({
                    resourcePath: msg.popup,
                    width: 300,
                    height: 200
                });
                break;
            case 'clear-popup':
                appAPI.browserAction.clearPopup();
                break;
        }
    });
});

[披露:我是Crossrider员工]

相关问题