1

Is it possible to get server time with Greasemonkey?

A site I use tells you when you last did something showing server time... but the clock on the corner of the page shows my time zone... I was hoping to write a script to change that, but was hoping there was a built in function instead of me having to write a script to do math on the time...

So in other words... the clock on the page might say 5:54pm but the page after I water a plant says that I watered at 12:54am... it's enough of a pain remembering the difference between a site's time and mine... but when I get 2 different times showing on the site it completely floors me.

Can Greasemonkey retrieve server time?

Kat Cox
  • 3,469
  • 2
  • 17
  • 29
  • What server are you thinking about for this? The server would have to already support an API to allow its current time to be interrogated. – Pointy Sep 28 '13 at 20:00
  • @Pointy http://www.thepikaclub.co.uk I assumed that all servers had a way to check server time, since wordpress and other CMS have the option to show server time on your site... also assumed it had a way to access it since it does tell you what time you watered... – Kat Cox Sep 28 '13 at 20:26
  • Closely related question: [Accessing the web page's HTTP Headers in JavaScript](http://stackoverflow.com/q/220231/331508). – Brock Adams Sep 28 '13 at 22:40

1 Answers1

1

Most servers return the server time in the page's response headers. JavaScript cannot see these headers, for the current request, and Greasemonkey does not (yet) provide a way to see them.

Greasemonkey can fetch the server time, though, with special HEAD AJAX. Here's a complete userscript that does that:

// ==UserScript==
// @name     _Get web server time
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @grant    GM_xmlhttpRequest
// ==/UserScript==

GM_xmlhttpRequest ( {
    url:    location.href,
    method: "HEAD",
    onload: function (rsp) {
        var serverTime  = "Server date not reported!";
        var RespDate    = rsp.responseHeaders.match (/\bDate:\s+(.+?)(?:\n|\r)/i);

        if (RespDate  &&  RespDate.length > 1) {
            serverTime  = RespDate[1];
        }
        console.log ("Server Time: ", serverTime);
    }
} );

Beware:

  1. The server time may be different from the time on the webpage, if the server attempts to "localize" the page to your timezone. Most quality servers report their time in GMT.
  2. The fetched time will usually be within milliseconds of the time the target page saw. But, depending on your script, the page, network traffic, etc., the fetched time may also be a few seconds after the time that the target page started. You can't get an exact match.
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • Unfortunately this isn't working... possibly cause I'm using an older version of GM... I suspect this line `GM_xmlhttpRequest` is the problem... I tried an alert since console.log has never worked for me but it shows blank where the time would be... My GM is old enough that I don't have to use `@grant`. Either that, or the retrieved info doesn't match your regex... how would I get a look at what is being returned? – Kat Cox Sep 30 '13 at 18:47
  • Never mind that.... it's returning Date: Mon, 30 Sep 2013 18:48:23 GMT ,Mon, 30 Sep 2013 18:48:23 GMT – Kat Cox Sep 30 '13 at 18:49
  • I should clarify that... RespDate returns the above... serverTime actually returns Mon, 30 Sep 2013 20:56:12 GMT... but changing your console.log to an alert returns a blank time... I changed your comma to a + and it displays right... now to play with this to extract just the time... then maybe I can figure out how to use this to create a countdown clock for another page... Thanks! – Kat Cox Sep 30 '13 at 20:57