-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbookmarklet.js
68 lines (56 loc) · 1.62 KB
/
bookmarklet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
javascript: (() => {
const datetimeReg = /\d\d\d\d-\d\d-\d\d(T\d\d:\d\d:\d\d)?/;
const selection = window.getSelection();
const range = selection.getRangeAt(0);
let container = range.commonAncestorContainer;
if (container.nodeType === 3) {
container = container.parentNode;
}
const links = container.querySelectorAll(
'a[href^="https://github.com/"][href*="/blob/"]'
);
function findDatetimeLastEdit(node) {
const timeEl = node.querySelector('.js-comment-edit-history menu relative-time');
if (timeEl) {
return timeEl.datetime;
}
if (node === document.body) {
return;
}
return findDatetimeLastEdit(node.parentNode);
}
function findDatetime(node) {
const timeEl = node.querySelector('relative-time');
if (timeEl) {
return timeEl.datetime;
}
if (node === document.body) {
return;
}
return findDatetime(node.parentNode);
}
function findDateTimeFallback(node) {
const fromText = datetimeReg.exec(node.innerText);
if (fromText) {
return fromText[0];
}
for (const { value } of node.attributes) {
const fromAttr = datetimeReg.exec(value);
if (fromAttr) {
return fromAttr[0];
}
}
if (node === document.body) {
return;
}
return findDateTimeFallback(node.parentNode);
}
const link = links[0] || container;
if (!link) {
return;
}
const date = findDatetimeLastEdit(link) || findDatetime(link) || findDateTimeFallback(link) || '2023-11-02T18:59:25Z';
window.open(
`https://inwerpsel.github.io/permalinkify?autoopen=1&date=${date}&url=${link.href}`
);
})();