aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--background_page.html5
-rw-r--r--lib/utils.js20
-rw-r--r--test_harnesses/automated.html13
3 files changed, 36 insertions, 2 deletions
diff --git a/background_page.html b/background_page.html
index c62697b0..51f9d462 100644
--- a/background_page.html
+++ b/background_page.html
@@ -2,6 +2,7 @@
<head>
<script type="text/javascript" src="commands.js"></script>
<script type="text/javascript" src="lib/clipboard.js"></script>
+<script type="text/javascript" src="lib/utils.js"></script>
<script type="text/javascript" charset="utf-8">
// Chromium #15242 will make this XHR request to access the manifest unnecessary.
var manifestRequest = new XMLHttpRequest();
@@ -268,11 +269,11 @@
}
function openCopiedUrlInCurrentTab(request) {
- openUrlInCurrentTab({ url: Clipboard.paste() });
+ openUrlInCurrentTab({ url: utils.ensureUrl(Clipboard.paste()) });
}
function openCopiedUrlInNewTab(request) {
- openUrlInNewTab({ url: Clipboard.paste() });
+ openUrlInNewTab({ url: utils.ensureUrl(Clipboard.paste()) });
}
/*
diff --git a/lib/utils.js b/lib/utils.js
index 0c86970e..a0668409 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -74,4 +74,24 @@ var utils = {
};
return null;
},
+
+ /**
+ * Tries to convert :str into a valid URL.
+ * We don't bother with escaping characters, however, as Chrome will do that for us.
+ */
+ ensureUrl: function(str) {
+ // trim str
+ str = str.replace(/^\s+|\s+$/g, '');
+
+ // definitely not a valid URL; treat as a search query
+ if (str.indexOf(" ") != -1 || (str.indexOf('.') == -1 && !/^((http|https|ftp):\/\/)?localhost/.test(str)))
+ return "http://www.google.com/search?q=" + str;
+ // possibly a valid URL, but not canonical
+ else if (!/^(http|https|ftp|chrome):\/\//.test(str))
+ return "http://" + str;
+ // cross our fingers and hope it is valid
+ else
+ return str;
+ },
+
};
diff --git a/test_harnesses/automated.html b/test_harnesses/automated.html
index 5e3ae93c..764b0d2f 100644
--- a/test_harnesses/automated.html
+++ b/test_harnesses/automated.html
@@ -250,6 +250,19 @@
);
+ context("Web query parsing",
+
+ should("Convert query string to valid URL", function() {
+ assert.equal("http://www.google.com/", utils.ensureUrl("http://www.google.com/"));
+ assert.equal("http://www.google.com/", utils.ensureUrl(" http://www.google.com/ "));
+ assert.equal("http://www.google.com", utils.ensureUrl("www.google.com"));
+ assert.equal("http://google.com", utils.ensureUrl("google.com"));
+ assert.equal("http://www.google.com/search?q=google", utils.ensureUrl("google"));
+ assert.equal("http://www.google.com/search?q=go ogle.com", utils.ensureUrl("go ogle.com"));
+ })
+
+ );
+
Tests.outputMethod = function(output) {
var newOutput = Array.prototype.join.call(arguments, "\n");
newOutput = newOutput.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;"); // escape html