From 1dbaa043473132b1fa20aa0484c290abbd35358b Mon Sep 17 00:00:00 2001 From: Jonathan Perry-Houts Date: Fri, 27 Nov 2020 16:55:25 -0800 Subject: [PATCH] Make date parsing available to other extensions (for Mail Merge in particular) --- background.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/background.js b/background.js index 24b9161e..7f3af816 100644 --- a/background.js +++ b/background.js @@ -891,6 +891,29 @@ browser.runtime.onMessageExternal.addListener( }); return true; } + else if (message["action"] === "parseDate") { + let relativeTo = new Date(); + const rSec = relativeTo.getSeconds(), + pSec = SLStatic.previousLoop.getSeconds(); + if (rSec > pSec) { + const tdiff = rSec-pSec; + relativeTo = new Date(relativeTo.getTime() + 60000 - tdiff*1000); + } + try { + const localeCode = browser.i18n.getUILanguage(); + const date = Sugar.Date.get( + relativeTo, + message["value"], + {locale: localeCode, future: true} + ); + const dateStr = SLStatic.parseableDateTimeFormat(date); + sendResponse(dateStr); + return; + } catch (ex) { + SLStatic.debug("Unable to parse date/time",ex); + } + sendResponse(null); + } }); browser.runtime.onMessage.addListener(async (message) => {