19

I'm looking for a way to change the value returned from javascript's new Date() function.
In general - I'm looking for a way to write an extension that will give the user the ability to set his timezone (or diff from the time of his system's clock) without changing time/timezone on his computer.

I checked the chrome extension api but found nothing there. Will appreciate it if someone can point me at the right direction.

Dekel
  • 57,326
  • 8
  • 92
  • 123

1 Answers1

7

I have seen some solutions involving overriding javascript's getTime() method:

Date.prototype.getTime = function() { return [DATE + OFFSET] };

usage:

(new Date).getTime();

(source)

Apparently the time does not come from the browser but rather the operating system. So my suggestion would be to inject this javascript using a content script, and in your overriding function, you can offset the date/time by however much you would like.

Noam Hacker
  • 4,233
  • 7
  • 32
  • 53
  • 2
    That's basically how the [Timeshift extension](https://mybrowseraddon.com/change-timezone.html) does it, if you look at its source code (in `data/content_script/inject.js`). It redefines all properties of `Date`, e.g. `Object.defineProperty(Date.prototype, 'toJSON', {"value": function () {return toJSON.call(this._date)}});`. – Dan Dascalescu Dec 11 '21 at 21:09