首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >oAuth交换后关闭Chrome窗口

问oAuth交换后关闭Chrome窗口
EN

Stack Overflow用户
提问于 2019-09-25 06:10:02
回答 1查看 343关注 0票数 0

我有打开窗口的代码

代码语言:javascript
复制
let mywindow = window.open("utl", "title", "resizable=yes,width=600,height=400,toolbar=no,titlebar=no,menubar=no,scrollbars=yes");

我想访问这个窗口的当前URL,我尝试过:

  • window.location
  • window.document.url

每次尝试都会返回:

Uncaught :阻止具有原始"https://mail.google.com“的帧访问跨原点帧。在(chrome-extension://dapaeellgmlagjcopljjcfiadalafdil/extension.js:57053:21),:1:10在chrome-extension://dapaeellgmlagjcopljjcfiadalafdil/extension.js:57053:21

我正在进行oAuth令牌交换,当窗口到达重定向URI时,我需要自动关闭该窗口。

我怎样才能做到这一点呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-25 06:38:35

您可以使用chrome.tabs API来完成这一任务,下面是一个示例:

代码语言:javascript
复制
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的示例:

代码语言:javascript
复制
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);
    }
  }
})
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58092090

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档