简体   繁体   English

Chrome扩展程序 - 获取当前标签页并重新打开它(在新标签页中)无效

[英]Chrome Extension - get current tab url and re-open it (in a new tab) not working

I want to re-open the current site in a new tab when clicking on the icon of my extension in Chrome. 我想在Chrome中单击我的扩展程序图标时,在新标签页中重新打开当前网站。 Instead of re-opening the same site it opens a new empty tab with NO URL. 它不会重新打开同一个站点,而是打开一个没有URL的新空选项卡。 So if I'm on Twitter.com it opens a new chrome start page instead of Twitter.com 因此,如果我在Twitter.com上,它会打开一个新的Chrome启动页面,而不是Twitter.com

The code below is in background.js 下面的代码在background.js中

chrome.browserAction.onClicked.addListener(openTabs);
function openTabs(){
    var currentURL;

    chrome.tabs.query({'active': true, 'windowId': 
chrome.windows.WINDOW_ID_CURRENT},
    function(tabs){
        getCurrentURL(tabs[0].url);
    });

    function getCurrentURL(tab){
        currentURL = tab;
    }

chrome.tabs.create({ url: currentURL });
}

Thanks, moving chrome.tabs.create inside chrome.tabs.query solved it. 谢谢,在chrome.tabs.query里面移动chrome.tabs.create解决了它。

Move chrome.tabs.create inside chrome.tabs.query callback. 在chrome.tabs.query回调中移动chrome.tabs.create。 This callback is asynchronous, meaning that currently chrome.tabs.create gets called before currentURL variable is updated. 此回调是异步的,这意味着当前更新currentURL变量之前会调用chrome.tabs.create。 -

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM