-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent.js
29 lines (25 loc) · 1.18 KB
/
content.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
const arxivLinkPattern = /^https?:\/\/(www\.)?arxiv\.org\/(abs|pdf)\/(.*)$/;
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.message === "getArxivLinks") {
// Get all <a> elements on the page
const links = document.getElementsByTagName("a");
let arxivLinks = {};
for (let link of links) {
if (arxivLinkPattern.test(link.href)) {
let modifiedLink = link.href.replace('/pdf/', '/abs/');
modifiedLink = modifiedLink.replace('.pdf', '');
// Add the link object to the arxivLinks object
// If the URL is already in the object, it will simply overwrite the existing entry
arxivLinks[modifiedLink] = {
link: modifiedLink,
position: link.getBoundingClientRect().top + window.scrollY,
};
}
}
// Convert the Object to an Array
arxivLinks = Object.values(arxivLinks);
sendResponse({ arxivLinks: arxivLinks });
} else if (request.message === "scrollToLink") {
window.scrollTo({ top: request.position - 50, behavior: 'smooth' });
}
});