我有打开窗口的代码
let mywindow = window.open("utl", "title", "resizable=yes,width=600,height=400,toolbar=no,titlebar=no,menubar=no,scrollbars=yes");我想访问这个窗口的当前URL,我尝试过:
每次尝试都会返回:
Uncaught :阻止具有原始"https://mail.google.com“的帧访问跨原点帧。在(chrome-extension://dapaeellgmlagjcopljjcfiadalafdil/extension.js:57053:21),:1:10在chrome-extension://dapaeellgmlagjcopljjcfiadalafdil/extension.js:57053:21
我正在进行oAuth令牌交换,当窗口到达重定向URI时,我需要自动关闭该窗口。
我怎样才能做到这一点呢?
发布于 2019-09-25 06:38:35
您可以使用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);
}
}
})https://stackoverflow.com/questions/58092090
复制相似问题