如何通过Chrome扩展程序同时重新加载两个非活动标签?

时间:2017-11-30 18:31:33

标签: google-chrome-extension

我想重新加载两个彼此相邻的标签。它可以工作,但是一旦我离开选项卡(例如,单击另一个选项卡)或单击页面上的其他位置,它就会停止刷新。为什么?无论我在哪里点击页面或浏览器,我都希望它能够不断刷新这些页面。

这是我的content.js

setInterval(function () {

  chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
    var currentIndex = tabs[0].index;
    var leftIndex = tabs[0].index - 1;
    chrome.tabs.query({currentWindow: true}, function (tabs) {// This will return all tabs in current window
      chrome.tabs.reload(tabs[currentIndex].id);
      chrome.tabs.reload(tabs[leftIndex].id);
    });
  })

}, 2000);

的manifest.json

{
  "manifest_version": 2,
  "name": "extension",
  "version": "1.0",
  "description": "My first Chrome extension.",
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "content_scripts": [
    {
      "matches": [
        "<all_urls>"
      ],
      "js": [
        "jquery.min.js",
        "content.js"
      ]
    }
  ]
}

popup.html

<!doctype html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
    <title>My cool extension</title>
    <script src="content.js"></script>
</head>
<body>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

我删除了popup.html,添加了背景部分,现在可以使用了。这是最后的剧本:

content.js

setInterval(function () {

  chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
    var currentIndex = tabs[0].index;
    var leftIndex = tabs[0].index - 1;
    chrome.tabs.query({currentWindow: true}, function (tabs) {// This will return all tabs in current window
      chrome.tabs.reload(tabs[0].id);
      chrome.tabs.reload(tabs[1].id);
    });
  });

}, 2000);

的manifest.json

{
  "manifest_version": 2,
  "name": "extension",
  "version": "1.0",
  "description": "My first Chrome extension.",
  "browser_action": {
    "default_icon": "icon.png"
  },
  "background": { "scripts": [ "content.js" ], "persistent": true },
  "content_scripts": [
    {
      "matches": [
        "<all_urls>"
      ],
      "js": [
        "jquery.min.js",
        "content.js"
      ]
    }
  ]
}