46

I am writing a Chrome extension, and I want a login window to be popped up when users click on the context menu so that user can input username and password. In Chrome extension, I only found chrome.pageAction.setPopup and chrome.browserAction.setPopup can be used to show popup windows, but they show popups only when the page action's icon or browser action's icon is clicked, not the context menu. Of course, I can use javascript prompt box to do this, but the problem is the password cannot be masked in the prompt box. So I am wondering if there are some other ways to create a popup window in Chrome extension.

Thanks!

chaohuang
  • 3,965
  • 4
  • 27
  • 35

1 Answers1

117

Pick and choose:

All of these methods allows you (your extension) to open a new window/dialog, and handle the logic from that page. This page should be packaged with your extension.
See Message passing to pass the entered data to your extension.

Demo

Tabs within your extension have direct access to the background page using chrome.runtime.getBackgroundPage. I'll demonstrate this feature in this demo, as well as a conventional way of message passing:

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
    });
};

Documentation for used methods

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • 1
    Hi, Rob, thanks for your reply. It's very helpful. But I only found how to make message passing between background page and content scripts, so could you tell me how to communicate between the dialog-like window and background page, so that the imputed username and password can be sent back to background page to make a request for a token in OAuth 2.0? Thanks again! – chaohuang Apr 30 '12 at 03:43
  • 1
    @RobW - Is there anyway I can stick the pop window to the current tab where it was initiated. It is a stand alone popup window currently. – manutdfan Mar 08 '16 at 18:38
  • @Vidit Do you mean adding to the tab strip (use `chrome.tabs.create`) or in the current page (don't do that)? – Rob W Mar 08 '16 at 20:18
  • 1
    @RobW In the current page, not in a new tab. I don't want the users to go away from the current page. So like a small popup show may be at the right side of the page or the bottom right or even the top right like browser action popup. I just posted a question, which can give you more detail perhaps. http://stackoverflow.com/questions/35875730/chrome-extension-create-custom-popup-from-context-menu-click – manutdfan Mar 08 '16 at 20:23
  • @RobW, What do you mean by the phrase "tabs within your extension"? Also, is there a way to stop the broadcast nature of "chrome.runtime.sendMessage", sending the request only to the background runtime? – Pacerier Aug 07 '17 at 14:53
  • @Pacerier Tabs in the browser with the URL of a page from your extension, e.g. chrome-extension://extensionid/page.html. You cannot change the broadcast nature of `chrome.runtime.sendMessage`, but as an alternative you could use [`chrome.runtime.connect`](https://developer.chrome.com/extensions/runtime#method-connect) to establish a message channel (port) to one of more specific destinations, and use the port's `postMessage` and `onMessage` APIs to communicate. – Rob W Aug 07 '17 at 15:27
  • Update: In the example code, method "chrome.extension.getURL()" should be renamed to [chrome.runtime.getURL](https://developer.chrome.com/docs/extensions/reference/runtime/#method-getURL). This solution is still very helpful 5 years on. +1 – Yogi Jan 24 '23 at 01:17