#147 First attempts with spacetime and time conversion
This commit is contained in:
parent
816ef34a65
commit
6cfa60bd9c
|
@ -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
|
||||
|
|
41
src/javascript/enhancements/timeConversion.js
Normal file
41
src/javascript/enhancements/timeConversion.js
Normal 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);
|
||||
})
|
||||
});
|
||||
}
|
|
@ -37,3 +37,18 @@ function handleKeyToggle(event, 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;
|
||||
}
|
Loading…
Reference in a new issue