oAuth 交换后关闭 Chrome window
Close Chrome window after oAuth exchange
我有打开 window
的代码
let mywindow = window.open("utl", "title", "resizable=yes,width=600,height=400,toolbar=no,titlebar=no,menubar=no,scrollbars=yes");
我想访问window的当前URL,我试过:
- window.location
- window.document.url
每次尝试 returns:
Uncaught DOMException: Blocked a frame with origin "https://mail.google.com" from accessing a cross-origin frame.
at eval (eval at (chrome-extension://dapaeellgmlagjcopljjcfiadalafdil/extension.js:57053:21), <anonymous>:1:10)
at chrome-extension://dapaeellgmlagjcopljjcfiadalafdil/extension.js:57053:21
我正在进行 oAuth 令牌交换,当 window 命中重定向 URI 时,我需要 window 自动关闭。
我可以通过哪些方式实现这一目标?
您可以使用 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
权限添加到清单。
如果您真的想使用新的 window 而不是当前 window 中的新选项卡来执行此操作,请查看 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);
}
}
})
我有打开 window
的代码let mywindow = window.open("utl", "title", "resizable=yes,width=600,height=400,toolbar=no,titlebar=no,menubar=no,scrollbars=yes");
我想访问window的当前URL,我试过:
- window.location
- window.document.url
每次尝试 returns:
Uncaught DOMException: Blocked a frame with origin "https://mail.google.com" from accessing a cross-origin frame. at eval (eval at (chrome-extension://dapaeellgmlagjcopljjcfiadalafdil/extension.js:57053:21), <anonymous>:1:10) at chrome-extension://dapaeellgmlagjcopljjcfiadalafdil/extension.js:57053:21
我正在进行 oAuth 令牌交换,当 window 命中重定向 URI 时,我需要 window 自动关闭。
我可以通过哪些方式实现这一目标?
您可以使用 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
权限添加到清单。
如果您真的想使用新的 window 而不是当前 window 中的新选项卡来执行此操作,请查看 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);
}
}
})