Skip to content

Commit

Permalink
Merge pull request #3 from Xatta-Trone/add-archive.is
Browse files Browse the repository at this point in the history
Add archive.is
  • Loading branch information
Xatta-Trone authored Sep 26, 2023
2 parents 4dc3d76 + 5c2cd8e commit c342d06
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 10 deletions.
117 changes: 108 additions & 9 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,34 @@
/** @format */
console.log("Medium parser loaded");
console.log("check medium start");
checkIfItIsMediumBlog();
console.log("check medium end");

function init() {
if (checkIfGoogleWebCache()) {
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() {
Expand All @@ -17,20 +43,57 @@ function checkIfItIsMediumBlog() {
function handleURLChange() {
let previousUrl = "";
let observer = new MutationObserver(function (mutations) {
if (location.href !== previousUrl) {
previousUrl = location.href;
console.log(`URL data changed to ${location.href}`);
runScript(location.href);
// your code here
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 (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:9999999;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 full article"; // <a>INNER_TEXT</a>
a.setAttribute(
"style",
"padding:10px 25px; color:white; background: #2c3e50; display: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();
}
}
}

// run the main script
function runScript(url) {
function runMediumScript(url) {
// check the url
const u = new URL(url);
// console.log(u);
Expand Down Expand Up @@ -64,3 +127,39 @@ function runScript(url) {
}
}
}

// 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 = {
"&amp;": "&",
"&lt;": "<",
"&gt;": ">",
"&quot;": '"',
"&#39;": "'",
};

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();
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "Medium parser",
"version": "1.1.0",
"version": "1.1.1",
"description": "Unlocks the whole medium article on the go.",
"icons": {
"16": "img/icon16.png",
Expand Down

0 comments on commit c342d06

Please sign in to comment.