javascript - oAuth 交换后关闭 Chrome 窗口
问题描述
我有打开一个窗口的代码
let mywindow = window.open("utl", "title", "resizable=yes,width=600,height=400,toolbar=no,titlebar=no,menubar=no,scrollbars=yes");
我想访问窗口的当前 URL,我尝试过:
- 窗口位置
- window.document.url
每次尝试都会返回:
未捕获的 DOMException:阻止具有源“ https://mail.google.com ”的框架访问跨域框架。在评估(评估在 (chrome-extension://dapaeellgmlagjcopljjcfiadalafdil/extension.js:57053:21), <anonymous>:1:10)在 chrome-extension://dapaeellgmlagjcopljjcfiadalafdil/extension.js:57053:21
我正在进行 oAuth 令牌交换,当窗口到达重定向 URI 时,我需要自动关闭窗口。
我可以通过哪些方式实现这一目标?
解决方案
您可以使用chrome.tabs
API 执行此操作,这是一个示例:
const orgURL = '<URL>';
chrome.tabs.create({url: orgURL}, createdTab => {
function updateListener(tabId, tab) => {
if (tabId == createdTab.id && tab.url !== orgURL) {
const redirectURL = tab.url;
// Do something with the redirect URL
chrome.tabs.remove(tabId); // Close the tab.
chrome.tabs.onUpdated.removeListener(updateListener);
}
}
chrome.tabs.onUpdated.addListener(updateListener);
});
不要忘记将chrome.tabs
权限添加到清单中。
如果您真的想使用新窗口而不是当前窗口中的新选项卡来执行此操作,请查看chrome.windows
API。
以下是使用chrome.windows
API 的示例:
const orgURL = "<URL>"
chrome.windows.create({ url: orgURL }, win => {
if (win.tabs.length) {
const firstTab = window.tabs[0];
if (firstTab.url !== orgURL) { // the redirect already happen
const redirectURL = window.tabs[0].url;
// Do something with the redirect URL
} else {// the redirect hasn't happen yet, listen for tab changes.
function updateListener(tabId, tab) => {
if (tabId == firstTab.id && tab.url !== orgURL) {
const redirectURL = tab.url;
// Do something with the redirect URL
chrome.windows.remove(win.id); // Close the window.
chrome.tabs.onUpdated.removeListener(updateListener);
}
}
chrome.tabs.onUpdated.addListener(updateListener);
}
}
})
推荐阅读
- javascript - 如何使用图像气泡创建滚动图像拼贴?
- python - 如何在使用 Tkinter 选择和删除单选按钮后创建单选按钮?
- node.js - 可能的竞争条件 Node/Express
- node.js - Node.js Soap 方法参数解析为字符串而不是相关对象
- css - 如何在 Safari 中制作粘性表格行
- java - 从我的对话框中调用活动方法时,为什么会出现 IndexOutOfBoundsException 错误?
- node.js - Discord.js 语音:无法播放 ytdl 流
- mysql - 使用 Express、NodeJS、MySQL 将最后插入的 ID 插入外键字段
- python - 使用 get() 访问 pd.Series 多索引未找到元素
- unity3d - 使用 Unity 从 AAR 文件调用 startMoniting 时发生致命崩溃