From 6cfa60bd9cff001d273634b6a833cc9cf5154246 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Mon, 28 Dec 2020 16:09:53 +0100 Subject: [PATCH] #147 First attempts with spacetime and time conversion --- src/javascript/app.js | 2 + src/javascript/enhancements/timeConversion.js | 41 +++++++++++++++++++ src/javascript/utils/helpers.js | 15 +++++++ 3 files changed, 58 insertions(+) create mode 100644 src/javascript/enhancements/timeConversion.js diff --git a/src/javascript/app.js b/src/javascript/app.js index d34e051..36fdadb 100644 --- a/src/javascript/app.js +++ b/src/javascript/app.js @@ -8,6 +8,7 @@ import { init as animeRequests } from './enhancements/animeRequests'; import { init as languageDisplay } from './enhancements/languageDisplay'; import { init as notifications } from './enhancements/notifications'; import { init as quickSearch } from './enhancements/quickSearch'; +import { init as timeConversion } from './enhancements/timeConversion'; import { init as watch2getherChat } from './enhancements/watch2getherChat'; // css import { init as cssEnhancements } from './enhancements/cssEnhancements'; @@ -24,6 +25,7 @@ animeRequests(); languageDisplay(); notifications(); quickSearch(); +timeConversion(); watch2getherChat(); // css diff --git a/src/javascript/enhancements/timeConversion.js b/src/javascript/enhancements/timeConversion.js new file mode 100644 index 0000000..d0506b9 --- /dev/null +++ b/src/javascript/enhancements/timeConversion.js @@ -0,0 +1,41 @@ +import spacetime from 'spacetime'; +import { getGlobalConfiguration, SETTINGS_websiteAutoTimeConversion } from '../configuration/configuration'; +import * as core from '../utils/aniwatchCore'; +import * as helper from '../utils/helpers'; + +export function init() { + getGlobalConfiguration().getProperty(SETTINGS_websiteAutoTimeConversion, value => { + if (value) { + core.runAfterLoad(() => { + updateTimestamps(); + }, ".*"); + + core.runAfterLocationChange(() => { + updateTimestamps(); + }, ".*"); + } + }); +} + +function updateTimestamps() { + let nodes = helper.findTextNodes(); + + nodes.forEach(node => { + const REG_DATETIME = /(\d{2}(\/|\.)){2}\d{4} *\d?\d:\d{2}( (AM|PM))?/g; + let hits = Array.from(node.textContent.matchAll(REG_DATETIME), match => match[0]); + + hits.forEach(hit => { + // if time has a space before am/pm, this has to be removed for spacetime + let processedStr = hit.replace(/\s(am|pm)/i, '$1'); + + console.log(processedStr); + let datetime = spacetime(processedStr, 'UTC+1', { dmy: true }); + datetime = datetime.goto(spacetime().tz); + let replaceText = datetime.format('{date}. {month-short} {year} {time}'); + console.log(replaceText); + + console.log('-------') + // node.textContent = node.textContent.replace(hit, replaceText); + }) + }); +} \ No newline at end of file diff --git a/src/javascript/utils/helpers.js b/src/javascript/utils/helpers.js index 053aa18..0ab13ec 100644 --- a/src/javascript/utils/helpers.js +++ b/src/javascript/utils/helpers.js @@ -36,4 +36,19 @@ function handleKeyToggle(event, isPressed) { } else if (event.key === 'Control') { isCtrlPressed = isPressed; } +} + +export function findTextNodes(baseNode) { + if (!assigned(baseNode)) { + baseNode = document.documentElement; + } + + let walker = document.createTreeWalker(baseNode, NodeFilter.SHOW_TEXT, null, false); + let node; + let results = []; + while (node = walker.nextNode()) { + results.push(node); + } + + return results; } \ No newline at end of file