Skip to content

Commit 40ba294

Browse files
committed
Refactor article link handling to use Freedium
URL.
1 parent e7dc3f6 commit 40ba294

1 file changed

Lines changed: 24 additions & 33 deletions

File tree

chromium/content.js

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,31 @@
1-
const twelveFootDomain = "https://12ft.io/proxy?q=";
1+
const freediumUrl = "https://freedium.cfd/";
22
const mediumDomain = "https://medium.com";
33

4-
const add12ftToMediumArticleURL = (url) => {
5-
if (!url.includes(twelveFootDomain)) {
6-
const urlWith12ft = twelveFootDomain + encodeURIComponent(mediumDomain + url);
7-
return urlWith12ft;
8-
}
9-
return url;
10-
};
4+
function handleNewArticles(mutationsList) {
5+
mutationsList.forEach((mutation) => {
6+
if (mutation.type === "childList") {
7+
mutation.addedNodes.forEach((node) => {
8+
if (node.tagName === "ARTICLE") {
9+
const links = node.querySelectorAll('div[class^="iq"] a');
1110

12-
const modifyEachArticleLink = () => {
13-
const links = document.querySelectorAll('a[aria-label="Post Preview Title"]');
14-
links.forEach((link) => {
15-
const originalURL = link.getAttribute("href");
16-
const modifiedURL = add12ftToMediumArticleURL(originalURL);
17-
link.setAttribute("href", modifiedURL);
11+
links.forEach((link) => {
12+
const originalURL = link.getAttribute("href");
13+
const modifiedURL = freediumUrl + originalURL;
1814

19-
// Prevent the navigation to Medium's detail page, and instead
20-
// navigate to the 12ft.io proxy page
21-
link.addEventListener("click", (event) => {
22-
event.preventDefault();
23-
window.location.href = modifiedURL;
24-
});
25-
});
26-
};
15+
link.setAttribute("href", modifiedURL);
2716

28-
const observeDOMChanges = (mutationsList, observer) => {
29-
for (const mutation of mutationsList) {
30-
if (mutation.type === "childList") {
31-
modifyEachArticleLink();
17+
link.addEventListener("click", (event) => {
18+
event.preventDefault();
19+
window.open(modifiedURL, "_blank");
20+
});
21+
});
22+
}
23+
});
3224
}
33-
}
34-
};
35-
36-
const observer = new MutationObserver(observeDOMChanges);
25+
});
26+
}
3727

38-
// Start observing the target element for changes
39-
const target = document.querySelector("div.dx.s");
40-
observer.observe(target, { childList: true, subtree: true });
28+
const targetNode = document.body;
29+
const config = { childList: true, subtree: true };
30+
const observer = new MutationObserver(handleNewArticles);
31+
observer.observe(targetNode, config);

0 commit comments

Comments
 (0)