Skip to content

Commit

Permalink
🚧 chrome extension in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
d2FuZ3h1ZG9uZw committed Sep 30, 2021
1 parent ab042e9 commit e6a4986
Show file tree
Hide file tree
Showing 6 changed files with 377 additions and 0 deletions.
13 changes: 13 additions & 0 deletions background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
async function getCurrentTab() {
let queryOptions = { active: true, currentWindow: true }
let [tab] = await chrome.tabs.query(queryOptions)
console.log(tab)

const url = new URL(tab.url)
console.log(url.searchParams.get('target'))
tab.url = url.searchParams.get('target')
}

chrome.tabs.onUpdated.addListener(() => {
getCurrentTab()
})
Empty file added background.ts
Empty file.
315 changes: 315 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,315 @@
// ==UserScript==
// @name Open the F**king URL Right Now
// @description 自动跳转某些网站不希望用户直达的外链
// @author OldPanda
// @match http://t.cn/*
// @match https://weibo.cn/sinaurl?*
// @match https://www.jianshu.com/go-wild?*
// @match http*://link.zhihu.com/?*
// @match https://www.douban.com/link2/?url=*
// @match https://link.ld246.com/forward?goto=*
// @match https://mp.weixin.qq.com/*
// @match http://redir.yy.duowan.com/warning.php?url=*
// @match https://weixin110.qq.com/cgi-bin/mmspamsupport-bin/newredirectconfirmcgi*
// @match https://link.csdn.net/?target=*
// @match https://steamcommunity.com/linkfilter/?url=*
// @match https://game.bilibili.com/linkfilter/?url=*
// @match https://www.oschina.net/action/GoToLink?url=*
// @match https://developers.weixin.qq.com/community/middlepage/href?href=*
// @match https://docs.qq.com/scenario/link.html?url=*
// @match https://www.pixiv.net/jump.php?url=*
// @match https://www.chinaz.com/go.shtml?url=*
// @match http://www.360doc.com/content/*
// @match https://nga.178.com/read.php?*
// @match https://bbs.nga.cn/read.php?*
// @match http*://c.pc.qq.com/*
// @match https://www.yuque.com/r/goto?url=*
// @match https://www.mcbbs.net/plugin.php?id=link_redirect&target=*
// @match https://link.juejin.cn/?target=*
// @match http*://www.360doc.cn/outlink.html?url=*
// @match https://jump2.bdimg.com/safecheck/index?url=*
// @match http*://iphone.myzaker.com/zaker/link.php?*
// @match https://www.tianyancha.com/security?target=*
// @match https://afdian.net/link?target=*
// @match https://mail.qq.com/cgi-bin/readtemplate*
// @match https://link.logonews.cn/?*
// @match https://link.uisdc.com/?redirect=*
// @version 0.15.0
// @run-at document-idle
// @namespace https://old-panda.com/
// @require https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js
// @license GPLv3 License
// ==/UserScript==

const $ = jQuery.noConflict(true)
;(function () {
'use strict'

const curURL = window.location.href

function rstrip(str, regex) {
let i = str.length - 1
while (i >= 0) {
if (!str[i].match(regex)) {
break
}
i--
}
return str.substring(0, i + 1)
}

/**
* Split concatenated URL string into separate URLs.
* @param {String} str
*/
function splitMultiURLs(str) {
let results = new Array()
let entry = ''
while (str.length > 0) {
if (str.indexOf('http:') === -1 && str.indexOf('https:') === -1) {
entry += str
str = ''
results.push(rstrip(entry, /[@:%_\+~#?&=,$^\*]/g))
break
}

if (str.startsWith('http:')) {
entry += 'http:'
str = str.substring('http:'.length)
} else if (str.startsWith('https:')) {
entry += 'https:'
str = str.substring('https:'.length)
} else {
return results
}

let nextIndex = Math.min(
str.indexOf('https:') === -1 ? Number.MAX_SAFE_INTEGER : str.indexOf('https:'),
str.indexOf('http:') === -1 ? Number.MAX_SAFE_INTEGER : str.indexOf('http:')
)
if (nextIndex > 0) {
entry += str.substring(0, nextIndex)
str = str.substring(nextIndex)
}
results.push(rstrip(entry, /[@:%_\+~#?&=,$^\*]/g))
entry = ''
}
return results
}

/**
* Replace url with clickable `<a>` tag in html content.
* @param {String} url
*/
function replaceSingleURL(url) {
$('#js_content').html((_, html) => {
return html.replace(url, `<a target="_blank" rel="noopener noreferrer" href="${url}">${url}</a>`)
})
}

/**
* Make urls clickable again on Weixin Media Platform.
*/
function enableURLs() {
let existingLinks = new Set()
$('a').each(function () {
existingLinks.add(this.href)
})

let content = $('#js_content').text()
let urls = content.matchAll(
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g
)
let replaced = new Set()
for (let value of urls) {
let urlStr = $.trim(value[0])
for (let url of splitMultiURLs(urlStr)) {
if (
!url ||
replaced.has(url) ||
url.includes('localhost') ||
url.includes('127.0.0.1') ||
existingLinks.has(url)
) {
continue
}
if (url.endsWith('.') && url[url.length - 2].match(/\d/g)) {
url = url.substring(0, url.length - 2)
}
replaceSingleURL(url)
replaced.add(url)
}
}
}

function redirect(fakeURLStr, trueURLParam, enableBase64 = false) {
let fakeURL = new URL(fakeURLStr)
let trueURL = fakeURL.searchParams.get(trueURLParam)
if (enableBase64) trueURL = window.atob(trueURL)
if (trueURL.indexOf('http://') !== 0 && trueURL.indexOf('https://') !== 0) {
trueURL = 'https://' + trueURL
}
window.location.replace(trueURL)
}

/**
* @function
* @name match
* @param {...string} patterns
* @description check if current URL matchs given patterns
*/
const match = (...patterns) => patterns.some(p => curURL.includes(p))

/**
* @function
* @name matchRegex
* @param {...string} patterns regex patterns
* @description check if current URL matchs given regex patterns
*/
const matchRegex = (...patterns) => patterns.some(p => curURL.search(p) > -1)

/**
* @enum {string}
* @name fuckers
* @description all link pattern needed deal with
*/
const fuckers = {
weibo: 'http://t.cn/', // 微博网页版
weibo2: 'https://weibo.cn/sinaurl?',
// http://t.cn/RgAKoPE
// https://weibo.cn/sinaurl?u=https%3A%2F%2Fwww.freebsd.org%2F
// https://weibo.cn/sinaurl?toasturl=https%3A%2F%2Ftime.geekbang.org%2F
// https://weibo.cn/sinaurl?luicode=10000011&lfid=230259&u=http%3A%2F%2Ft.cn%2FA6qHeVlf
jianshu: 'https://www.jianshu.com/go-wild?',
zhihu: 'https://link.zhihu.com/?',
zhihu2: 'http://link.zhihu.com/?',
// https://link.zhihu.com/?target=https%3A%2F%2Ftime.geekbang.org%2F
// https://link.zhihu.com/?target=https%3A%2F%2Fwww.freebsd.org%2F
// https://link.zhihu.com/?utm_oi=35221042888704&target=https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/import
douban: 'https://www.douban.com/link2/?url=',
dilian: 'https://link.ld246.com/forward?goto=',
theWorst: 'https://mp.weixin.qq.com/',
theWorst2: 'https://weixin110.qq.com/cgi-bin/mmspamsupport-bin/newredirectconfirmcgi',
yy: 'http://redir.yy.duowan.com/warning.php?url=',
csdn: 'https://link.csdn.net/?target=',
steam: 'https://steamcommunity.com/linkfilter/?url=',
gamebilibili: 'https://game.bilibili.com/linkfilter/?url=',
oschina: 'https://www.oschina.net/action/GoToLink?url=',
weixindev: 'https://developers.weixin.qq.com/community/middlepage/href?href=',
qqdocs: 'https://docs.qq.com/scenario/link.html?url=',
pixiv: 'https://www.pixiv.net/jump.php?url=',
chinaz: 'https://www.chinaz.com/go.shtml?url=',
doc360: 'http://www.360doc.com/content/',
nga: 'https://nga.178.com/read.php?',
nga2: 'https://bbs.nga.cn/read.php?',
qq: 'http://c.pc.qq.com/(middlem|index).html',
qq2: 'https://c.pc.qq.com/(middlem|index).html',
yuque: 'https://www.yuque.com/r/goto?url=',
mcbbs: 'https://www.mcbbs.net/plugin.php?id=link_redirect&target=',
juejin: 'https://link.juejin.cn/?target=',
doc360_2: 'http://www.360doc.cn/outlink.html?url=',
doc360_3: 'https://www.360doc.cn/outlink.html?url=',
tieba: 'https://jump2.bdimg.com/safecheck/index?url=',
zaker: 'http://iphone.myzaker.com/zaker/link.php?',
zaker2: 'https://www.360doc.cn/outlink.html?url=',
tianyancha: 'https://www.tianyancha.com/security?target=',
afdian: 'https://afdian.net/link?target=',
qqmail: 'https://mail.qq.com/cgi-bin/readtemplate',
logonews: 'https://link.logonews.cn/?',
uisdc: 'https://link.uisdc.com/?redirect='
}

$(document).ready(function () {
if (match(fuckers.weibo, fuckers.weibo2)) {
const link = $('.wrap .link').first().text() || document.querySelector('.open-url').children[0].href
window.location.replace(link)
}
if (match(fuckers.jianshu)) {
redirect(curURL, 'url')
}
if (match(fuckers.zhihu, fuckers.zhihu2)) {
redirect(curURL, 'target')
}
if (match(fuckers.douban)) {
redirect(curURL, 'url')
}
if (match(fuckers.dilian)) {
redirect(curURL, 'goto')
}
if (match(fuckers.theWorst)) {
enableURLs()
}
if (match(fuckers.yy)) {
redirect(curURL, 'url')
}
if (match(fuckers.theWorst2)) {
window.location.replace($('.weui-msg__desc').first().text())
}
if (match(fuckers.csdn)) {
redirect(curURL, 'target')
}
if (match(fuckers.steam)) {
redirect(curURL, 'url')
}
if (match(fuckers.gamebilibili)) {
redirect(curURL, 'url')
}
if (match(fuckers.oschina)) {
redirect(curURL, 'url')
}
if (match(fuckers.weixindev)) {
redirect(curURL, 'href')
}
if (match(fuckers.qqdocs)) {
redirect(curURL, 'url')
}
if (match(fuckers.pixiv)) {
redirect(curURL, 'url')
}
if (match(fuckers.chinaz)) {
redirect(curURL, 'url')
}
if (match(fuckers.doc360)) {
$('#articlecontent table tbody tr td#artContent').find('a').off('click')
}
if (match(fuckers.nga, fuckers.nga2)) {
$('#m_posts #m_posts_c a').prop('onclick', null).off('click')
}
if (matchRegex(fuckers.qq, fuckers.qq2)) {
redirect(curURL, 'pfurl')
}
if (match(fuckers.yuque)) {
redirect(curURL, 'url')
}
if (match(fuckers.mcbbs)) {
redirect(curURL, 'target')
}
if (match(fuckers.juejin)) {
redirect(curURL, 'target')
}
if (match(fuckers.doc360_2, fuckers.doc360_3)) {
redirect(curURL, 'url')
}
if (match(fuckers.tieba)) {
window.location.replace(document.getElementsByClassName('btn')[0].getAttribute('href'))
}
if (match(fuckers.zaker, fuckers.zaker2)) {
redirect(curURL, 'b', true)
}
if (match(fuckers.tianyancha)) {
redirect(curURL, 'target')
}
if (match(fuckers.afdian)) {
redirect(curURL, 'target')
}
if (match(fuckers.qqmail)) {
redirect(curURL, 'gourl')
}
if (match(fuckers.logonews)) {
redirect(curURL, 'url')
}
if (match(fuckers.uisdc)) {
redirect(curURL, 'redirect')
}
})
})()
14 changes: 14 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "Fuck Redirect",
"description": "rebuild the superhighway of the information age",
"version": "1.0",
"manifest_version": 3,
"background": {
"service_worker": "background.js",
"type": "module"
},
// "externally_connectable": {
// "matches": ["https://link.juejin.cn/?target=*"]
// }
"permissions": ["tabs"]
}
Empty file added src/index.ts
Empty file.
35 changes: 35 additions & 0 deletions src/util/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const lastIndexOfPro = (str: string, search: string | RegExp) => {
let i = str.length - 1
while (i >= 0) {
if (!str[i].match(search)) {
return i
}
i -= 1
}
return -1
}

const includesPro = (str: string, search: string | RegExp) => str.search(search) >= 0

const substringLastIndex = (str: string, search: string | RegExp) =>
str.substring(0, lastIndexOfPro(str, search) + 1)

// const splitMulti = (str: string) => {
// const results = new Array()
// }

const createUrl = (url: string | URL) => new URL(url)

const isValidUrl = (url: string) => includesPro(url, /^https?:\/\//)

const isInvalidUrl = (url: string) => !isValidUrl(url)

const redirect = (fakeUrl: string | URL, realUrlParam: string) => {
fakeUrl = createUrl(fakeUrl)
let realUrl = fakeUrl.searchParams.get(realUrlParam)
realUrl = decodeURIComponent(realUrl)
if (isInvalidUrl(realUrl)) {
realUrl = 'https://' + realUrl
}
window.location.replace(realUrl)
}

0 comments on commit e6a4986

Please sign in to comment.