Skip to content

Commit

Permalink
v2.0.0 - 2018-04-29
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornstar committed Apr 29, 2018
1 parent c480b4f commit e81cbae
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 81 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
80 changes: 0 additions & 80 deletions background.js

This file was deleted.

116 changes: 116 additions & 0 deletions src/background.js
Original file line number Diff line number Diff line change
@@ -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
});
3 changes: 2 additions & 1 deletion manifest.json → src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
Expand All @@ -21,5 +22,5 @@
"*://vk.com/",
"*://www.youtube.com/"
],
"version": "1.7.0"
"version": "2.0.0"
}
15 changes: 15 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -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 })
});

0 comments on commit e81cbae

Please sign in to comment.