-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-article-details-medium-profile.js
56 lines (47 loc) · 1.49 KB
/
get-article-details-medium-profile.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
// At this point in time it only works with the homepage
// the paginated example has not been tested, and should probably be done manually
(() => {
/**
* custom domain would be username.medium.com, not any other fancy stuff,
* at the moment at least
*
* @param {string} link
* @returns {boolean}
*/
const isCustomDomain = (link) => !link.includes("/medium.com");
/**
* @param {string} link
*/
const parseLink = (link) => {
if (!link) {
return;
}
const canonicalId = link.split("?")[0].split("-").at(-1);
let domain;
if (isCustomDomain()) {
domain = link.split(".com")[0] + ".com";
} else {
const [mediumDomain, rawAuthorName] = link.split("@");
const authorName = `@${rawAuthorName.split("/")[0]}`;
domain = mediumDomain + authorName;
}
return [domain, canonicalId].join("/");
};
const candidateArticles = Array.from(document.querySelectorAll("h2"));
const articles = candidateArticles.slice(0, -1);
const articleDetails = articles.map((article, index) => {
console.log({ article });
return [
index,
{
title: article.innerText.trim(),
link: parseLink(article.parentElement.parentElement.href),
},
];
});
const sortedArticleDetails = articleDetails.sort(([a], [b]) => a + b);
const printableArticleDetails = sortedArticleDetails
.map(([, { title, link }]) => [title, link].join("\n"))
.join("\n".repeat(2));
console.log(printableArticleDetails);
})();