简体   繁体   English

Chrome扩展程序-标签创建/删除问题

[英]Chrome Extension - tab creation / remove issue

I'm building a Chrome Extension that creates a new tab, loads a page, retrieve the source code and closes the tab. 我正在构建一个Chrome扩展程序,它将创建一个新标签页,加载一个页面,检索源代码并关闭该标签页。 It's my first Chrome Extension. 这是我的第一个Chrome扩展程序。

So far creating the tab, retrieving the source code and closing works but something I can't explain is happening in the flow that causes repetitions. 到目前为止,创建选项卡,检索源代码并关闭工作,但是导致重复的流程中发生了我无法解释的事情。

I'm calling the following function in a FOR loop: 我在FOR循环中调用以下函数:

function chromeTabsCreateAsync(createProperties) 
{
    return new Promise((resolve, reject) => 
        {
            console.log('tab start');
            chrome.tabs.create(createProperties, tab => 
                {
                    if (chrome.runtime.lastError) 
                        {
                            reject(new Error(chrome.runtime.lastError));
                        } 
                    else 
                        {

                        chrome.tabs.onUpdated.addListener(onUpdated);
                        function onUpdated(updatedTabId, details) 
                            {
                                    console.log(details.status+' '+tab.id);
                                    if (details.status == 'complete') 
                                        {

                                            chrome.tabs.executeScript(tab.id, 
                                                {
                                                    file: 'test.js',

                                                }, function(results) 
                                                {

                                                    var source = results[0];

                                                    chrome.tabs.remove(tab.id); 
                                                    resolve(source);
                                                });

                                        }
                            }
                        }

                });
            console.log('tab end'); 
        });
}

The properties contain: url: some url active: false 这些属性包含:url:一些活动的url:false

I have voluntarily added console.log everywhere to show what is happening. 我自愿在各处添加console.log来显示正在发生的事情。

Here is the output I see in my console: 这是我在控制台中看到的输出:

First iteration: 第一次迭代:

tab start
tab end
loading 16
undefined 16
complete 16

=> so far, everything is ok =>到目前为止,一切正常

Second iteration: 第二次迭代:

tab start
tab end
loading 16
loading 18
undefined 16
undefined 18
undefined 16
undefined 18
complete 16
complete 18

=> uh? =>嗯? what is 16 doing here, the tab was already closed during the first iteration 16在这里做什么,在第一次迭代中该选项卡已经关闭

Third iteration: 第三次迭代:

tab start
tab end
loading 16
loading 18
loading 20
undefined 16
undefined 18
undefined 20
undefined 16
undefined 18
undefined 20
complete 16
complete 18
complete 20 

=> and it goes on like this, each iteration takes the tab id from the previous ones while those tabs have been closed. =>并继续进行,每次迭代都从前一个选项卡ID中关闭,而这些选项卡均已关闭。

Of course because the id doesn't exist anymore, I get an error: 当然,因为id不再存在,所以我得到一个错误:

Unchecked runtime.lastError while running tabs.executeScript: No tab with id: 16. at onUpdated 运行tabs.executeScript时未选中的runtime.lastError:ID为16的无选项卡。at onUpdated

Do you have any idea why those iterations re-use previous ids ? 您是否知道为什么这些迭代会重复使用以前的ID?

thanks 谢谢

Laurent 劳伦特

答案由wOxxOm提供(请参阅第一个评论)

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

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