Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix URL rewriting when there are additional query parameters.

Add some basic unit tests.

Move JavaScript to separate file, both for testing and to prepare for manifest version 2.0.
  • Loading branch information...
commit 4561b7d89d8de0c9febc5a8477b278ce395c099e 1 parent b9a50fb
Mihai Parparita authored
15 extension/background.html
View
@@ -1,19 +1,6 @@
<!DOCTYPE html>
<html>
<body>
-<script>
-chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
- if (tab.url.indexOf('utm_') <= tab.url.indexOf('?')) return;
-
- var strippedUrl = tab.url.replace(
- /([\?\&]utm_(source|medium|campaign|content)=[^&#]+)/ig, '');
-
- if (strippedUrl == tab.url) return;
-
- chrome.tabs.executeScript(
- tabId,
- {code: 'history.replaceState(undefined, undefined, "' + strippedUrl + '");'});
-});
-</script>
+<script src="background.js"></script>
</body>
</html>
23 extension/background.js
View
@@ -0,0 +1,23 @@
+function getStrippedUrl(url) {
+ if (url.indexOf('utm_') <= url.indexOf('?')) return url;
+
+ var strippedUrl =
+ url.replace(/([\?\&]utm_(source|medium|campaign|content)=[^&#]+)/ig, '');
+
+ // If there were other query parameters, and the utm_ ones were first, then
+ // we need to convert the first ampersand to a ? to still have a valid URL.
+ if (strippedUrl.indexOf('&') != -1 && strippedUrl.indexOf('?') == -1) {
+ strippedUrl = strippedUrl.replace('&', '?');
+ }
+
+ return strippedUrl;
+}
+
+chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
+ var strippedUrl = getStrippedUrl(tab.url);
+ if (strippedUrl == tab.url) return;
+
+ chrome.tabs.executeScript(
+ tabId,
+ {code: 'history.replaceState(undefined, undefined, "' + strippedUrl + '");'});
+});
2  extension/manifest.json
View
@@ -1,7 +1,7 @@
{
"name": "Google Analytics Parameter Stripper",
"description": "Removes Google Analytics (UTM) tracking tokens from URL query strings.",
- "version": "1.0",
+ "version": "1.0.1",
"background_page": "background.html",
"permissions": [ "tabs", "<all_urls>" ],
"icons": {"48": "icon48.png", "128": "icon128.png" }
62 extension/tests.html
View
@@ -0,0 +1,62 @@
+<!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="background.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': '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'
+ }
+
+ 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>
Please sign in to comment.
Something went wrong with that request. Please try again.