From 1d7e098853c02e6337209b90ade466f2798f1e63 Mon Sep 17 00:00:00 2001 From: "DESKTOP-O1U24RQ\\Kevin_Yu" Date: Tue, 21 Jun 2022 15:03:28 +0800 Subject: [PATCH] add timezone support --- puppet/package.json | 7 ++++--- puppet/src/contentscript.js | 3 ++- puppet/src/main.js | 2 +- puppet/src/puppet.js | 29 +++++++++++++++++++++++++++-- 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/puppet/package.json b/puppet/package.json index 04e955f..8d5c6c6 100644 --- a/puppet/package.json +++ b/puppet/package.json @@ -19,9 +19,10 @@ }, "dependencies": { "arg": "^4.1.3", - "chrono-node": "^2.1.7", - "systemd-daemon": "^1.1.2", - "puppeteer": "9.1.1" + "chrono-node": "^2.3.8", + "dayjs": "^1.11.3", + "puppeteer": "9.1.1", + "systemd-daemon": "^1.1.2" }, "devDependencies": { "babel-eslint": "^10.1.0", diff --git a/puppet/src/contentscript.js b/puppet/src/contentscript.js index 15a656b..faf2518 100644 --- a/puppet/src/contentscript.js +++ b/puppet/src/contentscript.js @@ -22,6 +22,7 @@ * @return {Promise} */ window.__chronoParseDate = function (text, ref, option) {} +window._tryParseDateByTimeZone = function (text, ref, option) { } /** * @param {...string} text - The objects to log. * @return {Promise} @@ -143,7 +144,7 @@ class MautrixController { * @private */ async _tryParseDate(text, ref, option) { - const parsed = await window.__chronoParseDate(text, ref, option) + const parsed = await window.__tryParseDateByTimeZone(text, ref, option) return parsed ? new Date(parsed) : null } diff --git a/puppet/src/main.js b/puppet/src/main.js index 7be1406..f693bda 100644 --- a/puppet/src/main.js +++ b/puppet/src/main.js @@ -42,7 +42,7 @@ MessagesPuppeteer.extensionDir = config.extension_dir || MessagesPuppeteer.exten MessagesPuppeteer.cycleDelay = config.cycle_delay || MessagesPuppeteer.cycleDelay MessagesPuppeteer.useXdotool = config.use_xdotool || MessagesPuppeteer.useXdotool MessagesPuppeteer.jiggleDelay = config.jiggle_delay || MessagesPuppeteer.jiggleDelay - +MessagesPuppeteer.timeZone = config.timeZone || MessagesPuppeteer.timeZone const api = new PuppetAPI(config.listen) function stop() { diff --git a/puppet/src/puppet.js b/puppet/src/puppet.js index 6917d42..ed0fd70 100644 --- a/puppet/src/puppet.js +++ b/puppet/src/puppet.js @@ -22,6 +22,9 @@ import chrono from "chrono-node" import TaskQueue from "./taskqueue.js" import { sleep } from "./util.js" +import dayjs from 'dayjs'; +import utc from 'dayjs/plugin/utc.js'; +import timezone from 'dayjs/plugin/timezone.js'; export default class MessagesPuppeteer { static profileDir = "./profiles" @@ -34,7 +37,7 @@ export default class MessagesPuppeteer { static viewport = { width: 960, height: 840 } static url = undefined static extensionDir = "extension_files" - + static timeZone = "Asia/Taipei" /** * * @param {string} id @@ -142,7 +145,7 @@ export default class MessagesPuppeteer { await this.page.exposeFunction("__mautrixLoggedOut", this._onLoggedOut.bind(this)) await this.page.exposeFunction("__chronoParseDate", chrono.parseDate) - + await this.page.exposeFunction("__tryParseDateByTimeZone", this._tryParseDateByTimeZone.bind(this)) // NOTE Must *always* re-login on a browser session, so no need to check if already logged in this.loginRunning = false this.loginCancelled = false @@ -150,6 +153,28 @@ export default class MessagesPuppeteer { this.log("Startup complete") } + async _tryParseDateByTimeZone(text, ref, option) { + const localTz = MessagesPuppeteer.timeZone // This is the ISO string + dayjs.extend(utc); + dayjs.extend(timezone); + const localTime = dayjs.tz(new Date(), localTz) + const localOffset = localTime.utcOffset() // returns in minutes + + const custom = chrono.casual.clone() + custom.refiners.push({ + refine: (results) => { + Array.from(results).forEach((result) => { + // Returns the time with the offset in included (must use minutes) + result.start.imply('timezoneOffset', localOffset) + result.end && result.end.imply('timezoneOffset', localOffset) + }) + return results + }}) + const parsed = custom.parseDate(text, ref, option) + this.log(`parsed ${parsed}`) + return parsed + } + async _preparePage(navigateTo) { await this.page.bringToFront() if (navigateTo) {