-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
156 additions
and
51 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
*.zip | ||
node_modules |
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
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "@bjornstar/intercept-redirect", | ||
"version": "3.0.0", | ||
"description": "Skip tracking redirects that serve no purpose other than to waste your precious time.", | ||
"main": "webextension/index.js", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"tap": "^11.1.4" | ||
}, | ||
"scripts": { | ||
"test": "tap test/*.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/bjornstar/intercept-redirect.git" | ||
}, | ||
"keywords": [ | ||
"webextension", | ||
"redirect" | ||
], | ||
"author": "Bjorn Stromberg <bjorn@bjornstar.com>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/bjornstar/intercept-redirect/issues" | ||
}, | ||
"homepage": "https://github.com/bjornstar/intercept-redirect#readme" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
const tap = require('tap'); | ||
|
||
const webExtension = require('../webextension'); | ||
const manifest = require('../webextension/manifest.json'); | ||
const package = require('../package.json'); | ||
|
||
const { analyzeURL } = webExtension; | ||
|
||
const urls = [ | ||
'https://disq.us/url?url=https%3A%2F%2Fbjornstar.com%2Fintercept-redirect%3AzjHJ9CS7YTS6D6-FWtZRTF8swk4', | ||
'https://exit.sc/?url=https%3A%2F%2Fbjornstar.com%2Fintercept-redirect', | ||
'https://l.facebook.com/l.php?u=https%3A%2F%2Fbjornstar.com%2Fintercept-redirect', | ||
'https://www.google.co.jp/imgres?imgrefurl=https%3A%2F%2Fbjornstar.com%2Fintercept-redirect', | ||
'https://www.google.co.jp/imgres?imgurl=https%3A%2F%2Fbjornstar.com%2Fintercept-redirect', | ||
'https://www.google.co.jp/url?q=https%3A%2F%2Fbjornstar.com%2Fintercept-redirect', | ||
'https://www.google.co.jp/url?url=https%3A%2F%2Fbjornstar.com%2Fintercept-redirect', | ||
'https://news.url.google.com/url?url=https%3A%2F%2Fbjornstar.com%2Fintercept-redirect', | ||
'https://plus.url.google.com/url?url=https%3A%2F%2Fbjornstar.com%2Fintercept-redirect', | ||
'https://www.google.com/imgres?imgrefurl=https%3A%2F%2Fbjornstar.com%2Fintercept-redirect', | ||
'https://www.google.com/imgres?imgurl=https%3A%2F%2Fbjornstar.com%2Fintercept-redirect', | ||
'https://www.google.com/url?q=https%3A%2F%2Fbjornstar.com%2Fintercept-redirect', | ||
'https://www.google.com/url?url=https%3A%2F%2Fbjornstar.com%2Fintercept-redirect', | ||
'https://l.instagram.com/?u=https%3A%2F%2Fbjornstar.com%2Fintercept-redirect', | ||
'https://l.messenger.com/l.php?u=https%3A%2F%2Fbjornstar.com%2Fintercept-redirect', | ||
'https://slack-redir.net/link?url=https%3A%2F%2Fbjornstar.com%2Fintercept-redirect', | ||
'https://steamcommunity.com/linkfilter/?url=https%3A%2F%2Fbjornstar.com%2Fintercept-redirect', | ||
'https://twitter.com/i/redirect?url=https%3A%2F%2Fbjornstar.com%2Fintercept-redirect', | ||
'https://t.umblr.com/redirect?z=https%3A%2F%2Fbjornstar.com%2Fintercept-redirect', | ||
'https://vk.com/away.php?to=https%3A%2F%2Fbjornstar.com%2Fintercept-redirect', | ||
'https://www.youtube.com/redirect?q=https%3A%2F%2Fbjornstar.com%2Fintercept-redirect' | ||
]; | ||
|
||
const redirectUrl = 'https://bjornstar.com/intercept-redirect'; | ||
|
||
const manifestSites = manifest.permissions.filter(function (permission) { | ||
return permission !== 'webRequest' && permission !== 'webRequestBlocking'; | ||
}).map(function (site) { | ||
return site.replace('*://', '').replace('/', ''); | ||
}); | ||
|
||
const testSites = urls.map(function (url) { | ||
return url.substring(8, url.indexOf('/', 8)); | ||
}); | ||
|
||
const webExtensionSites = Object.keys(webExtension.sites); | ||
|
||
urls.forEach(function (url) { | ||
tap.same(analyzeURL({ url }), { redirectUrl }, `Source: ${url}`); | ||
}); | ||
|
||
webExtensionSites.forEach(function (site) { | ||
tap.ok(manifestSites.indexOf(site) !== -1, `Unmatched: ${site}`); | ||
}); | ||
|
||
manifestSites.forEach(function (site) { | ||
tap.ok(webExtensionSites.indexOf(site) !== -1, `Unmatched: ${site}`); | ||
}); | ||
|
||
manifestSites.forEach(function (site) { | ||
tap.ok(testSites.indexOf(site) !== -1, `Missing tests: ${site}`); | ||
}); | ||
|
||
tap.notOk(analyzeURL({ url: 'https://www.google.com/' })); | ||
tap.notOk(analyzeURL({ url: 'https://bjornstar.com/' })); | ||
|
||
tap.equal(manifest.version, package.version); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,116 +1,134 @@ | ||
// For node compatibility so we can test. | ||
const URL = typeof window === 'undefined' ? require('url').URL : window.URL; | ||
|
||
const googlePathnames = { | ||
'/imgres': ['imgurl','imgrefurl'], | ||
'/url': ['q','url'] | ||
}; | ||
|
||
const sites = { | ||
'disq.us': { | ||
pathnames: { | ||
'/url': 'url' | ||
'/url': ['url'] | ||
}, | ||
extra: function (s) { | ||
extra: function (s = '') { | ||
return s.substring(0, s.lastIndexOf(':')); | ||
} | ||
}, | ||
'exit.sc': { | ||
pathnames: { | ||
'/': 'url' | ||
'/': ['url'] | ||
} | ||
}, | ||
'l.facebook.com': { | ||
pathnames: { | ||
'/l.php': 'u' | ||
'/l.php': ['u'] | ||
} | ||
}, | ||
'www.google.co.jp': { | ||
pathnames: { | ||
'/imgres': 'imgurl', | ||
'/url': 'url' | ||
} | ||
pathnames: googlePathnames | ||
}, | ||
'news.url.google.com': { | ||
pathnames: { | ||
'/url': 'url' | ||
'/url': ['url'] | ||
} | ||
}, | ||
'plus.url.google.com': { | ||
pathnames: { | ||
'/url': 'url' | ||
'/url': ['url'] | ||
} | ||
}, | ||
'www.google.com': { | ||
pathnames: { | ||
'/imgres': 'imgurl', | ||
'/url': 'url' | ||
} | ||
pathnames: googlePathnames | ||
}, | ||
'l.instagram.com': { | ||
pathnames: { | ||
'/': 'u' | ||
'/': ['u'] | ||
} | ||
}, | ||
'l.messenger.com': { | ||
pathnames: { | ||
'/l.php': 'u' | ||
'/l.php': ['u'] | ||
} | ||
}, | ||
'slack-redir.net': { | ||
pathnames: { | ||
'/link': 'url' | ||
'/link': ['url'] | ||
} | ||
}, | ||
'steamcommunity.com': { | ||
pathnames: { | ||
'/linkfilter/': 'url' | ||
'/linkfilter/': ['url'] | ||
} | ||
}, | ||
'twitter.com': { | ||
pathnames: { | ||
'/i/redirect': ['url'] | ||
} | ||
}, | ||
't.umblr.com': { | ||
pathnames: { | ||
'/redirect': 'z' | ||
'/redirect': ['z'] | ||
} | ||
}, | ||
'vk.com': { | ||
pathnames: { | ||
'/away.php': 'to' | ||
'/away.php': ['to'] | ||
} | ||
}, | ||
'www.youtube.com': { | ||
pathnames: { | ||
'/redirect': 'q' | ||
'/redirect': ['q'] | ||
} | ||
} | ||
}; | ||
|
||
function siteReducer(urls, host) { | ||
return urls.concat(Object.keys(sites[host].pathnames).map(function (pathname) { | ||
return `*://${host}${pathname}*`; | ||
})); | ||
function reduceKeyValues(o, pair) { | ||
const [k, v] = pair.split('='); | ||
o[k] = decodeURIComponent(v); | ||
return o; | ||
} | ||
|
||
const urls = Object.keys(sites).reduce(siteReducer, []); | ||
function findKey(o, keys = []) { | ||
for (let i = 0; i < keys.length; i += 1) { | ||
if (o.hasOwnProperty(keys[i])) { | ||
return o[keys[i]]; | ||
} | ||
} | ||
} | ||
|
||
function analyzeURL(request) { | ||
const url = new URL(request.url); | ||
const site = sites[url.host]; | ||
const key = site.pathnames[url.pathname]; | ||
|
||
if (!site) { | ||
return; | ||
} | ||
|
||
const keys = site.pathnames[url.pathname]; | ||
const pairs = url.search.slice(1).split('&'); | ||
|
||
const q = pairs.reduce((o, pair) => { | ||
const [k, v] = pair.split('='); | ||
o[k] = decodeURIComponent(v); | ||
return o; | ||
}, {}); | ||
const q = pairs.reduce(reduceKeyValues, {}); | ||
const value = findKey(q, keys); | ||
|
||
const redirectUrl = (!site.extra && q[key]) || site.extra(q[key] || ''); | ||
const redirectUrl = (site.extra && site.extra(value)) || value; | ||
|
||
return redirectUrl && { redirectUrl }; | ||
} | ||
|
||
function reduceSites(urls, host) { | ||
return urls.concat(Object.keys(sites[host].pathnames).map(function (pathname) { | ||
return `*://${host}${pathname}*`; | ||
})); | ||
} | ||
|
||
const urls = Object.keys(sites).reduce(reduceSites, []); | ||
|
||
// For node compatibility so we can test. | ||
typeof chrome === 'object' && chrome.webRequest.onBeforeRequest.addListener(analyzeURL, { urls }, ['blocking']); | ||
|
||
// For node compatibility so we can test. | ||
typeof exports === 'object' && Object.assign(exports, { | ||
analyzeURL, | ||
siteReducer, | ||
sites | ||
}); |
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