From 96e2908347fb992304bd0722c4e4e590bc0b8de4 Mon Sep 17 00:00:00 2001 From: Bjorn Stromberg Date: Thu, 24 May 2018 10:36:01 +0900 Subject: [PATCH] v3.5.0 - 2018-05-24 --- CHANGELOG.md | 7 ++++++ package.json | 2 +- test/index.js | 45 +++++++++++++++++++++++++++++++++++--- webextension/index.js | 42 ++++++++++++++++++++++++++++------- webextension/manifest.json | 3 ++- 5 files changed, 86 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4276b60..c337977 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/package.json b/package.json index 951de51..c8762ee 100644 --- a/package.json +++ b/package.json @@ -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": {}, diff --git a/test/index.js b/test/index.js index 9cef61f..d0cc6f8 100644 --- a/test/index.js +++ b/test/index.js @@ -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', @@ -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); @@ -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}`); @@ -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'); + }); }); diff --git a/webextension/index.js b/webextension/index.js index 4e10f04..cae9e34 100644 --- a/webextension/index.js +++ b/webextension/index.js @@ -1,5 +1,5 @@ -// 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'], @@ -7,6 +7,11 @@ const googlePathnames = { }; const sites = { + '*.digidip.net': { + pathnames: { + '/visit': ['url'] + } + }, 'disq.us': { pathnames: { '/url': ['url'] @@ -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); @@ -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); @@ -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 }); diff --git a/webextension/manifest.json b/webextension/manifest.json index 97ff6cd..1cbd2c1 100644 --- a/webextension/manifest.json +++ b/webextension/manifest.json @@ -7,6 +7,7 @@ "permissions": [ "webRequest", "webRequestBlocking", + "*://*.digidip.net/", "*://disq.us/", "*://exit.sc/", "*://l.facebook.com/", @@ -23,5 +24,5 @@ "*://vk.com/", "*://www.youtube.com/" ], - "version": "3.4.1" + "version": "3.5.0" }