Skip to content

Commit

Permalink
v3.0.0 - 2018-05-02
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornstar committed May 2, 2018
1 parent e81cbae commit 1355f0a
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 51 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.zip
node_modules
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Intercept Redirect

## v3.0.0 - 2018-05-02
- Test with TAP
- Rename `src` to `webextension`
- Add `package.json`
- Add `node_modules` to `.gitignore`
- Put built web extensions in `build` directory

## v2.0.0 - 2018-04-29
- Add Google image search redirect
- Add `test.js`
Expand Down
Empty file added build/.gitkeep
Empty file.
27 changes: 27 additions & 0 deletions package.json
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"
}
15 changes: 0 additions & 15 deletions test.js

This file was deleted.

66 changes: 66 additions & 0 deletions test/urls.js
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);
86 changes: 52 additions & 34 deletions src/background.js → webextension/index.js
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
});
5 changes: 3 additions & 2 deletions src/manifest.json → webextension/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"background": {
"scripts": [ "background.js" ]
"scripts": [ "index.js" ]
},
"manifest_version": 2,
"name": "Intercept Redirect",
Expand All @@ -18,9 +18,10 @@
"*://l.messenger.com/",
"*://slack-redir.net/",
"*://steamcommunity.com/",
"*://twitter.com/",
"*://t.umblr.com/",
"*://vk.com/",
"*://www.youtube.com/"
],
"version": "2.0.0"
"version": "3.0.0"
}

0 comments on commit 1355f0a

Please sign in to comment.