Skip to content

Commit

Permalink
v3.5.0 - 2018-05-24
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornstar committed May 24, 2018
1 parent 14a3c8c commit 96e2908
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 13 deletions.
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.5.0 - 2018-05-24
- Add `*.digidip.net` (Fixes #2)
- Add support for wildcard subdomains
- [Testing] Add missing space in test description
- [Testing] Ensure CHANGELOG has an entry for current version
- Improve readability of URL polyfill

## v3.4.1 - 2018-05-18
- [CircleCI] Remove badge from README because it shows build as failing instead of passing, issue reported to CircleCI, but after a week nothing has changed

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bjornstar/intercept-redirect",
"version": "3.4.1",
"version": "3.5.0",
"description": "Skip tracking redirects that serve no purpose other than to waste your precious time.",
"main": "webextension/index.js",
"dependencies": {},
Expand Down
45 changes: 42 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
const assert = require('assert');
const fs = require('fs');
const mocha = require('mocha');
const path = require('path');

const webExtension = require('../webextension');
const manifest = require('../webextension/manifest.json');
const pkg = require('../package.json');

const { describe, it } = mocha;
const { analyzeURL } = webExtension;
const { analyzeURL, subdomain } = webExtension;

const urls = [
'https://bjornstar.digidip.net/visit?url=https%3A%2F%2Fbjornstar.com%2Fintercept-redirect',
'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',
Expand Down Expand Up @@ -41,7 +44,8 @@ const manifestSites = manifest.permissions.filter(function (permission) {
});

const testSites = urls.map(function (url) {
return url.substring(8, url.indexOf('/', 8));
const host = url.substring(8, url.indexOf('/', 8));
return subdomain(host);
});

const webExtensionSites = Object.keys(webExtension.sites);
Expand Down Expand Up @@ -89,7 +93,7 @@ describe('Packaging', function () {
});
});

describe('Every site implemented inthe webExtension has a test', function () {
describe('Every site implemented in the webExtension has a test', function () {
manifestSites.forEach(function (site) {
it(`site: ${site}`, function () {
assert.ok(testSites.indexOf(site) !== -1, `Missing tests: ${site}`);
Expand All @@ -100,4 +104,39 @@ describe('Packaging', function () {
it('Version number is the same in both the package and manifest', function () {
assert.equal(manifest.version, pkg.version);
});

it('The CHANGELOG has an entry for the current version', function (done) {
fs.readFile(path.resolve('./CHANGELOG.md'), 'utf8', function (error, changelog) {
if (error) {
return done(error);
}

const lines = changelog.split('\n');
const versionLine = `## v${pkg.version}`;

for (var i = 0; i < lines.length; i += 1) {
if (lines[i].indexOf(versionLine) === 0) {
return done();
}
}

done('Current version not found in CHANGELOG');
});
});
});

describe('Subdomain', function () {
it(`For supported domains returns *.domain`, function () {
assert.equal(subdomain('foobar.digidip.net'), '*.digidip.net');
assert.equal(subdomain('foo.bar.digidip.net'), '*.digidip.net');
});

it('Does not apply to domain host', function () {
assert.equal(subdomain('digidip.net'), 'digidip.net');
});

it('Returns host when not supported', function () {
assert.equal(subdomain('intercept-redirect.bjornstar.com'), 'intercept-redirect.bjornstar.com');
assert.equal(subdomain('bjornstar.com'), 'bjornstar.com');
});
});
42 changes: 34 additions & 8 deletions webextension/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
// For node compatibility so we can test.
const URL = typeof window === 'undefined' ? require('url').URL : window.URL;
// URL polyfill
const URL = typeof window === 'object' ? window.URL : require('url').URL;

const googlePathnames = {
'/imgres': ['imgurl','imgrefurl'],
'/url': ['q','url']
};

const sites = {
'*.digidip.net': {
pathnames: {
'/visit': ['url']
}
},
'disq.us': {
pathnames: {
'/url': ['url']
Expand Down Expand Up @@ -83,6 +88,24 @@ const sites = {
}
};

const domains = [
'digidip.net'
];

function subdomain(host) {
const hostLength = host.length;

for (let i = 0; i < domains.length; i += 1) {
const domain = domains[i];
const expectedIndex = hostLength - domain.length;

if (expectedIndex > 0 && host.lastIndexOf(domain) === expectedIndex) {
return `*.${domain}`;
}
}
return host;
}

function reduceKeyValues(o, pair) {
const [k, v] = pair.split('=');
o[k] = decodeURIComponent(v);
Expand All @@ -99,14 +122,17 @@ function findKey(o, keys = []) {

function analyzeURL(request) {
const url = new URL(request.url);
const site = sites[url.host];
const host = subdomain(url.host);

const site = sites[host];

if (!site) {
return;
}

const keys = site.pathnames[url.pathname];
const pairs = url.search.slice(1).split('&');
const { pathname, search } = url;
const keys = site.pathnames[pathname];
const pairs = search.slice(1).split('&');

const q = pairs.reduce(reduceKeyValues, {});
const value = findKey(q, keys);
Expand All @@ -124,11 +150,11 @@ function reduceSites(urls, host) {

const urls = Object.keys(sites).reduce(reduceSites, []);

// For node compatibility so we can test.
// Only runs in the browser
typeof chrome === 'object' && chrome.webRequest.onBeforeRequest.addListener(analyzeURL, { urls }, ['blocking']);

// For node compatibility so we can test.
typeof exports === 'object' && Object.assign(exports, {
analyzeURL,
sites
sites,
subdomain
});
3 changes: 2 additions & 1 deletion webextension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"permissions": [
"webRequest",
"webRequestBlocking",
"*://*.digidip.net/",
"*://disq.us/",
"*://exit.sc/",
"*://l.facebook.com/",
Expand All @@ -23,5 +24,5 @@
"*://vk.com/",
"*://www.youtube.com/"
],
"version": "3.4.1"
"version": "3.5.0"
}

0 comments on commit 96e2908

Please sign in to comment.