简体   繁体   English

Chrome 扩展程序中的更新选项卡并等到它打开以打开另一个下载链接。 如何等到 url 在选项卡中打开?

[英]Update Tab in Chrome Extension And Wait Till It opened to open another download link. How to wait till url is opened in the tab?

I want to open 100 links one by one and wait till each one is opened so that i can download data from it.我想一个一个打开100个链接,等到每个链接都打开,这样我就可以从中下载数据了。 How to wait till URL is opened in the tab?如何等到 URL 在选项卡中打开?

    function downloadreport()
    { 
        chrome.tabs.getSelected(null,function(tab)
        {
            var reportlink=tab.url;
            looplink=tab.url.slice(0,tab.url.length-1);
            var indexcode=reportlink.indexOf('code')
            reportlink=reportlink.slice(0,indexcode+4)+"/getReport.php";
            var i;
            for (i=0;i<100;i++)
            {
                chrome.tabs.update(tab.id,{url: looplink+i});
                /*

                How To Wait Here Till Tab Is Opened


                */
               chrome.tabs.update(tab.id,{url: reportlink});
            }
        });
    }
    document.addEventListener('DOMContentLoaded', function () {
    document.getElementById('start').addEventListener('click',downloadreport);
    }); 

Use async/await , WebExtension polyfill , Promise , and tabs.onUpdated listener:使用async/awaitWebExtension polyfillPromisetabs.onUpdated监听器:

async function downloadreport() { 
  const [tab] = await browser.tabs.query({active: true, currentWindow: true});
  let reportLink = tab.url.slice(0, tab.url.indexOf('code') + 4) + '/getReport.php';
  let loopLink = tab.url.slice(0, -1);
  for (let i = 0; i < 100; i++) {
    await goToUrl(tab, loopLink + i);
    await goToUrl(tab, reportLink);
  }
}

function goToUrl(tab, url) {
  browser.tabs.update(tab.id, {url});
  return new Promise(resolve => {
    browser.tabs.onUpdated.addListener(function onUpdated(tabId, info) {
      if (tabId === tab.id && info.status === 'complete') {
        browser.tabs.onUpdated.removeListener(onUpdated);
        resolve();
      }
    });
  });
}

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

相关问题 Chrome扩展程序:捕获到最新标签页的URL - Chrome Extension: Capture URL of the newest tab opened 如何强制我的 chrome 扩展程序等到请求结束才能发送另一个请求? - How to force my chrome extension wait till a request end to send another request? 等待量角器加载在新选项卡中打开的数据 - Wait for protractor to load data opened in new tab Chrome扩展程序-在后台打开浏览器时打开新标签页(Mac) - Chrome extension - open new tab when browser opened in background (Mac) Chrome 扩展程序 - 在新打开的标签页中不起作用 - Chrome Extension - Not working in the newly opened tab 将脚本注入新打开的标签页-Chrome扩展程序 - injecting a script into newly opened tab - Chrome Extension 将变量从Chrome扩展程序传递到已打开的标签 - Pass variable from Chrome extension to opened tab 如果我们再次单击相同的 URL,我如何使用相同的打开选项卡重定向 Chrome/Firefox 扩展? - How can i Redirect Chrome/Firefox Extension with same opened tab if we click on same URL again? 如何识别用户在 chrome 扩展程序中手动打开的新选项卡? - How to identify a new tab is opened by user manually in chrome extension? 如何从chrome扩展程序中打开标签页,等待用户输入并返回扩展程序? - how do I, from chrome extension, open tab wait for user input and go back to extension?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM