Skip to content

Commit

Permalink
fix(timezone): improve timezone spoofing; #325
Browse files Browse the repository at this point in the history
  • Loading branch information
sereneblue committed May 2, 2020
1 parent 24add97 commit ce5faf4
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions src/lib/spoof/timezone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,37 @@ export default {
const supportedLocalesOf = ORIGINAL_INTL.supportedLocalesOf;
window.Date = function(...args) {
let tmp = this instanceof Date ? new ORIGINAL_DATE(...args) : new ORIGINAL_DATE();
let timestamp = tmp.getTime();
if (isNaN(timestamp)) {
return tmp;
}
const modifyDate = (d) => {
let timestamp = d.getTime();
let spoofData = Object.assign({}, CHAMELEON_SPOOF.get(window).timezone);
let offsetIndex = spoofData.zone.untils.findIndex(o => o === null || (timestamp < o) );
let offsetNum = spoofData.zone.offsets[offsetIndex];
let offsetStr = \`\${offsetNum < 0 ? '+' : '-' }\${String(Math.abs(offsetNum / 60)).padStart(2, '0')}\${String(offsetNum % 60).padStart(2, '0')}\`;
let tzName = ORIGINAL_INTL('en-us', { timeZone: spoofData.zone.name, timeZoneName: 'long'}).format(tmp).split(', ')[1];
let tzName = ORIGINAL_INTL('en-us', { timeZone: spoofData.zone.name, timeZoneName: 'long'}).format(d).split(', ')[1];
spoofData.date = new ORIGINAL_DATE(toLocaleString.apply(tmp, [ spoofData.locale, { timeZone: spoofData.zone.name }]));
spoofData.date = new ORIGINAL_DATE(toLocaleString.apply(d, [ spoofData.locale, { timeZone: spoofData.zone.name }]));
spoofData.zoneInfo = {
offsetStr,
offsetNum,
tzAbbr: spoofData.zone.abbrs[offsetIndex],
tzName
};
tmp[window.CHAMELEON_SPOOF] = spoofData;
d[window.CHAMELEON_SPOOF] = spoofData;
return d;
}
window.Date = function(...args) {
let tmp = this instanceof Date ? new ORIGINAL_DATE(...args) : new ORIGINAL_DATE();
let timestamp = tmp.getTime();
if (isNaN(timestamp)) {
return tmp;
}
modifyDate(tmp);
return (this instanceof Date) ? tmp : tmp.toString();
};
Expand All @@ -62,30 +69,45 @@ export default {
if (isNaN(this.getTime())) {
return NaN;
}
if (!this[window.CHAMELEON_SPOOF]) modifyDate(this);
return getFullYear.apply(this[window.CHAMELEON_SPOOF].date);
}
window.Date.prototype.getHours = function(){
if (isNaN(this.getTime())) {
return NaN;
}
if (!this[window.CHAMELEON_SPOOF]) modifyDate(this);
return getHours.apply(this[window.CHAMELEON_SPOOF].date);
}
window.Date.prototype.getMinutes = function() {
if (isNaN(this.getTime())) {
return NaN;
}
if (!this[window.CHAMELEON_SPOOF]) modifyDate(this);
return getMinutes.apply(this[window.CHAMELEON_SPOOF].date);
}
window.Date.prototype.getMonth = function() {
if (isNaN(this.getTime())) {
return NaN;
}
if (!this[window.CHAMELEON_SPOOF]) modifyDate(this);
return getMonth.apply(this[window.CHAMELEON_SPOOF].date);
}
window.Date.prototype.getTimezoneOffset = function() {
if (isNaN(this.getTime())) {
return NaN;
}
if (!this[window.CHAMELEON_SPOOF]) modifyDate(this);
return this[window.CHAMELEON_SPOOF].zoneInfo.offsetNum;
}
window.Date.prototype.setDate = function(...args) {
Expand Down

0 comments on commit ce5faf4

Please sign in to comment.