Last active
July 9, 2021 06:01
-
-
Save kobachi/1bf7540f6b21dd3b73971175f8b376cc to your computer and use it in GitHub Desktop.
Pocket Direct Linkify
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Pocket Direct Linkify | |
// @description Rewrite Pocket item link to directly opens original website | |
// @namespace kobachi.dev | |
// @version 2.2 | |
// @include https://getpocket.com/my-list | |
// ==/UserScript== | |
(function() { | |
function simplify(articles) { | |
articles.forEach((a) => { | |
var simplified = null; | |
var redirects = a.querySelectorAll('a[href*="/redirect?url="]'); | |
for (var i = 0; i < redirects.length; i++) { | |
var link = redirects[i]; | |
simplified = strip(decodeURIComponent(link.href.replace(/.+\/redirect\?url=/, ""))); | |
// console.log("Simplified search (pass 1): result = " + simplified); | |
if (simplified != null) { | |
rewrite(link, simplified); | |
} | |
} | |
if (simplified == null) { | |
var directs = a.querySelectorAll('a[href^="http://"], a[href^="https://"]') | |
if (directs == null || directs.length == 0) { | |
return; | |
} | |
simplified = strip(directs[0].href); | |
// console.log("Simplified search (pass 2): result = " + simplified); | |
} | |
if (simplified == null) { | |
return; | |
} | |
var reads = a.querySelectorAll('a[href^="/read/"]'); | |
for (var j = 0; j < reads.length; j++) { | |
rewrite(reads[j], simplified); | |
} | |
}); | |
} | |
function strip(href) { | |
var u = new URL(href); | |
if (u.search.length == 0) { | |
return href; | |
} | |
var entries = u.search.substring(1).split("&"); | |
for (var i = entries.length - 1; 0 <= i; i--) { | |
if (/utm_.*/.test(entries[i])) { | |
entries.splice(i, 1); | |
} | |
} | |
if (entries.length == 0) { | |
u.search = ""; | |
} else { | |
u.search = "?" + entries.join("&"); | |
} | |
return u.toString(); | |
} | |
function rewrite(element, href) { | |
element.setAttribute("href", href); | |
element.setAttribute("rel", "noopener noreferrer"); | |
element.onclick = (e) => { | |
e.preventDefault(); | |
location.href = e.target.href; | |
return true; | |
}; | |
} | |
simplify(document.body.querySelectorAll('article')); | |
new MutationObserver((mutations) => { | |
var articles = mutations.filter((m) => { return m.type == "childList" }) | |
.flatMap(m => { return Array.from(m.addedNodes) }) | |
.filter(n => n instanceof Element) | |
.flatMap(e => { | |
if (e.tagName == "ARTICLE") { | |
return [e]; | |
} else { | |
return Array.from(e.querySelectorAll("article")); | |
} | |
}); | |
if (articles.length == 0) { | |
return; | |
} | |
simplify(articles); | |
}).observe(document.body, { childList: true, subtree: true }); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use this link to install this user script.