Skip to content

Commit

Permalink
merged in some changes from https://github.com/mihaip/utm-stripper
Browse files Browse the repository at this point in the history
  • Loading branch information
grahams committed Dec 13, 2015
1 parent c4f85e8 commit 5771f0d
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 15 deletions.
8 changes: 5 additions & 3 deletions safari-utm-stripper.safariextension/Info.plist
Expand Up @@ -5,17 +5,17 @@
<key>Author</key>
<string>Sean Graham</string>
<key>Builder Version</key>
<string>9537.76.4</string>
<string>11601.3.9</string>
<key>CFBundleDisplayName</key>
<string>UTM Stripper</string>
<key>CFBundleIdentifier</key>
<string>com.dosburros.safari-utm-stripper</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleShortVersionString</key>
<string>2.0</string>
<string>2.0.3</string>
<key>CFBundleVersion</key>
<string>4</string>
<string>7</string>
<key>Chrome</key>
<dict>
<key>Global Page</key>
Expand All @@ -33,6 +33,8 @@
</dict>
<key>Description</key>
<string>Strips Google Analytics (i.e. Urchin Traffic Monitor) tokens from URL query strings.</string>
<key>DeveloperIdentifier</key>
<string>J3Q79T636D</string>
<key>ExtensionInfoDictionaryVersion</key>
<string>1.0</string>
<key>Permissions</key>
Expand Down
45 changes: 33 additions & 12 deletions safari-utm-stripper.safariextension/charisma.js
@@ -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;
}

})();
70 changes: 70 additions & 0 deletions safari-utm-stripper.safariextension/tests.html
@@ -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>
Binary file added sea urchin 1024x768.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sea urchin 256x256.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sea urchin orig.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5771f0d

Please sign in to comment.