oversoon
oversoon

Reputation: 360

Chrome extension, auto refresh page. javascript

I'm a beginner at Javascript. I want to write a Chrome extension that scans a page every x seconds for changes but I can't get the button to begin refreshing.

Am I over complicating refreshing the tab? All I want is to do is keep refreshing the current tab. When I have fixed this error I need to add code that will scrape a section of the page every refresh until it finds a 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="[email protected]">
    </div>
    <div class="form-group">
      <label for="ppPassword">PP PW</label>
      <input type="text" class="form-control" id="pp-password" placeholder="[email protected]">
    </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",
   "*://*/*"
   ]

}

Upvotes: 0

Views: 2686

Answers (1)

Denis L
Denis L

Reputation: 3292

As I noticed in the comments, you confused with parentheses. Here is the correct code:

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);

Upvotes: 1

Related Questions