Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merged in some changes from https://github.com/mihaip/utm-stripper
- Loading branch information
Showing
6 changed files
with
108 additions
and
15 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 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 |
---|---|---|
@@ -1,20 +1,41 @@ | ||
function getStrippedUrl(url) { | ||
var queryStringIndex = url.indexOf('?'); | ||
|
||
(function() { | ||
var url = window.location.href; | ||
if (url.indexOf('utm_') > queryStringIndex) { | ||
url = url.replace( | ||
/([\?\&]utm_(reader|source|medium|term|campaign|content)=[^&#]+)/ig, | ||
''); | ||
} | ||
|
||
var queryStringIndex = url.indexOf('?'); | ||
// Strip MailChimp parameters | ||
if((url.indexOf('mc_eid') > url.indexOf('?')) || | ||
(url.indexOf('mc_cid') > url.indexOf('?')) ) { | ||
url = url.replace(/([\?\&](mc_cid|mc_eid)=[^&#]+)/ig, ''); | ||
} | ||
|
||
if (url.indexOf('utm_') > queryStringIndex) { | ||
var stripped = url.replace( | ||
/([\?\&]utm_(source|medium|term|campaign|content)=[^&#]+)/ig, | ||
''); | ||
if (stripped.charAt(queryStringIndex) === '&') { | ||
stripped = stripped.substr(0, queryStringIndex) + '?' + | ||
stripped.substr(queryStringIndex + 1) | ||
// Strip YouTube parameters | ||
if((url.indexOf('http://www.youtube.com/watch') === 0) || | ||
(url.indexOf('https://www.youtube.com/watch') === 0) ) { | ||
url = url.replace(/([\?\&](feature|app|ac)=[^&#]*)/ig, ''); | ||
} | ||
if (stripped != url) { | ||
history.replaceState(null, null, stripped); | ||
|
||
if (url.charAt(queryStringIndex) === '&') { | ||
url = url.substr(0, queryStringIndex) + '?' + | ||
url.substr(queryStringIndex + 1); | ||
} | ||
|
||
return url; | ||
} | ||
|
||
(function() { | ||
|
||
var url = getStrippedUrl(window.location.href); | ||
|
||
if(url !== window.location.href) { | ||
history.replaceState(null, null, url); | ||
} | ||
else { | ||
return; | ||
} | ||
|
||
})(); |
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,70 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Unit Tests</title> | ||
<script> | ||
// Stub so that background.js loads without errors. | ||
var chrome = { | ||
tabs: { | ||
onUpdated: { | ||
addListener: function() {} | ||
} | ||
} | ||
}; | ||
</script> | ||
<script src="charisma.js"></script> | ||
<script> | ||
function log(msg, opt_color) { | ||
var msgNode = document.createElement('div'); | ||
if (opt_color) { | ||
msgNode.style.color = opt_color; | ||
} | ||
msgNode.innerText = msg; | ||
document.getElementById('log').appendChild(msgNode); | ||
} | ||
|
||
function assertTrue(cond, opt_msg) { | ||
if (cond) { | ||
log('PASS: true'); | ||
} else { | ||
log(opt_msg ? 'FAIL: ' + opt_msg : 'FAIL', 'red'); | ||
} | ||
} | ||
|
||
function assertEquals(expected, actual, opt_msg) { | ||
if (expected == actual) { | ||
log('PASS: ' + expected + ' (as expected)'); | ||
} else { | ||
var msg = opt_msg ? ' ' + opt_msg : ''; | ||
log('FAIL: ' + expected + ' (expected) != ' + actual + ' (actual)' + msg, 'red'); | ||
} | ||
} | ||
|
||
var TEST_DATA = { | ||
'http://example.com/post': 'http://example.com/post', | ||
'http://example.com/post?foo=bar': 'http://example.com/post?foo=bar', | ||
'http://example.com/post?utm_source=src&utm_medium=medium&utm_campaign=campaign&utm_reader=feedly': 'http://example.com/post', | ||
'http://example.com/post?mc_cid=1234&mc_eid=4321': 'http://example.com/post', | ||
'http://example.com/post?foo=bar&utm_source=src': 'http://example.com/post?foo=bar', | ||
'http://example.com/post?utm_source=src&foo=bar': 'http://example.com/post?foo=bar', | ||
'http://example.com/post?utm_source=src#foo=bar': 'http://example.com/post#foo=bar', | ||
|
||
'https://www.youtube.com/watch?v=YlGqN3AKOsA&feature=kp': 'https://www.youtube.com/watch?v=YlGqN3AKOsA', | ||
'https://www.youtube.com/watch?feature=youtu.be&v=YlGqN3AKOsA': 'https://www.youtube.com/watch?v=YlGqN3AKOsA', | ||
'http://www.youtube.com/watch?v=YlGqN3AKOsA&feature=kp': 'http://www.youtube.com/watch?v=YlGqN3AKOsA', | ||
'http://www.youtube.com/watch?v=YlGqN3AKOsA&app=desktop': 'http://www.youtube.com/watch?v=YlGqN3AKOsA', | ||
'http://www.youtube.com/watch?v=YlGqN3AKOsA&feature=kp&app=desktop': 'http://www.youtube.com/watch?v=YlGqN3AKOsA', | ||
'http://www.youtube.com/watch?app=desktop&v=YlGqN3AKOsA': 'http://www.youtube.com/watch?v=YlGqN3AKOsA', | ||
} | ||
|
||
function runTests() { | ||
for (var input in TEST_DATA) { | ||
assertEquals(TEST_DATA[input], getStrippedUrl(input)); | ||
} | ||
} | ||
</script> | ||
</head> | ||
<body onload="runTests()"> | ||
<pre id="log"></pre> | ||
</body> | ||
</html> |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.