Javascript oAuth交换后关闭Chrome窗口

Javascript oAuth交换后关闭Chrome窗口,javascript,dom,google-chrome-extension,gmail,Javascript,Dom,Google Chrome Extension,Gmail,我有打开窗口的代码 let mywindow = window.open("utl", "title", "resizable=yes,width=600,height=400,toolbar=no,titlebar=no,menubar=no,scrollbars=yes"); 我想访问窗口的当前URL,我已尝试: 窗口位置 window.document.url 每次尝试都会返回: 未捕获DOMEException:阻止原点为“”的帧访问跨原点帧。 评估时(评估时)(铬)-extens

我有打开窗口的代码

let mywindow = window.open("utl", "title", "resizable=yes,width=600,height=400,toolbar=no,titlebar=no,menubar=no,scrollbars=yes");
我想访问窗口的当前URL,我已尝试:

  • 窗口位置
  • window.document.url
每次尝试都会返回:

未捕获DOMEException:阻止原点为“”的帧访问跨原点帧。 评估时(评估时)(铬)-extension://dapaeellgmlagjcopljjcfiadalafdil/extension.js:57053:21), :1:10) 在铬-extension://dapaeellgmlagjcopljjcfiadalafdil/extension.js:57053:21

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

我可以通过什么方式实现这一点?

您可以使用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);
});
您可以使用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.windows
API尝试了这一点,但窗口/选项卡的URL没有更新,因此URL属性没有更改。这是预期的吗?如何从窗口获取当前URL?@SanketDG我不知道为什么,您可能正在重定向(URL更改后)后添加更新侦听器(
chrome.tabs.onUpdated.addListener
)。要获取选项卡的URL,您可以使用以下命令:
chrome.windows.create({URL:..},win=>console.log(win.tabs[0].URL))
,基本上,创建新窗口后,访问第一个选项卡(
win.tabs[0]
)并检查其
url
属性。@SanketDG我已经编辑了我的问题,使用
chrome.windows
API添加了一个示例。我用
chrome.windows
API尝试了这一点,但窗口/选项卡的url没有更新,因此url属性没有更改。这是预期的吗?如何从窗口获取当前URL?@SanketDG我不知道为什么,您可能正在重定向(URL更改后)后添加更新侦听器(
chrome.tabs.onUpdated.addListener
)。要获取选项卡的URL,您可以使用以下内容:
chrome.windows.create({URL:..},win=>console.log(win.tabs[0.URL))
,基本上,在创建新窗口后,访问第一个选项卡(
win.tabs[0]
),并检查其
URL
属性。@SanketDG我已经编辑了我的问题,以使用
chrome.windows
API添加一个示例。