Created
June 10, 2022 14:19
-
-
Save lo48576/c0b722bcd63a22833fd5ec75443f0558 to your computer and use it in GitHub Desktop.
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 auto redirect | |
// @author NOP Thread <[email protected]> | |
// @version 0.0.4 | |
// @include https://pbs.twimg.com/* | |
// @include https://twitter.com/* | |
// @include https://mobile.twitter.com/* | |
// @include https://ja.m.wikipedia.org/* | |
// @include http://d.hatena.ne.jp/* | |
// @include https://d.hatena.ne.jp/* | |
// @include https://www.google.co.jp/* | |
// @include https://www.google.com/* | |
// @include https://www.amazon.co.jp/* | |
// @include https://datatracker.ietf.org/doc/html/* | |
// @grant none | |
// @run-at document-start | |
// ==/UserScript== | |
'use strict'; | |
class Redirector { | |
constructor(l = location) { | |
this.protocol = l.protocol; | |
this.host = l.host; | |
this.pathname = l.pathname; | |
this.search = l.search; | |
this.hash = l.hash; | |
} | |
redirect() { | |
location.replace(`${this.protocol}//${this.host}${this.pathname}${this.search}${this.hash}`); | |
} | |
with_protocol(protocol) { | |
this.protocol = protocol; | |
return this; | |
} | |
with_host(host) { | |
this.host = host; | |
return this; | |
} | |
with_pathname(pathname) { | |
this.pathname = pathname; | |
return this; | |
} | |
with_search(search) { | |
this.search = search; | |
return this; | |
} | |
with_hash(hash) { | |
this.hash = hash; | |
return this; | |
} | |
} | |
const host_map = new Map([ | |
['ja.m.wikipedia.org', 'ja.wikipedia.org'], | |
['mobile.twitter.com', 'twitter.com'], | |
]); | |
(function() { | |
if(host_map.has(location.hostname)) { | |
location.hostname = host_map.get(location.hostname); | |
return; | |
} | |
switch(location.hostname) { | |
case 'd.hatena.ne.jp': | |
escape_hatena(); | |
return; | |
case 'datatracker.ietf.org': | |
redirect_ietf_rfc(); | |
return; | |
case 'pbs.twimg.com': | |
redirect_pbs_twimg(); | |
return; | |
case 'twitter.com': | |
redirect_twitter(); | |
return; | |
case 'www.amazon.co.jp': | |
redirect_amazon(); | |
return; | |
case 'www.google.co.jp': | |
case 'www.google.com': | |
redirect_google(); | |
return; | |
} | |
})(); | |
function escape_hatena() { | |
const pat = /^(\/[^/]+)\/touch(\/.*)$/; | |
const matches = pat.exec(location.pathname); | |
if(matches) { | |
new Redirector().with_pathname(matches[1] + matches[2]).redirect(); | |
} | |
} | |
function redirect_pbs_twimg() { | |
const params = new URL(location).searchParams; | |
params.set("name", "orig"); | |
const params_str = `?${params.toString()}`; | |
if(params_str !== location.search && params_str !== '?') { | |
new Redirector().with_search(params_str).redirect(); | |
} | |
} | |
function redirect_google() { | |
if(location.pathname === '/url') { | |
redirect_google_redirector(); | |
} else { | |
redirect_google_search(); | |
} | |
} | |
function redirect_google_search() { | |
// See <http://webos-goodies.jp/archives/50785287.html>. | |
const params = new URL(location).searchParams; | |
/* | |
const whitelist = [ | |
// `q`: query. | |
"q", | |
// `hq`: hidden query. | |
"hq", | |
// `tbm`: search target? | |
"tbm", | |
// `tbs`: image license. | |
"tbs", | |
]; | |
*/ | |
const blacklist = [ | |
// Unknown. Display related? | |
"dpr", | |
// Browser height? | |
"bih", | |
// Browser width? | |
"biw", | |
// Unknown. | |
"ved", | |
// Unknown. | |
"sa", | |
// How the search is started. | |
"source", | |
// Unknown. Geolocation? | |
"gs_l", | |
// Unknown. | |
"gs_lcp", | |
// Unknown. | |
"ei", | |
// Old query. | |
"oq", | |
// Browser (client). | |
"client", | |
// Unknown. | |
"sclient", | |
]; | |
blacklist.forEach((x) => params.delete(x)); | |
params.sort(); | |
const params_str = `?${params.toString()}`; | |
if(params_str !== location.search && params_str !== '?') { | |
new Redirector().with_search(params_str).redirect(); | |
} | |
} | |
function redirect_google_redirector() { | |
const params = new URL(location).searchParams; | |
const target_encoded = params.get("url"); | |
if(target_encoded) { | |
const target = decodeURIComponent(target_encoded.replace('+', ' ')); | |
location.replace(target); | |
} | |
} | |
function redirect_amazon() { | |
const pat = /\/[^/]+\/dp\/(.*)$/; | |
const matches = pat.exec(location.pathname); | |
if(matches) { | |
new Redirector().with_pathname(`/dp/${matches[1]}`).redirect(); | |
} | |
} | |
function redirect_twitter() { | |
const pat = /^\/[^/]+\/status\//; | |
const matches = pat.exec(location.pathname); | |
if(matches && location.search) { | |
new Redirector().with_search("").redirect(); | |
} | |
} | |
function redirect_ietf_rfc() { | |
const pat = /^https:\/\/datatracker.ietf.org\/doc\/html\/rfc([0-9]+)/; | |
const matches = pat.exec(location.href); | |
if(matches) { | |
location.replace(`https://www.rfc-editor.org/rfc/rfc${matches[1]}.html`); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment