-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
139 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
}); |