【发布时间】:2016-01-27 05:17:34
【问题描述】:
我有一个 Chrome 扩展程序应该允许用户通过弹出窗口登录 Google,因为它不会显示在 iframe 中。我使用window.open() 执行此操作,效果很好,只是在用户登录后我无法关闭弹出窗口。
我一直在尝试使用chrome.windows.create 而不是window.open,但没有成功,希望一旦通过 URL 检测到他们已成功登录,我就能够关闭弹出窗口。
在popup_google.js我有简单的功能:
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) { });
}
通过"onclick" 调用登录函数,如下所示:
<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>
我这辈子都无法让chrome.windows.create 打开一个新的弹出窗口,即使是你在这里看到的最简单的形式,但window.open 就像一个魅力(我只是似乎无法得到它在 Chrome 中关闭)。
【问题讨论】:
-
您是否将选项卡权限添加到清单中?
-
不要在 html 中使用内联 js 处理程序,如果它位于扩展程序的弹出页面中。将其移动到单独的
popup.js文件中,并将其加载到popup.html中,并在关闭</body>标记之前使用<script src="popup.js"></script>。 -
好的,我可以添加标签权限并添加:
-
"optional_permissions": ["tabs"], "permissions": ["tabs"],我仍然无法让这一行工作:chrome.windows.create({'url': 'stackoverflow.com', 'type': 'popup'}, function(window) { });
标签: javascript google-chrome google-chrome-extension