#147 First attempts with spacetime and time conversion

This commit is contained in:
Serraniel 2020-12-28 16:09:53 +01:00
parent 816ef34a65
commit 6cfa60bd9c
Signed by: Serraniel
GPG key ID: 3690B4E7364525D3
3 changed files with 58 additions and 0 deletions

View file

@ -8,6 +8,7 @@ import { init as animeRequests } from './enhancements/animeRequests';
import { init as languageDisplay } from './enhancements/languageDisplay'; import { init as languageDisplay } from './enhancements/languageDisplay';
import { init as notifications } from './enhancements/notifications'; import { init as notifications } from './enhancements/notifications';
import { init as quickSearch } from './enhancements/quickSearch'; import { init as quickSearch } from './enhancements/quickSearch';
import { init as timeConversion } from './enhancements/timeConversion';
import { init as watch2getherChat } from './enhancements/watch2getherChat'; import { init as watch2getherChat } from './enhancements/watch2getherChat';
// css // css
import { init as cssEnhancements } from './enhancements/cssEnhancements'; import { init as cssEnhancements } from './enhancements/cssEnhancements';
@ -24,6 +25,7 @@ animeRequests();
languageDisplay(); languageDisplay();
notifications(); notifications();
quickSearch(); quickSearch();
timeConversion();
watch2getherChat(); watch2getherChat();
// css // css

View file

@ -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);
})
});
}

View file

@ -37,3 +37,18 @@ function handleKeyToggle(event, isPressed) {
isCtrlPressed = isPressed; 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;
}