Open and Close a Simple Popup Window in Chrome Extension

I have a Chrome extension should allow the users to log in with Google via a popup because it won't display in the iframe. I was doing this with window.open() which worked fine except that I could not close the pop-up after the user logged in.

I have been trying with no success to use chrome.windows.create instead of window.open in hopes that I will be able to close the popup once I detect via the URL that they have successfully logged in.

In popup_google.js I have simple function:

function login(login_url) {
    // var win = window.open(login_url, "windowname1", 'width=800, height=600');
    chrome.windows.create({'url': 'http://stackoverflow.com/', 'type': 'popup'}, function(window) { });
}

The login function is called via "onclick" something like this:

<a href='#' onClick='login("https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=http%3A%2F%2Fdev.sbceoportal.org%2Fcastest%2Fwp-login.php&client_id=191567976061-9uksb59gedrtlsmoeo1fouov94370j96.apps.googleusercontent.com&scope=openid+email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&access_type=online&approval_prompt=auto&state=d96bdddc11%257Chttp%253A%252F%252Fdev.sbceoportal.org%252Fcastest%252Fportal%252F");' id="loginText"> Click here to login </a>

I can't for the life of me get the chrome.windows.create to open a new popup window, even in the simplest form you see here, but window.open works like a charm (I just can't seem to get it closed in Chrome).

Answers


It looks like your popup_google.js file is a content script. Content scripts cannot use chrome.*APIs except for a few exceptions. This is why you can't use the chrome.window API at all. On a similar note, adding "tabs" to the permissions utilizes the chrome.Tabs API which is not one of the few allowed chrome APIs for content scripts. Try using the matches: [/url pattern/] field to limit the urls in which your content script will run, as shown in the content script API.

Content scripts have full access to the DOM but not to any variables or functions defined by the web pages. While you can create your own popup using window.open, the content script has no way to be sure it is referencing the correct 'window' variable when you wish to call window.close() because the script sees two windows: the one it created using window.open() and the window loaded on the page's DOM.

Try looking at Chrome's examples using popups and modify them to achieve your desired functionality. Or check out other great SO explanations to develop a working popup login.


Need Your Help

Jenkins clearcase Plugin

plugins jenkins

I am new to Jenkins and looking to download Clearcase plugin for it.

HTML 5 Video Tag

video html5

This is my first dip into HTML5, firstly is there a special way to open the html tag? I read a stack overflow question, which seemed to result in the fact that an rtsp could be srced in a video tag.