0

I am writing a Chrome extension, in which there is a dialog-like window to let users input the username and password, which are then sent back to background page to make a request for the token in OAuth 2.0.

To send the username and password from dialog window back to background page, I used the following codes (inside the dialog window .html file):

<script>
function usrpwd(){
    var up = {};
    up.usr = document.login_form.usr.value;
    up.pwd = document.login_form.pwd.value;
    chrome.tabs.sendRequest(window.dialogArguments,up);
    window.close();
}
</script>

where the window.dialogArguments is supposed to be the tab ID of the extension's background page.

And the dialog window is opened in background page by

chrome.contextMenus.create({
  "title" : "show Modal Dialog",
  "contexts" : ["all", "page"], 
  "onclick": handle_click
});

function handle_click(){
    chrome.tabs.getSelected(null, function(tab){
        console.log('tab ', tab);   
        window.showModalDialog("login_popup.html", tab.id, "dialogHeight:300px; dialogLeft:200px;");
    });
}

The tab.id is supposed to be the ID of the background page, and it will be passed to dialog window and assigned to window.dialogArguments.

Also in the background page, the username and password are received by,

chrome.extension.onRequest.addListener(
    function(request){
        console.log("Username: ", request.usr);
        console.log("Username: ", request.pwd);
    }
);

However, console.log('tab ', tab) inside the handle_click function always shows that the getSelected tab is the tab where the context menu got clicked, not the background page. So I am wondering how to get the tab ID of the background page in this case. Or is there any other better ways to communicate between dialog window and background page?

Thanks a lot!

chaohuang
  • 3,965
  • 4
  • 27
  • 35

1 Answers1

1

Background pages do not have a tabId, since they are not tabs.

To send a message to the background page, use chrome.extension.sendRequest (extension instead of tabs).

PS. Full demo

Community
  • 1
  • 1
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • Hi, Rob, one more question. Now I can send the username and password from the a dialog window (opened by `window.showModalDialog`) back to the background page by using `chrome.extension.getBackgroundPage`, but I have to input and submit the username and password TWICE to close the dialog window. I don't know what's the problem. The dialog.html is [here](http://jsfiddle.net/dQkYX/2/). Thank you! – chaohuang May 01 '12 at 03:43
  • 1
    @chaohuang Press Ctrl+Shift+J to open the console, and check whether something weird happens. Instead of using `window.close()`, you can also use [`chrome.extension.sendRequest`](http://code.google.com/chrome/extensions/extension.html#method-sendRequest), and use the supplied `sender.tab.id` to close it using [`chrome.tabs.remove`](http://code.google.com/chrome/extensions/tabs.html#method-remove). – Rob W May 01 '12 at 07:08
  • I used `chrome.tabs.create` to replace the `window.showModalDialog`, then there is no problem. I guess there are some bugs regarding to `window.showModalDialog` in Chrome. Thanks for your patient help. I appreciate it. – chaohuang May 01 '12 at 20:25