简体   繁体   English

切换 chrome 标签页并每 5 秒刷新一次 - javascript

[英]Switch chrome tabs and refresh every 5 seconds - javascript

I'm trying to build a chrome extension using the Chrome tabs API that will cycle through all of the open tabs in a window and refresh each one every n seconds.我正在尝试使用 Chrome 选项卡 API 构建一个 chrome 扩展程序,该扩展程序将循环浏览窗口中所有打开的选项卡,并每 n 秒刷新一次。 I'm having trouble iterating my for loop once every 5 seconds.我在每 5 秒迭代一次 for 循环时遇到问题。 I've tried a few different ways of setting timeouts and such but I'm either ending up with the loop not running at all, or I get an infinite loop.我已经尝试了几种不同的设置超时等方法,但我要么最终循环根本没有运行,要么我得到一个无限循环。 I'm new to javascript.我是 javascript 新手。 Any help would be appreciated!任何帮助将不胜感激!

background.js背景.js

chrome.browserAction.onClicked.addListener(function(tab) {
  // Send a message to the active tab
  chrome.tabs.query({currentWindow: true}, function(tabs) {
    var foundSelected = false;
      (function switch() {
        for(i = 0; i <= tabs.length; i++){
          if(i == tabs.length){
            i = 0;
          }
          if (tabs[i].active){
            foundSelected = true;
          }
            // Finding the next tab    
          else if (foundSelected){
            // Selecting the next tab.
            chrome.tabs.update(tabs[i].id, {active: true});
            chrome.tabs.reload(tabs[i].id);
            setTimeout(switch, 5000);
          }
        }
      })();


    // var activeTab = tabs[0];
    chrome.tabs.sendMessage(activeTab.id, {"message": "clicked_browser_action"});
  });
});

content.js内容.js

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if( request.message === "clicked_browser_action" ) {

      chrome.tabs.update(tabId, {selected: true});
    }
  }
);

I realize that there are already extensions out there that do this.我意识到已经有扩展可以做到这一点。 I'm creating my own as a learning experience and for more control over functionality.我正在创建自己的作为学习经验和对功能的更多控制。

Create background.js like given below.创建如下所示的 background.js。 I thnik it will work properly.我认为它会正常工作。

background.js背景.js

/* Below code start background.js when you start your browser
 * and sw function work after every 5 second and reload every tab. 
 */
function sw()
{
   chrome.tabs.query({currentWindow: true},function(tabs){
     var i=0;
     console.log(tabs.length);
     for(i=0;i<=tabs.length;i++)
     {
         chrome.tabs.reload(tabs[i].id);
     }

   })
};
setInterval(sw,5000);

This is the working solution.这是有效的解决方案。

chrome.browserAction.onClicked.addListener(function(tab) {

  setInterval(function () {
    chrome.tabs.query({currentWindow: true}, function(tabs) {
      var foundSelected = false;
      for(var i = 0; i <= tabs.length; i++){
        if(i == tabs.length){
          i = 0;
        }
        if (tabs[i].active){
          foundSelected = true;
        }
        // Finding the next tab.
        else if (foundSelected){
          // Selecting the next tab.
          chrome.tabs.update(tabs[i].id, {active: true});
          chrome.tabs.reload(tabs[i].id);
          return;
        }
      }
    });
  }, 5000);

});

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

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