From e81cbae1e2d97d8fc4c0fc422a955a4add5ba0d5 Mon Sep 17 00:00:00 2001 From: Bjorn Stromberg Date: Sun, 29 Apr 2018 12:54:21 +0900 Subject: [PATCH] v2.0.0 - 2018-04-29 --- CHANGELOG.md | 6 ++ background.js | 80 -------------------- src/background.js | 116 +++++++++++++++++++++++++++++ manifest.json => src/manifest.json | 3 +- test.js | 15 ++++ 5 files changed, 139 insertions(+), 81 deletions(-) delete mode 100644 background.js create mode 100644 src/background.js rename manifest.json => src/manifest.json (91%) create mode 100644 test.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 445a100..296b373 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Intercept Redirect +## v2.0.0 - 2018-04-29 +- Add Google image search redirect +- Add `test.js` +- Reorganize into src & build directories +- Add compatibility for node + ## v1.7.0 - 2018-04-25 - If there's no extra data, just return the key - I should probably write some tests diff --git a/background.js b/background.js deleted file mode 100644 index 1215299..0000000 --- a/background.js +++ /dev/null @@ -1,80 +0,0 @@ -const sites = { - 'disq.us': { - pathname: 'url', - key: 'url', - extra: function (s) { - return s.substring(0, s.lastIndexOf(':')); - } - }, - 'exit.sc': { - pathname: '', - key: 'url' - }, - 'l.facebook.com': { - pathname: 'l.php', - key: 'u' - }, - 'news.url.google.com': { - pathname: 'url', - key: 'url' - }, - 'plus.url.google.com': { - pathname: 'url', - key: 'url' - }, - 'www.google.com': { - pathname: 'url', - key: 'url' - }, - 'l.instagram.com': { - pathname: '', - key: 'u' - }, - 'l.messenger.com': { - pathname: 'l.php', - key: 'u' - }, - 'slack-redir.net': { - pathname: 'link', - key: 'url' - }, - 'steamcommunity.com': { - pathname: 'linkfilter/', - key: 'url' - }, - 't.umblr.com': { - pathname: 'redirect', - key: 'z' - }, - 'vk.com': { - pathname: 'away.php', - key: 'to' - }, - 'www.youtube.com': { - pathname: 'redirect', - key: 'q' - } -}; - -const urls = Object.keys(sites).map(function(host) { - return `*://${host}/${sites[host].pathname}*`; -}); - -chrome.webRequest.onBeforeRequest.addListener(function(request) { - const url = new URL(request.url); - const site = sites[url.host]; - - 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 redirectUrl = (!site.extra && q[site.key]) || site.extra(q[site.key] || ''); - - if (redirectUrl) { - return { redirectUrl }; - } -}, { urls }, ['blocking']); diff --git a/src/background.js b/src/background.js new file mode 100644 index 0000000..90083f4 --- /dev/null +++ b/src/background.js @@ -0,0 +1,116 @@ +// For node compatibility so we can test. +const URL = typeof window === 'undefined' ? require('url').URL : window.URL; + +const sites = { + 'disq.us': { + pathnames: { + '/url': 'url' + }, + extra: function (s) { + return s.substring(0, s.lastIndexOf(':')); + } + }, + 'exit.sc': { + pathnames: { + '/': 'url' + } + }, + 'l.facebook.com': { + pathnames: { + '/l.php': 'u' + } + }, + 'www.google.co.jp': { + pathnames: { + '/imgres': 'imgurl', + '/url': 'url' + } + }, + 'news.url.google.com': { + pathnames: { + '/url': 'url' + } + }, + 'plus.url.google.com': { + pathnames: { + '/url': 'url' + } + }, + 'www.google.com': { + pathnames: { + '/imgres': 'imgurl', + '/url': 'url' + } + }, + 'l.instagram.com': { + pathnames: { + '/': 'u' + } + }, + 'l.messenger.com': { + pathnames: { + '/l.php': 'u' + } + }, + 'slack-redir.net': { + pathnames: { + '/link': 'url' + } + }, + 'steamcommunity.com': { + pathnames: { + '/linkfilter/': 'url' + } + }, + 't.umblr.com': { + pathnames: { + '/redirect': 'z' + } + }, + 'vk.com': { + pathnames: { + '/away.php': 'to' + } + }, + 'www.youtube.com': { + pathnames: { + '/redirect': 'q' + } + } +}; + +function siteReducer(urls, host) { + return urls.concat(Object.keys(sites[host].pathnames).map(function (pathname) { + return `*://${host}${pathname}*`; + })); +} + +const urls = Object.keys(sites).reduce(siteReducer, []); + +function analyzeURL(request) { + const url = new URL(request.url); + const site = sites[url.host]; + const key = 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 redirectUrl = (!site.extra && q[key]) || site.extra(q[key] || ''); + + return redirectUrl && { redirectUrl }; +} + +// 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 +}); diff --git a/manifest.json b/src/manifest.json similarity index 91% rename from manifest.json rename to src/manifest.json index a45a7c2..bfb8005 100644 --- a/manifest.json +++ b/src/manifest.json @@ -10,6 +10,7 @@ "*://disq.us/", "*://exit.sc/", "*://l.facebook.com/", + "*://www.google.co.jp/", "*://news.url.google.com/", "*://plus.url.google.com/", "*://www.google.com/", @@ -21,5 +22,5 @@ "*://vk.com/", "*://www.youtube.com/" ], - "version": "1.7.0" + "version": "2.0.0" } diff --git a/test.js b/test.js new file mode 100644 index 0000000..8c5ca43 --- /dev/null +++ b/test.js @@ -0,0 +1,15 @@ +const assert = require('assert'); + +const analyzeURL = require('./src/background.js').analyzeURL; + +const testURLs = [ + { + src: 'https://t.umblr.com/redirect?z=http%3A%2F%2Fwww.theguardian.com%2Fnews%2F2018%2Fapr%2F19%2Fwolves-of-instagram-jordan-belmont-social-media-traders', + dst: 'http://www.theguardian.com/news/2018/apr/19/wolves-of-instagram-jordan-belmont-social-media-traders' + } +]; + +testURLs.forEach(function (url) { + const redirectUrl = url.dst; + assert.deepEqual(analyzeURL({ url: url.src }), { redirectUrl }) +});