【问题标题】:popup window in Chrome extensionChrome扩展程序中的弹出窗口
【发布时间】:2012-05-07 14:27:41
【问题描述】:

我正在编写一个 Chrome 扩展程序,我希望在用户单击上下文菜单时弹出一个登录窗口,以便用户输入用户名和密码。在 Chrome 扩展中,我只发现chrome.pageAction.setPopupchrome.browserAction.setPopup 可以用来显示弹出窗口,但它们仅在单击页面操作的图标或浏览器操作的图标时才显示弹出窗口,而不是上下文菜单。当然,我可以使用javascript提示框来做到这一点,但问题是提示框中的密码不能被屏蔽。所以我想知道是否还有其他方法可以在 Chrome 扩展中创建弹出窗口。

谢谢!

【问题讨论】:

    标签: javascript google-chrome-extension popup


    【解决方案1】:

    挑选:

    所有这些方法都允许您(您的扩展程序)打开一个新窗口/对话框,并处理来自该页面的逻辑。此页面应与您的扩展程序一起打包。
    请参阅Message passing 将输入的数据传递给您的扩展程序。

    演示

    您的扩展程序中的选项卡可以使用chrome.runtime.getBackgroundPage 直接访问后台页面。我将在此演示中演示此功能,以及传统的消息传递方式:

    manifest.json

    {
      "name": "Dialog tester",
      "version": "1.0",
      "manifest_version": 2,
      "background": {
          "scripts": ["background.js"],
          "persistent": false
      },
      "content_scripts": [{
          "matches": ["<all_urls>"],
          "js": ["open-dialog.js"]
      }]
    }
    

    background.js

    // Handle requests for passwords
    chrome.runtime.onMessage.addListener(function(request) {
        if (request.type === 'request_password') {
            chrome.tabs.create({
                url: chrome.extension.getURL('dialog.html'),
                active: false
            }, function(tab) {
                // After the tab has been created, open a window to inject the tab
                chrome.windows.create({
                    tabId: tab.id,
                    type: 'popup',
                    focused: true
                    // incognito, top, left, ...
                });
            });
        }
    });
    function setPassword(password) {
        // Do something, eg..:
        console.log(password);
    };
    

    open-dialog.js

    if (confirm('Open dialog for testing?'))
        chrome.runtime.sendMessage({type:'request_password'});
    

    dialog.html

    <!DOCTYPE html><html><head><title>Dialog test</title></head><body>
    <form>
        <input id="pass" type="password">
        <input type="submit" value="OK">
    </form>
    <script src="dialog.js"></script>
    </body></html>
    

    dialog.js

    document.forms[0].onsubmit = function(e) {
        e.preventDefault(); // Prevent submission
        var password = document.getElementById('pass').value;
        chrome.runtime.getBackgroundPage(function(bgWindow) {
            bgWindow.setPassword(password);
            window.close();     // Close dialog
        });
    };
    

    使用方法的文档

    【讨论】:

    • 嗨,Rob,感谢您的回复。这很有帮助。但是我只找到了如何在后台页面和内容脚本之间进行消息传递,所以你能告诉我如何在类对话框窗口和后台页面之间进行通信,以便可以将推算的用户名和密码发送回后台页面进行OAuth 2.0 中的令牌请求?再次感谢!
    • @RobW - 无论如何我可以将弹出窗口粘贴到启动它的当前选项卡上。目前它是一个独立的弹出窗口。
    • @Vidit 你的意思是添加到标签条(使用chrome.tabs.create)还是在当前页面中(不要这样做)?
    • @RobW 在当前页面中,而不是在新标签中。我不希望用户离开当前页面。所以像一个小的弹出显示可能在页面的右侧或右下角,甚至像浏览器动作弹出窗口一样的右上角。我刚刚发布了一个问题,也许可以为您提供更多详细信息。 stackoverflow.com/questions/35875730/…
    • @RobW,您所说的“扩展程序中的选项卡”是什么意思?另外,有没有办法停止“chrome.runtime.sendMessage”的广播性质,只将请求发送到后台运行时?
    猜你喜欢
    • 2023-01-02
    • 2011-09-27
    • 1970-01-01
    • 2012-03-08
    • 2011-06-12
    • 2012-02-17
    • 1970-01-01
    • 2017-12-29
    • 2012-11-12
    相关资源
    最近更新 更多