At the office, I work with several other sysads and programmers. We use Google day in and day out to solve a variety of IT problems. One of my friends was recently musing that perhaps our search queries would actually yield better results if they were somehow nudged sideways a bit from what we thought we were looking for.
I decided to take his suggestion and I built a simple search form that tacked on a random word from the dictionary to the end of each query are then redirect to the search results. The page has proven to be a practical success as well as just being fun to use and occasionally entertaining. Thought I had originally thrown the page together in C#, I decided to rewrite it in PHP so I could put it up on public shared hosting for others to try.
You can find the page at Google Salted.
I started with the back end. I wanted a simple page that could be called by an Ajax function on the browser and return a random word from the dictionary. Everything else would be on the client side.
To begin, I downloaded a dictionary from an open-source spell-checking library. It came in the form of a plain text file with about 100,000 lines, one word per line. I imported these into a simple single-column MySQL table. Then, I needed a light-weight query to randomly select one word from the list. This does the trick:
SELECT word FROM words ORDER BY RAND() LIMIT 1
Then the entire contents of GetSalt.php became:
<?php
function GetSalt()
{
$salt = "";
$con=mysqli_connect("localhost","words_user","password","words_database");
// Check connection
if (mysqli_connect_errno())
{
echo "Error: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT word FROM words ORDER BY RAND() LIMIT 1");
while($row = mysqli_fetch_array($result))
{
$salt = $row['word'];
}
mysqli_close($con);
return $salt;
}
echo GetSalt();
?>
Hit this URL, and you get a word back in plain text with absolutely nothing (markup, etc.) wrapped around it.
Now for the front end. There is essentially a text box and search button to click. The rest is just frosting.
<input name="SearchBox" type="text" id="SearchBox" style="font-family:Arial;font-size:20px;height:35px;width:700px;" />
<button id="SearchButton" onclick="search(false);">Google Salted Search!</button>
<button id="LuckyButton" onclick="search(true);">I'm Feeling Very Lucky</button>
Now for what the search actually does when you click the button:
<script type="text/javascript">
// Declare salt variable in document scope
var salt = '';
// Cause hitting enter inside of the search box to trigger a search
$("#SearchBox").keyup(function(event){
if(event.keyCode == 13){
$("#SearchButton").click();
}
});
function search(feelingLucky)
{
// Get random English word
getSalt();
// Wait for the AJAX call to finish before continuing
$(document).ajaxStop(function () {
// Base Google search results URL with query parameter
var redirect = 'https://www.google.com/search?q=';
// Add on the values from the text box, replacing spaces with '+'
redirect += $('#SearchBox').val().replace(/ /g, '+');
redirect += '+' + salt;
// If they hit the lucky button, tack a little more on to the end
if(feelingLucky)
redirect += '&btnI';
// Now redirect the user. Done.
window.location = redirect;
}); // End ajaxStop function
}
function getSalt()
{
$.ajax({
type: 'get',
url: '/googlesalt/GetSalt.php',
dataType:'text',
success: function(data) {
// Call will return a single word as a string. Set the salt variable to this value
salt = data;
}
});
}
</script>
This all pretty simple except for the fact that the getSalt() function executes asyncronously. That is what the A in ajax is for after all. That means that it will just make the call and go right on humming building the rest of the redirect url before the call replies back with a random word. To stop it from continuing until the “salt” word is retrieved, we have to wrap the function in that ajaxStop method.
$(document).ajaxStop(function () {
Do stuff after all the ajax calls are done.
};
That is the heart of it. Then I styled the page, photoshopped a logo, and added a few niceties like a function that detects an enter keystroke in the textbox and executes the search. By making most of the logic on the client-side, no additional round-trip to the server is needed and queries are kept anonymous and no logged.
Is this just a silly little toy? Sort of, but it has turned out to be a case of engineered serendipity. A few examples below. (I had some better examples, from a couple weeks ago, but they have slipped my mind.)
A colleague of mine had been frustrated searching for information on a strange error related to a recent Windows service pack. He tried the salted search and it added the word “snap” to the end of his query. He was immediately shown a rather buried blog post about how someone had worked around the error using a particular Snap-in on the Windows MMC console. This result was buried under unrelated junk, but introducing some randomness brought it to the top. You would say, “That’s just luck!”, ah, but you’d be amazed how often one gets lucky.
Another friend was trying to dress up his office with a interesting doorstop. The salt search suggested an antique typewriter. Great idea.
Searching for “javascript wait for ajax” just now tacked on the word “meowing”, which led to a good example of how to do the same in a particular ruby framework. Apparently “meow” is a convention used in example code in some circles, kind of like ‘foo’ and ‘bar’. Not particularly useful knowledge, but kind of fun. I think the tool is ultimately the most useful if you are really stuck looking for something. Forcing you to look somewhere in the library that you hadn’t considered can lead you back toward the correct path if you are lost.