Skip to content

Commit

Permalink
handle changes to node href attr
Browse files Browse the repository at this point in the history
  • Loading branch information
kishore881 committed Jan 8, 2024
1 parent bb697e1 commit 05e6333
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 24 deletions.
6 changes: 4 additions & 2 deletions source/js/fixer.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ class Fixer {
for (const target of this.targets) {
for (const node of this.getNodes(target)) {
try {
// eslint-disable-next-line no-new
new HoverPopup(target.attachTo(node), target.timestamp(node), target.label ?? 'post', target.url(node));
const popup = new HoverPopup(target.attachTo(node), target.timestamp(node), target.label ?? 'post', target.url(node));
if (target.observe) {
target.observe(node, popup);
}
} catch (error) {
console.error('failed to process node:', error, node);
} finally {
Expand Down
59 changes: 37 additions & 22 deletions source/js/timezone-fixers/web-archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,48 @@ import Fixer from '../fixer.js';
const fixer = new Fixer('WayBackMachine', [
{
name: 'Time of Crawl',
selector: 'div.captures-range-info a[href^="/web/"], a.capture-link[href^="/web/"], a.snapshot-link[href^="/web/"]',
selector: 'div.captures-range-info a[href^="/web/"], a.snapshot-link[href^="/web/"]',
attachTo: node => node,
timestamp(node) {
// Href = "/web/20230304105925/example.com"
const timestamp = node.getAttribute('href').match(/\/web\/(\d+)\//);
if (timestamp && timestamp[1]) {
return parseWayBackMachineDateString(timestamp[1]);
}

return null;
},
url(node) {
// `https://web.archive.org/web/20230304105925/example.com`
return `https://web.archive.org${node.getAttribute('href')}`;
},
timestamp: getTimestampFromHref,
url: getResourceUrlFromHref,
label: 'snapshot',
},
{
name: 'Last Hovered Time of Crawl',
selector: 'a.capture-link[href^="/web/"]',
attachTo: node => node,
timestamp: getTimestampFromHref,
url: getResourceUrlFromHref,
label: 'snapshot',
observe(node, popup) {
const observer = new MutationObserver(mutationsList => {
// If href attribute changed, update timestamp and resourceUrl in the popup
for (const mutation of mutationsList) {
if (mutation.type === 'attributes' && mutation.attributeName === 'href') {
popup.moment = getTimestampFromHref(node);
popup.resourceUrl = `https://web.archive.org${node.getAttribute('href')}`;
}
}
});
observer.observe(node, {attributes: true});
},
},
]);

fixer.start();

const parseWayBackMachineDateString = dateString => {
// DateString = "20230304105925" in UTC
if (typeof dateString !== 'string' || !/^\d{14}$/.test(dateString)) {
return null;
function getTimestampFromHref(node) {
// Href = "/web/20230304105925/example.com"
const timestamp = node.getAttribute('href').match(/\/web\/(\d+)\//);
if (timestamp && timestamp[1] && /^\d{14}$/.test(timestamp[1])) {
return moment.utc(timestamp[1], 'YYYYMMDDHHmmss');
}

return moment.utc(dateString, 'YYYYMMDDHHmmss');
};
return null;
}

function getResourceUrlFromHref(node) {
// Href = "/web/20230304105925/example.com"
// ResourceUrl = 'https://web.archive.org/web/20230304105925/example.com'
return `https://web.archive.org${node.getAttribute('href')}`;
}

fixer.start();

0 comments on commit 05e6333

Please sign in to comment.