2

I am currently developing a Chrome Extension for my school which will notify students when the school's website is updated. I have already figured out how to tell chrome that i am going to make cross-origin requests. But I am stuck at trying to get the request. I have the following code in bg.js:

var checkUrl="http://www.some_website.com/";
var firstCheck = false;
window.setInterval(checkForUpdate, 10000);
var pageLoad = new Date().getTime();

function checkForUpdate() {
$.ajax(checkUrl, {
    ifModified : true,
    type : 'HEAD',

    success : function (response, status, xhr) {
        if (firstCheck === false) {
          firstCheck = true;
          return;
        }
        console.info(response);   //ALWAYS outputs 'undefined'
        console.info(status);     //ALWAYS outputs 'undefined'

        // if the server omits the 'Last-Modified' header
        // the following line will return 0. meaning that
        // has not updated.

        var lastModified = new Date(xhr.getResponseHeader('Last-Modified')).getTime();

        console.info(lastModified);//ALWAYS outputs 0

        if (lastModified > pageLoad) {
          alert("modified TEST"); //Will change later
        }
    }
});
}

And this manifest.json:

{
"manifest_version": 2,
"name": "Hyperion",
"version": "0.0.1",

"description": "TEST",
"icons": {"16":"icons/icon16_chk.png"},
"background": {
  "scripts":["javascript/jquery-3.2.0.min.js","javascript/bg.js"],
  "persistent":false
},

"permissions":["http://*/", "https://*/"],

"browser_action": {
  "default_icon":"icons/icon16_chk.png",
  "default_title":"Nothing has changed!"
}
}

Why is the request and status always 'undefined' and why can i not see the Last-Modified header in the response

Any help would be appreciated! //Oskar

Oskar T
  • 21
  • 2
  • `Last-Modified` header may have not been set by the remote server because it's not mandatory. You can inspect the response by opening the [background page devtools](https://stackoverflow.com/a/10258029)' network panel. – wOxxOm Mar 29 '17 at 10:35
  • Yes, but that still doesn't explain why the response and status variables are 'undefined' – Oskar T Mar 29 '17 at 11:04
  • Ah. You're using a non-persistent event page which is unloaded after 5 seconds of inactivity. – wOxxOm Mar 29 '17 at 11:48
  • No. I am using a background page which sends the requests as expected. The problem i am having is that the request and status variables are undefined – Oskar T Mar 29 '17 at 12:16
  • 1
    You have `"persistent": false` in manifest.json which means exactly what I told you. – wOxxOm Mar 29 '17 at 12:22
  • Oh... I see. But what will happen if i set `"persistent"` to true – Oskar T Mar 30 '17 at 07:11

0 Answers0