简体   繁体   English

在 Chrome 扩展程序中打开和关闭一个简单的弹出窗口

[英]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.我有一个 Chrome 扩展程序应该允许用户通过弹出窗口登录 Google,因为它不会显示在 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.我是用window.open()做这个的,它工作得很好,只是在用户登录后我无法关闭弹出窗口。

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.我一直在尝试使用chrome.windows.create而不是window.open ,但没有成功,希望一旦我通过 URL 检测到他们已成功登录,我就能够关闭弹出窗口。

In popup_google.js I have simple function: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) { });
}

The login function is called via "onclick" something like this:登录功能通过"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>

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).我一辈子都无法让chrome.windows.create打开一个新的弹出窗口,即使是你在这里看到的最简单的形式,但window.open就像一个魅力(我似乎无法chrome.windows.create在 Chrome 中关闭)。

It looks like your popup_google.js file is a content script.看起来您的 popup_google.js 文件是一个内容脚本。 Content scripts cannot use chrome.*APIs except for a few exceptions .除了少数例外,内容脚本不能使用 chrome.*API。 This is why you can't use the chrome.window API at all.这就是你根本不能使用 chrome.window API 的原因。 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.在类似的说明中,向权限添加“选项卡”会利用 chrome.Tabs API,这不是少数允许用于内容脚本的 chrome API 之一。 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 .尝试使用matches: [/url pattern/] 字段来限制您的内容脚本将在其中运行的网址,如内容脚本API 中所示

Content scripts have full access to the DOM but not to any variables or functions defined by the web pages.内容脚本可以完全访问 DOM,但不能访问网页定义的任何变量或函数。 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.虽然您可以使用 window.open 创建自己的弹出窗口,但是当您希望调用 window.close() 时,内容脚本无法确保它引用了正确的“窗口”变量,因为脚本看到两个窗口:一个是它使用 window.open() 创建并在页面的 DOM 上加载窗口。

Try looking at Chrome's examples using popups and modify them to achieve your desired functionality.尝试使用弹出窗口查看 Chrome 的示例并修改它们以实现您想要的功能。 Or check out other great SO explanations to develop a working popup login.或者查看其他很棒的 SO解释来开发一个有效的弹出登录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM