Last active
September 29, 2021 11:24
-
-
Save noromanba/76a3d7791cf6eaf1c94c to your computer and use it in GitHub Desktop.
remove t.umblr.com redirector from links on Tumblr for UserScript/Content Scripts
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 t.um block | |
// @namespace http://noromanba.flavors.me | |
// @description remove t.umblr.com redirector from links on Tumblr for UserScript/Content Scripts | |
// @include http://*.tld/* | |
// @include https://*.tumblr.com/* | |
// @grant none | |
// @noframes | |
// @run-at document-body | |
// @version 2018.2.12.0 | |
// @homepage https://gist.github.com/noromanba/76a3d7791cf6eaf1c94c | |
// @downloadURL https://gist.github.com/noromanba/76a3d7791cf6eaf1c94c/raw/t-um-block.user.js | |
// @license CC0 Univ PD https://creativecommons.org/publicdomain/zero/1.0/legalcode | |
// @author noromanba https://noromanba.github.com | |
// @icon https://upload.wikimedia.org/wikipedia/commons/thumb/3/38/WHMIS_Class_D-2.svg/256px-WHMIS_Class_D-2.svg.png | |
// ==/UserScript== | |
// Icon (PD by Silsor/Health Canada) | |
// https://commons.wikimedia.org/wiki/File%3AWHMIS_Class_D-2.svg | |
// Bookmaklet | |
// http://let.hatelabo.jp/noromanba/let/hLHU2KbCtZgI | |
// Devel | |
// https://gist.github.com/noromanba/76a3d7791cf6eaf1c94c | |
// c.f. | |
// http://ptech.g.hatena.ne.jp/noromanba/20160208/1454965267 | |
// https://stackoverflow.com/questions/35023389/tumblr-injecting-new-code-into-my-links | |
// e.g. | |
// https://yahoo.tumblr.com | |
// http://blog.qiita.com | |
(() => { | |
'use strict'; | |
// Tumblr detection | |
if (!document.head || !document.head.querySelector([ | |
'script[src*="//assets.tumblr.com/assets/scripts/pre_tumblelog.js"]', | |
])) { | |
return; | |
} | |
const detox = (ctx) => { | |
if (!ctx.querySelectorAll) return; | |
ctx.querySelectorAll([ | |
'a', | |
'area', | |
].map(tag => tag + '[href^="https://t.umblr.com/redirect?z="]')) | |
.forEach(link => { | |
// redirector syntax; hash endsWith "%3D%3D" i.e. "==" Base64 padding | |
// https://t.umblr.com/redirect?z=<ENCODED_URL>&t=<72_DIGIT_HASH> | |
const url = (new URL(link.href)).searchParams.get('z'); | |
if (!url) return; | |
link.href = decodeURIComponent(url); | |
}); | |
}; | |
detox(document.body); | |
new MutationObserver(records => { | |
records.forEach(record => { | |
detox(record.target); | |
}); | |
}).observe(document.body, { childList: true, subtree: true }); | |
})(); | |
// Tumblr has https and custom-domain e.g. | |
// https://yahoo.tumblr.com | |
// http://blog.qiita.com | |
// | |
// methods of Tumblr detection | |
// | |
// -[ ] og:url | optional and nullable | |
// <meta content="" property="og:url" /> | |
// -[ ] canonical | custom-domain url | |
// <link rel="canonical" href="https://yahoo.tumblr.com" /> | |
// <link rel="canonical" href="http://blog.qiita.com" /> | |
// -[-] shortcut icon | probably ok, but nullable | |
// <link href="http://38.media.tumblr.com/avatar_ee011965a9a8_128.png" rel="shortcut icon" /> | |
// TBD ~= whitespace-separated serector; "[rel~="shortcut"], [rel~="icon"]" | |
// document.head.querySelector('link[rel="shortcut icon"][href]'); | |
// -[x] pre_tumblelog.js | absolutely ok | |
// DBG | |
/* pseudo Array#uniq() | |
Array.from(document.querySelectorAll([ | |
'[href^="http://t.umblr.com/redirect?z="]', | |
'[src^="http://t.umblr.com/redirect?z="]', | |
]), link => link.tagName).filter((val, idx, ary) => { | |
return ary.indexOf(val) === idx; | |
}); | |
*/ | |
// | |
// `searchParams` is limited, recommended to use w/ `URL` interface | |
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/searchParams | |
// https://bugzilla.mozilla.org/show_bug.cgi?id=1213815 | |
// https://developer.mozilla.org/en-US/docs/Web/API/URL/searchParams | |
// https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment