From e7e22a10613dab1280dab6a71fa2524f16fc461e Mon Sep 17 00:00:00 2001 From: Serraniel Date: Mon, 28 Dec 2020 15:08:47 +0100 Subject: [PATCH 01/14] #147 Added base configuration --- src/html/settings.html | 3 +++ src/javascript/configuration/configuration.js | 1 + 2 files changed, 4 insertions(+) diff --git a/src/html/settings.html b/src/html/settings.html index b63bf93..fa2bb27 100644 --- a/src/html/settings.html +++ b/src/html/settings.html @@ -16,6 +16,9 @@
+ +
+
diff --git a/src/javascript/configuration/configuration.js b/src/javascript/configuration/configuration.js index 23ad4fd..73c4d92 100644 --- a/src/javascript/configuration/configuration.js +++ b/src/javascript/configuration/configuration.js @@ -3,6 +3,7 @@ import { assigned } from "../utils/helpers"; // website export const SETTINGS_websiteDisplayQuickSearch = 'websiteDisplayQuickSearch'; +export const SETTINGS_websiteAutoTimeConversion = 'websiteAutoTimeConversion'; export const SETTINGS_websiteShowNotificationsCountInTab = 'websiteShowNotificationsCountInTab'; export const SETTINGS_websiteHideUnusedTabs = 'websiteHideUnusedTabs'; export const SETTINGS_websiteOptimizeListAppearance = 'websiteOptimizeListAppearance'; -- 2.43.4 From 816ef34a65eb29fbefc3b6068834f02a04bffe16 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Mon, 28 Dec 2020 16:01:40 +0100 Subject: [PATCH 02/14] #147 installed spacetime --- package-lock.json | 24 +++++++++++++++++------- package.json | 2 ++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4854e38..92ee916 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3326,13 +3326,13 @@ } }, "color": { - "version": "3.1.2", - "resolved": "https://registry.npmjs.org/color/-/color-3.1.2.tgz", - "integrity": "sha512-vXTJhHebByxZn3lDvDJYw4lR5+uB3vuoHsuYA5AKuxRVn5wzzIfQKGLBmgdVRHKTJYeK5rvJcHnrd0Li49CFpg==", + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/color/-/color-3.1.3.tgz", + "integrity": "sha512-xgXAcTHa2HeFCGLE9Xs/R82hujGtu9Jd9x4NW3T34+OMs7VoPsjwzRczKHvTAHeJwWFwX5j15+MgAppE8ztObQ==", "dev": true, "requires": { "color-convert": "^1.9.1", - "color-string": "^1.5.2" + "color-string": "^1.5.4" } }, "color-convert": { @@ -3349,9 +3349,9 @@ "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=" }, "color-string": { - "version": "1.5.3", - "resolved": "https://registry.npmjs.org/color-string/-/color-string-1.5.3.tgz", - "integrity": "sha512-dC2C5qeWoYkxki5UAXapdjqO672AM4vZuPGRQfO8b5HKuKGBbKWpITyDYN7TOFKvRW7kOgAn3746clDBMDJyQw==", + "version": "1.5.4", + "resolved": "https://registry.npmjs.org/color-string/-/color-string-1.5.4.tgz", + "integrity": "sha512-57yF5yt8Xa3czSEW1jfQDE79Idk0+AkN/4KWad6tbdxUmAs3MvjxlWSWD4deYytcRfoZ9nhKyFl1kj5tBvidbw==", "dev": true, "requires": { "color-name": "^1.0.0", @@ -8434,6 +8434,11 @@ "xtend": "^4.0.0" } }, + "moment": { + "version": "2.29.1", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz", + "integrity": "sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==" + }, "mozjpeg": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/mozjpeg/-/mozjpeg-6.0.1.tgz", @@ -11775,6 +11780,11 @@ "integrity": "sha1-PpNdfd1zYxuXZZlW1VEo6HtQhKM=", "dev": true }, + "spacetime": { + "version": "6.12.2", + "resolved": "https://registry.npmjs.org/spacetime/-/spacetime-6.12.2.tgz", + "integrity": "sha512-w0St4Q9X8KtuZ/JY8+FM8a4hMrAoNNUWQCt9UQQAUzwk8eDW5wrGh4SaNvEg+9cjLF++vixm6SgJyC6F7ALF/A==" + }, "sparkles": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/sparkles/-/sparkles-1.0.1.tgz", diff --git a/package.json b/package.json index 99c7c1d..916d619 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,8 @@ }, "homepage": "https://github.com/Serraniel/AniwatchPlus#readme", "dependencies": { + "moment": "^2.29.1", + "spacetime": "^6.12.2", "uuid": "^8.3.2" }, "devDependencies": { -- 2.43.4 From 6cfa60bd9cff001d273634b6a833cc9cf5154246 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Mon, 28 Dec 2020 16:09:53 +0100 Subject: [PATCH 03/14] #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 -- 2.43.4 From 5fcaca8b547222922a6b6e83bb9cbb0a6114b286 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Mon, 28 Dec 2020 16:35:51 +0100 Subject: [PATCH 04/14] #147 optimization for 24h format --- src/javascript/enhancements/timeConversion.js | 40 +++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/javascript/enhancements/timeConversion.js b/src/javascript/enhancements/timeConversion.js index d0506b9..578038e 100644 --- a/src/javascript/enhancements/timeConversion.js +++ b/src/javascript/enhancements/timeConversion.js @@ -17,25 +17,59 @@ export function init() { }); } +function getSpaceTimeFormat(use24Format) { + if (use24Format) { + return '{date}. {month-short} {year} {time-24}'; + } + + return '{date}. {month-short} {year} {time}'; +} + function updateTimestamps() { let nodes = helper.findTextNodes(); nodes.forEach(node => { const REG_DATETIME = /(\d{2}(\/|\.)){2}\d{4} *\d?\d:\d{2}( (AM|PM))?/g; + const REG_TIME = /\d?\d:\d{2}/; + const REG_AMPM = /\s(am|pm)/i; + let hits = Array.from(node.textContent.matchAll(REG_DATETIME), match => match[0]); hits.forEach(hit => { + let use24Format = false; + let processedStr = hit + + // string must be converted into 12h format + if (processedStr.search(REG_AMPM) < 0) { + let timeStr = processedStr.match(REG_TIME)[0]; + let hm = timeStr.split(':'); + let hour = parseInt(hm[0]); + + if (hour >= 12) { + timeStr = timeStr.replace(`${hour}:`, `${hour - 12}:`); + timeStr += 'pm'; + } + else { + timeStr += 'am'; + } + + console.log(timeStr) + + processedStr = processedStr.replace(REG_TIME, timeStr); + use24Format = true; + } + // if time has a space before am/pm, this has to be removed for spacetime - let processedStr = hit.replace(/\s(am|pm)/i, '$1'); + processedStr = processedStr.replace(REG_AMPM, '$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}'); + let replaceText = datetime.format(getSpaceTimeFormat(use24Format)); console.log(replaceText); console.log('-------') - // node.textContent = node.textContent.replace(hit, replaceText); + node.textContent = node.textContent.replace(hit, replaceText); }) }); } \ No newline at end of file -- 2.43.4 From c76aebda9445a9da79b17fba84fe188a17672f38 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Mon, 28 Dec 2020 16:40:36 +0100 Subject: [PATCH 05/14] #147 Fixed parsing and removed console.log --- src/javascript/enhancements/timeConversion.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/javascript/enhancements/timeConversion.js b/src/javascript/enhancements/timeConversion.js index 578038e..58c107d 100644 --- a/src/javascript/enhancements/timeConversion.js +++ b/src/javascript/enhancements/timeConversion.js @@ -7,11 +7,15 @@ export function init() { getGlobalConfiguration().getProperty(SETTINGS_websiteAutoTimeConversion, value => { if (value) { core.runAfterLoad(() => { - updateTimestamps(); + updateTimestamps(document.documentElement); }, ".*"); core.runAfterLocationChange(() => { - updateTimestamps(); + updateTimestamps(document.documentElement); + }, ".*"); + + core.registerScript(node => { + updateTimestamps(node); }, ".*"); } }); @@ -25,8 +29,8 @@ function getSpaceTimeFormat(use24Format) { return '{date}. {month-short} {year} {time}'; } -function updateTimestamps() { - let nodes = helper.findTextNodes(); +function updateTimestamps(node) { + let nodes = helper.findTextNodes(node); nodes.forEach(node => { const REG_DATETIME = /(\d{2}(\/|\.)){2}\d{4} *\d?\d:\d{2}( (AM|PM))?/g; @@ -53,8 +57,6 @@ function updateTimestamps() { timeStr += 'am'; } - console.log(timeStr) - processedStr = processedStr.replace(REG_TIME, timeStr); use24Format = true; } @@ -62,13 +64,10 @@ function updateTimestamps() { // if time has a space before am/pm, this has to be removed for spacetime processedStr = processedStr.replace(REG_AMPM, '$1'); - console.log(processedStr); let datetime = spacetime(processedStr, 'UTC+1', { dmy: true }); datetime = datetime.goto(spacetime().tz); let replaceText = datetime.format(getSpaceTimeFormat(use24Format)); - console.log(replaceText); - - console.log('-------') + node.textContent = node.textContent.replace(hit, replaceText); }) }); -- 2.43.4 From 139e1e0cffe85e178944f735e034896f7001f83f Mon Sep 17 00:00:00 2001 From: Serraniel Date: Mon, 28 Dec 2020 16:48:51 +0100 Subject: [PATCH 06/14] #147 Update dates --- src/javascript/enhancements/timeConversion.js | 114 ++++++++++++------ 1 file changed, 77 insertions(+), 37 deletions(-) diff --git a/src/javascript/enhancements/timeConversion.js b/src/javascript/enhancements/timeConversion.js index 58c107d..a53e998 100644 --- a/src/javascript/enhancements/timeConversion.js +++ b/src/javascript/enhancements/timeConversion.js @@ -21,7 +21,7 @@ export function init() { }); } -function getSpaceTimeFormat(use24Format) { +function getSpaceDateTimeFormat(use24Format) { if (use24Format) { return '{date}. {month-short} {year} {time-24}'; } @@ -29,46 +29,86 @@ function getSpaceTimeFormat(use24Format) { return '{date}. {month-short} {year} {time}'; } +function getSpaceDateFormat() { + return '{date}. {month-short} {year}'; +} + +function updateDateTime(node) { + const REG_DATETIME = /(\d{2}(\/|\.)){2}\d{4} *\d?\d:\d{2}( (AM|PM))?/g; + const REG_TIME = /\d?\d:\d{2}/; + const REG_AMPM = /\s(am|pm)/i; + + let hits = Array.from(node.textContent.matchAll(REG_DATETIME), match => match[0]); + + if (hits.length === 0) { + return false; + } + + hits.forEach(hit => { + let use24Format = false; + let processedStr = hit + + // string must be converted into 12h format + if (processedStr.search(REG_AMPM) < 0) { + let timeStr = processedStr.match(REG_TIME)[0]; + let hm = timeStr.split(':'); + let hour = parseInt(hm[0]); + + if (hour >= 12) { + timeStr = timeStr.replace(`${hour}:`, `${hour - 12}:`); + timeStr += 'pm'; + } + else { + timeStr += 'am'; + } + + processedStr = processedStr.replace(REG_TIME, timeStr); + use24Format = true; + } + + // if time has a space before am/pm, this has to be removed for spacetime + processedStr = processedStr.replace(REG_AMPM, '$1'); + + let datetime = spacetime(processedStr, 'UTC+1', { dmy: true }); + datetime = datetime.goto(spacetime().tz); + let replaceText = datetime.format(getSpaceDateTimeFormat(use24Format)); + + node.textContent = node.textContent.replace(hit, replaceText); + }); + + return true; +} + +function updateDate(node) { + const REG_DATE = /(\d{2}(\/|\.)){2}\d{4}/g; + + let hits = Array.from(node.textContent.matchAll(REG_DATE), match => match[0]); + + if (hits.length === 0) { + return false; + } + + hits.forEach(hit => { + let datetime = spacetime(hit, 'UTC+1', { dmy: true }); + datetime = datetime.goto(spacetime().tz); + let replaceText = datetime.format(getSpaceDateFormat()); + + node.textContent = node.textContent.replace(hit, replaceText); + }); + + return true; +} + function updateTimestamps(node) { let nodes = helper.findTextNodes(node); nodes.forEach(node => { - const REG_DATETIME = /(\d{2}(\/|\.)){2}\d{4} *\d?\d:\d{2}( (AM|PM))?/g; - const REG_TIME = /\d?\d:\d{2}/; - const REG_AMPM = /\s(am|pm)/i; + if (updateDateTime(node)) { + return; + } - let hits = Array.from(node.textContent.matchAll(REG_DATETIME), match => match[0]); - - hits.forEach(hit => { - let use24Format = false; - let processedStr = hit - - // string must be converted into 12h format - if (processedStr.search(REG_AMPM) < 0) { - let timeStr = processedStr.match(REG_TIME)[0]; - let hm = timeStr.split(':'); - let hour = parseInt(hm[0]); - - if (hour >= 12) { - timeStr = timeStr.replace(`${hour}:`, `${hour - 12}:`); - timeStr += 'pm'; - } - else { - timeStr += 'am'; - } - - processedStr = processedStr.replace(REG_TIME, timeStr); - use24Format = true; - } - - // if time has a space before am/pm, this has to be removed for spacetime - processedStr = processedStr.replace(REG_AMPM, '$1'); - - let datetime = spacetime(processedStr, 'UTC+1', { dmy: true }); - datetime = datetime.goto(spacetime().tz); - let replaceText = datetime.format(getSpaceTimeFormat(use24Format)); - - node.textContent = node.textContent.replace(hit, replaceText); - }) + if (updateDate(node)) { + return; + } }); } \ No newline at end of file -- 2.43.4 From 25bb2de5cde0cfe96527fee2d3fbfc77aed9ba36 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Mon, 28 Dec 2020 18:05:41 +0200 Subject: [PATCH 07/14] #147 Time parsing --- src/javascript/enhancements/timeConversion.js | 73 ++++++++++++++++++- 1 file changed, 71 insertions(+), 2 deletions(-) diff --git a/src/javascript/enhancements/timeConversion.js b/src/javascript/enhancements/timeConversion.js index a53e998..9f69de3 100644 --- a/src/javascript/enhancements/timeConversion.js +++ b/src/javascript/enhancements/timeConversion.js @@ -3,6 +3,8 @@ import { getGlobalConfiguration, SETTINGS_websiteAutoTimeConversion } from '../c import * as core from '../utils/aniwatchCore'; import * as helper from '../utils/helpers'; +const __alteredNodes = []; + export function init() { getGlobalConfiguration().getProperty(SETTINGS_websiteAutoTimeConversion, value => { if (value) { @@ -22,11 +24,15 @@ export function init() { } function getSpaceDateTimeFormat(use24Format) { + return `${getSpaceDateFormat()} ${getSpaceTimeFormat(use24Format)}`; +} + +function getSpaceTimeFormat(use24Format) { if (use24Format) { - return '{date}. {month-short} {year} {time-24}'; + return '{time-24}'; } - return '{date}. {month-short} {year} {time}'; + return '{time}'; } function getSpaceDateFormat() { @@ -99,15 +105,78 @@ function updateDate(node) { return true; } +function updateTime(node) { + const REG_TIME = /\d?\d:\d{2}( (AM|PM))?/g; + const REG_AMPM = /\s(am|pm)/i; + + let hits = Array.from(node.textContent.matchAll(REG_TIME), match => match[0]); + + if (hits.length === 0) { + return false; + } + + hits.forEach(hit => { + let use24Format = false; + let processedStr = hit + + // string must be converted into 12h format + if (processedStr.search(REG_AMPM) < 0) { + let timeStr = processedStr.match(REG_TIME)[0]; + let hm = timeStr.split(':'); + let hour = parseInt(hm[0]); + + if (hour >= 12) { + timeStr = timeStr.replace(`${hour}:`, `${hour - 12}:`); + timeStr += 'pm'; + } + else { + timeStr += 'am'; + } + + processedStr = processedStr.replace(REG_TIME, timeStr); + use24Format = true; + } + + // if time has a space before am/pm, this has to be removed for spacetime + processedStr = processedStr.replace(REG_AMPM, '$1'); + + let datetime = spacetime(); + datetime = datetime.goto('UTC+1'); + datetime = datetime.time(processedStr); + datetime = datetime.goto(spacetime().tz); + let replaceText = datetime.format(getSpaceTimeFormat(use24Format)); + + console.log(node.textContent); + console.log(processedStr); + console.log(replaceText); + console.log('----------') + node.textContent = node.textContent.replace(hit, replaceText); + }); + + return true; +} + function updateTimestamps(node) { let nodes = helper.findTextNodes(node); nodes.forEach(node => { + // avoid double updates + if (__alteredNodes.indexOf(node) >= 0) { + return; + } + if (updateDateTime(node)) { + __alteredNodes.push(node); return; } if (updateDate(node)) { + __alteredNodes.push(node); + return; + } + + if (updateTime(node)) { + __alteredNodes.push(node); return; } }); -- 2.43.4 From 85bd44b677381dffff9c7752bfecb390d094d92a Mon Sep 17 00:00:00 2001 From: Serraniel Date: Mon, 28 Dec 2020 17:17:56 +0100 Subject: [PATCH 08/14] #147 Display correct timezone on hover --- src/javascript/enhancements/timeConversion.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/javascript/enhancements/timeConversion.js b/src/javascript/enhancements/timeConversion.js index 9f69de3..5c230e4 100644 --- a/src/javascript/enhancements/timeConversion.js +++ b/src/javascript/enhancements/timeConversion.js @@ -156,6 +156,16 @@ function updateTime(node) { return true; } +function updateTimeZone(node) { + const HINT_UTC = 'UTC+1'; + if (node.textContent === HINT_UTC) { + let tzMeta = spacetime().timezone(); + console.log(tzMeta); + + node.textContent = `${tzMeta.name} (UTC${tzMeta.current.offset >= 0 ? '+' : ''}${tzMeta.current.offset})`; + } +} + function updateTimestamps(node) { let nodes = helper.findTextNodes(node); @@ -179,5 +189,7 @@ function updateTimestamps(node) { __alteredNodes.push(node); return; } + + updateTimeZone(node); }); } \ No newline at end of file -- 2.43.4 From 7e3db5f8d51d18e0dd0500fc3f10c83440e95c6e Mon Sep 17 00:00:00 2001 From: Serraniel Date: Mon, 28 Dec 2020 17:18:49 +0100 Subject: [PATCH 09/14] #147 smaller refactorings --- src/javascript/enhancements/timeConversion.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/javascript/enhancements/timeConversion.js b/src/javascript/enhancements/timeConversion.js index 5c230e4..df1aec9 100644 --- a/src/javascript/enhancements/timeConversion.js +++ b/src/javascript/enhancements/timeConversion.js @@ -39,7 +39,7 @@ function getSpaceDateFormat() { return '{date}. {month-short} {year}'; } -function updateDateTime(node) { +function tryUpdateDateTime(node) { const REG_DATETIME = /(\d{2}(\/|\.)){2}\d{4} *\d?\d:\d{2}( (AM|PM))?/g; const REG_TIME = /\d?\d:\d{2}/; const REG_AMPM = /\s(am|pm)/i; @@ -85,7 +85,7 @@ function updateDateTime(node) { return true; } -function updateDate(node) { +function tryUpdateDate(node) { const REG_DATE = /(\d{2}(\/|\.)){2}\d{4}/g; let hits = Array.from(node.textContent.matchAll(REG_DATE), match => match[0]); @@ -105,7 +105,7 @@ function updateDate(node) { return true; } -function updateTime(node) { +function tryUpdateTime(node) { const REG_TIME = /\d?\d:\d{2}( (AM|PM))?/g; const REG_AMPM = /\s(am|pm)/i; @@ -156,7 +156,7 @@ function updateTime(node) { return true; } -function updateTimeZone(node) { +function tryUpdateTimeZone(node) { const HINT_UTC = 'UTC+1'; if (node.textContent === HINT_UTC) { let tzMeta = spacetime().timezone(); @@ -175,21 +175,24 @@ function updateTimestamps(node) { return; } - if (updateDateTime(node)) { + if (tryUpdateDateTime(node)) { __alteredNodes.push(node); return; } - if (updateDate(node)) { + if (tryUpdateDate(node)) { __alteredNodes.push(node); return; } - if (updateTime(node)) { + if (tryUpdateTime(node)) { __alteredNodes.push(node); return; } - updateTimeZone(node); + if (tryUpdateTimeZone(node)) { + __alteredNodes.push(node); + return; + } }); } \ No newline at end of file -- 2.43.4 From a1745d7c6507c7866681853efcaa78eb1cd388ce Mon Sep 17 00:00:00 2001 From: Serraniel Date: Mon, 28 Dec 2020 17:19:18 +0100 Subject: [PATCH 10/14] #147 removed console logs --- src/javascript/enhancements/timeConversion.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/javascript/enhancements/timeConversion.js b/src/javascript/enhancements/timeConversion.js index df1aec9..eb8cbdc 100644 --- a/src/javascript/enhancements/timeConversion.js +++ b/src/javascript/enhancements/timeConversion.js @@ -146,10 +146,6 @@ function tryUpdateTime(node) { datetime = datetime.goto(spacetime().tz); let replaceText = datetime.format(getSpaceTimeFormat(use24Format)); - console.log(node.textContent); - console.log(processedStr); - console.log(replaceText); - console.log('----------') node.textContent = node.textContent.replace(hit, replaceText); }); @@ -160,7 +156,6 @@ function tryUpdateTimeZone(node) { const HINT_UTC = 'UTC+1'; if (node.textContent === HINT_UTC) { let tzMeta = spacetime().timezone(); - console.log(tzMeta); node.textContent = `${tzMeta.name} (UTC${tzMeta.current.offset >= 0 ? '+' : ''}${tzMeta.current.offset})`; } -- 2.43.4 From 4ee9b0d651ae5bca0d4c6ec4859399880ea9e51c Mon Sep 17 00:00:00 2001 From: Serraniel Date: Mon, 28 Dec 2020 18:15:48 +0100 Subject: [PATCH 11/14] #147 Special treatment for airing times --- src/javascript/enhancements/timeConversion.js | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/javascript/enhancements/timeConversion.js b/src/javascript/enhancements/timeConversion.js index eb8cbdc..546ec4f 100644 --- a/src/javascript/enhancements/timeConversion.js +++ b/src/javascript/enhancements/timeConversion.js @@ -4,6 +4,7 @@ import * as core from '../utils/aniwatchCore'; import * as helper from '../utils/helpers'; const __alteredNodes = []; +const DAYS = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']; export function init() { getGlobalConfiguration().getProperty(SETTINGS_websiteAutoTimeConversion, value => { @@ -147,6 +148,33 @@ function tryUpdateTime(node) { let replaceText = datetime.format(getSpaceTimeFormat(use24Format)); node.textContent = node.textContent.replace(hit, replaceText); + + // SPECIAL CASE: Anime has the day written in broadcast bade. This may be different in another timezone + let tzMeta = spacetime().timezone(); + let originalH = datetime.hour() - tzMeta.current.offset + 1; + + let dOffset = 0; + // we moved to next day + if (originalH < 0) { + dOffset = 1; + } + // we moved to previous day + else if (originalH > 24) { + dOffset = -1; + } + + // if day changed + if (dOffset != 0) { + let dayNode = node.parentNode.previousElementSibling; + if (helper.assigned(dayNode)) { + for (let i = 0; i < DAYS.length; i++) { + if (dayNode.textContent.indexOf(DAYS[i]) >= 0) { + dayNode.textContent = dayNode.textContent.replace(DAYS[i], DAYS[(i + DAYS.length + dOffset) % DAYS.length]); // add DAYS.length to avoid negative numbers in the modulo operation + break; + } + } + } + } }); return true; -- 2.43.4 From 56d8b718b3fa1004bb6011103a3e609d91a58072 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Mon, 28 Dec 2020 18:37:19 +0100 Subject: [PATCH 12/14] #147 Changed regex pattern to not change anything on the airing page --- src/javascript/enhancements/timeConversion.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/javascript/enhancements/timeConversion.js b/src/javascript/enhancements/timeConversion.js index 546ec4f..4dacdd6 100644 --- a/src/javascript/enhancements/timeConversion.js +++ b/src/javascript/enhancements/timeConversion.js @@ -11,15 +11,15 @@ export function init() { if (value) { core.runAfterLoad(() => { updateTimestamps(document.documentElement); - }, ".*"); + }, "^/(?!airing).*$"); core.runAfterLocationChange(() => { updateTimestamps(document.documentElement); - }, ".*"); + }, "^/(?!airing).*$"); core.registerScript(node => { updateTimestamps(node); - }, ".*"); + }, "^/(?!airing).*$"); } }); } -- 2.43.4 From 8d0fb9c94c385cf5cbd4f10614c556dfa37e6f3a Mon Sep 17 00:00:00 2001 From: Serraniel Date: Mon, 28 Dec 2020 18:38:49 +0100 Subject: [PATCH 13/14] #147 Added comment --- src/javascript/enhancements/timeConversion.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/javascript/enhancements/timeConversion.js b/src/javascript/enhancements/timeConversion.js index 4dacdd6..a479759 100644 --- a/src/javascript/enhancements/timeConversion.js +++ b/src/javascript/enhancements/timeConversion.js @@ -9,6 +9,9 @@ const DAYS = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday' export function init() { getGlobalConfiguration().getProperty(SETTINGS_websiteAutoTimeConversion, value => { if (value) { + // The regexp pattern matches anything except the airing page. + // This is because we would have to restructure the complete site to update time data. + // Additionally, there is a big hint that all data would be UTC+1 core.runAfterLoad(() => { updateTimestamps(document.documentElement); }, "^/(?!airing).*$"); -- 2.43.4 From 59325c4f7664678b8a636706496eec84e6b02b29 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Mon, 28 Dec 2020 18:44:48 +0100 Subject: [PATCH 14/14] #147 Removed moment This was accidentally added with 816ef34a65eb29fbefc3b6068834f02a04bffe16 --- package-lock.json | 6 ------ package.json | 1 - 2 files changed, 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7866175..25847f7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3351,7 +3351,6 @@ "version": "1.5.4", "resolved": "https://registry.npmjs.org/color-string/-/color-string-1.5.4.tgz", "integrity": "sha512-57yF5yt8Xa3czSEW1jfQDE79Idk0+AkN/4KWad6tbdxUmAs3MvjxlWSWD4deYytcRfoZ9nhKyFl1kj5tBvidbw==", - "dev": true, "requires": { "color-name": "^1.0.0", "simple-swizzle": "^0.2.2" @@ -8433,11 +8432,6 @@ "xtend": "^4.0.0" } }, - "moment": { - "version": "2.29.1", - "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz", - "integrity": "sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==" - }, "mozjpeg": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/mozjpeg/-/mozjpeg-6.0.1.tgz", diff --git a/package.json b/package.json index 37eaffc..20875fd 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,6 @@ "homepage": "https://github.com/Serraniel/AniwatchPlus#readme", "dependencies": { "color": "^3.1.3", - "moment": "^2.29.1", "spacetime": "^6.12.2", "uuid": "^8.3.2" }, -- 2.43.4