Chrome扩展程序,自动刷新页面。 JavaScript的

时间:2017-11-21 21:21:05

标签: javascript jquery google-chrome google-chrome-extension

我是Javascript的初学者。我想编写一个Chrome扩展程序,每隔x秒扫描一次页面以进行更改,但我无法让按钮开始刷新。

我是否过于复杂地刷新标签?我想做的就是继续刷新当前标签。当我修复了这个错误后,我需要添加代码,每次刷新都会刮掉页面的一部分,直到找到一个URL。

document.addEventListener('DOMContentLoaded', function () {

    var beginSearchButton = document.getElementById('beginSearchButton');

      setInterval(function () {

          beginSearchButton.addEventListener('click', function () {
              chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
              chrome.tabs.update(tabs[0].id, {url: tabs[0].url});
          });
      }); // <-- it was here

    }, 3000); // <-- should be here

}, false);
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Foley</title>
  <link rel="stylesheet" href="css/bootstrap.css">
  <link rel="stylesheet" href="css/style.css">
  <script src="js/popup.js">

  </script>
</head>
<body>

  <form>
    <div class="form-group">
      <label for="ppEmail">PP Email address</label>
      <input type="email" class="form-control" id="pp-email" placeholder="name@example.com">
    </div>
    <div class="form-group">
      <label for="ppPassword">PP PW</label>
      <input type="text" class="form-control" id="pp-password" placeholder="name@example.com">
    </div>
    <div class="form-group">
      <label for="searchTerm">Search Term</label>
      <input type="text" class="form-control" id="term" placeholder="">
    </div>
    <button id="beginSearchButton" class="btn btn-primary btn-block">Begin</button>
  </form>

  <script src="js/jquery.min.js"></script>
  <script src="js/popper.min.js"></script>
  <script src="js/bootstrap.min.js"></script>
</body>
</html>
{
  "manifest_version": 2,

  "name": "Foley",
  "description": "Foley",
  "version": "1.0",

  "browser_action": {
   "default_icon": "icon.png",
   "default_popup": "popup.html"
  },

  "permissions": [
   "activeTab",
   "webNavigation",
   "*://*/*"
   ]

}

1 个答案:

答案 0 :(得分:0)

正如我在评论中注意到的那样,你与括号混淆了。 这是正确的代码:

document.addEventListener('DOMContentLoaded', function () {

    var beginSearchButton = document.getElementById('beginSearch');

    setInterval(function () {

        beginSearchButton.addEventListener('click', function () {
            chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
                chrome.tabs.update(tabs[0].id, {url: tabs[0].url});
            });
        }); // <-- it was here

    }, 3000); // <-- should be here

}, false);