forked from Xatta-Trone/medium-parser-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
204 lines (180 loc) · 5.82 KB
/
app.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/** @format */
console.log("Medium parser loaded");
const ignoreURLs = [
"/me/lists",
"/me/lists/saved",
"/me/list/highlights",
"/me/lists/reading-history",
"/me/stories/public",
"/me/stories/responses",
"/me/stories/drafts",
"/me/stats",
"/me/settings",
"/me/following",
"/me/settings",
"/me/settings/publishing",
"/me/settings/notifications",
"/me/settings/membership",
"/me/settings/security",
"/me/notifications",
"/plans",
"/mastodon",
"/verified-authors",
"/partner-program",
"/gift-plans",
"/new-story",
"/m/signin",
];
function init() {
if (checkIfGoogleWebCache()) {
return formatGoogleWebCache();
}
checkIfItIsMediumBlog();
}
init();
// checks if the page is google web cache and referred by this extension
function checkIfGoogleWebCache() {
console.log(
"Checking if this site is google webcache and referred by medium-parser extension"
);
const url = new URL(document.URL);
if (
url.hostname == "webcache.googleusercontent.com" &&
url.searchParams.has("referer", "medium-parser") &&
url.searchParams.has("vwsrc", "1")
) {
console.log("Hooray !!! It is referred by medium-parser extension");
return true;
}
console.log("Nah !!! It is not referred by medium-parser extension");
return false;
}
// if it is a medium blog then run the script
function checkIfItIsMediumBlog() {
const e = /https?:\/\/cdn-(?:static|client)(?:-\d)?\.medium\.com\//;
[...document.querySelectorAll("script")].filter((r) => e.test(r.src)).length >
0 || e.test(window.location.hostname)
? console.log("Is a medium blog !", handleURLChange())
: console.log("Not a medium blog :( ");
}
// handle URL change
function handleURLChange() {
let previousUrl = "";
let observer = new MutationObserver(function (mutations) {
if (window.location.href !== previousUrl) {
previousUrl = window.location.href;
console.log(`URL data changed to ${window.location.href}`);
runMedium(location.href);
}
});
const config = { attributes: true, childList: true, subtree: true };
observer.observe(document, config);
}
function runMedium(url) {
// check the url
const u = new URL(url);
// check if it is a page
const root = document.getElementById("root");
root.style.position = "relative";
if (
ignoreURLs.indexOf(u.pathname) == -1 &&
u.pathname.split("/").filter((e) => e).length >= 1
) {
// get the title
var leftDiv = document.createElement("div"); //Create left div
leftDiv.id = "medium-parser"; //Assign div id
leftDiv.setAttribute(
"style",
"position:absolute;z-index:1;top:150px;right:150px;"
); //Set div attributes
a = document.createElement("a");
a.href = `http://webcache.googleusercontent.com/search?q=cache:${url}&strip=0&vwsrc=1&referer=medium-parser`; // Instead of calling setAttribute
a.innerHTML = "Read from Google Cache";
a.setAttribute(
"style",
"padding:14px 25px; color:white; background: #242424; display:block;text-align:center;"
); //Set div attributes
a.setAttribute("target", "_blank"); //Set div attributes
archive = document.createElement("a");
archive.href = `https://archive.is?url=${url}&run=1&referer=medium-parser`; // Instead of calling setAttribute
archive.innerHTML = "Read from Archive.is";
archive.setAttribute(
"style",
"padding:14px 25px; color:white; background: #242424; display:block; margin-top:10px;text-align:center;"
); //Set div attributes
archive.setAttribute("target", "_blank"); //Set div attributes
leftDiv.appendChild(a); // Append the link to the div
leftDiv.appendChild(archive);
root.appendChild(leftDiv); // A
} else {
// remove the element
const el = document.getElementById("medium-parser");
if (el != undefined || el != null) {
el.remove();
}
}
}
// run the main script
function runMediumScript(url) {
// check the url
const u = new URL(url);
// console.log(u);
// check if it is a page
const root = document.getElementById("root");
root.style.position = "relative";
if (u.pathname.split("/").filter((e) => e).length >= 1) {
var leftDiv = document.createElement("div"); //Create left div
leftDiv.id = "medium-parser"; //Assign div id
leftDiv.setAttribute(
"style",
"position:absolute;z-index:9999999;top:150px;right:150px;"
); //Set div attributes
a = document.createElement("a");
a.href = `https://medium-parser.vercel.app/?url=${url}`; // Instead of calling setAttribute
a.innerHTML = "Read full article"; // <a>INNER_TEXT</a>
a.setAttribute(
"style",
"padding:10px 25px; color:white; background: #2c3e50; display:inline-block;"
); //Set div attributes
a.setAttribute("target", "_blank"); //Set div attributes
leftDiv.appendChild(a); // Append the link to the div
root.appendChild(leftDiv); // A
} else {
// remove the element
const el = document.getElementById("medium-parser");
if (el != undefined || el != null) {
el.remove();
}
}
}
// format google webcache
function formatGoogleWebCache() {
const contents = htmlDecode(
document.querySelector("body > div > pre").innerHTML
);
const title = getTitle(contents);
document.body.innerHTML = contents;
document.title = "Medium parser - " + title;
}
function htmlDecode(rawContent) {
var entities = {
"&": "&",
"<": "<",
">": ">",
""": '"',
"'": "'",
};
for (var prop in entities) {
if (entities.hasOwnProperty(prop)) {
rawContent = rawContent.replace(new RegExp(prop, "g"), entities[prop]);
}
}
return rawContent;
}
function getTitle(rawContent) {
start = '<title data-rh="true">';
end = "</title>";
var startPos = rawContent.indexOf(start) + start.length;
var endPos = rawContent.indexOf(end);
return rawContent.substring(startPos, endPos).trim();
}