Recently I got an email from my friend John Simpson. He was having a search problem and thought I might be able to help him out. I was, and wanted to share with you what I did, because a) you might be able to use it too and b) it’s not often in my Internet experience that you end up solving a problem using a method that was popular over ten years ago.
Here’s John’s problem: he does regular Google searches of a particular kind, but finds that with most of these searches he gets an overwhelming number of results from just a couple of sites. He wants to consistently exclude those sites from his search results, but he doesn’t want to have to type in the exclusions every time.
The rock-simple solution to this problem would be: do the Google search excluding all the sites you don’t want to see, bookmark the result, and then revisit that bookmark whenever you’re ready to search. But a more elegant solution would be to use an bookmark enhanced with JavaScript: a bookmarklet.
What’s a Bookmarklet?
A bookmarklet is not much more than how I described it in the last paragraph: a bookmark, enhanced with JavaScript. Whereas a bookmark is static, a bookmarklet allows you to have input – like a prompt for a Google search.
What I did was create a bookmarklet that excluded the sites from which he didn’t want results. When he clicks on the bookmarklet, it prompts him for his search term and then runs the search.
I tested the bookmarklet on Chromium, Chrome, and Firefox, and it works fine if you allow pop-ups (unfortunately I couldn’t find a way to just whitelist local pop-ups.)
Let’s walk through how I did it. Maybe there’s a way you can use this!
Creating a Bookmarklet
There are still some bookmarklet places hanging around the Web, though the last time I saw it being mentioned and worked with a lot was 2005 or so. Squarefree’s bookmarklets list is one of those sites (and its front page was last updated in 2004!) The search engine bookmarklets page has a bookmarklet for searching Google, which is what I based this on — I am eternally grateful to Jesse Ruderman for keeping his site up and not making me dig through my own archives trying to find code.
Here’s the Squarefree bookmarklet code:
javascript:q = “” + (window.getSelection ? window.getSelection() : document.getSelection ? document.getSelection() : document.selection.createRange().text); if (!q) q = prompt(“You didn’t select any text. Enter a search phrase:”, “”); if (q!=null) location=”http://www.google.com/search?q=” + escape(q).replace(/ /g, “+”); void 0
Basically the bookmarklet checks to see if you’ve highlighted text on a Web page, and if you have it runs that as a Google search. If not it prompts you to enter your own search term.
This is the part we need to change:
location="http://www.google.com/search?q="
That is a plain Google search, but John wants to exclude all search results from Pinterest.com and experts-exchange.com. We need to incorporate those exclusions. That will take three steps.
Step One: Run the Search You Want to See
I’m going to pretend to be John and run a search for cows, excluding Pinterest and Experts Exchange. (DISCLAIMER: I have no idea of John searches for cows or not.) My query will look like this.
cows -site:pinterest.com -site:experts-exchange.com
The Google search result URL will look something like this (I removed some of the cruft that Google adds):
https://www.google.com/search?&q=cows+-site%3Apinterest.com+-site%3Aexperts-exchange.com
(You’ll notice that the URL reads -site%3Apinterest.com instead of -site:pinterest.com. That’s because the colon (:) symbol has been encoded in the URL. You can learn more about URL encoding here.)
Step Two: Extract the search modifiers from the result URL
If you’re doing something super esoteric, the Google search result URL might be a little hard to read, or the URL encoding might throw you off if you’re using a lot of special characters. But generally you can map what you’re seeing in the search results to the search terms you were looking for. In this case I want to put -site%3Apinterest.com -site%3Aexperts-exchange.com in my Google search result URL of the bookmarklet.
Step Three: Modify the Bookmarklet
The bookmarklet’s Google result URL looks like this, as I noted:
location="http://www.google.com/search?q="
I want to integrate the search result modifiers into this URL, but at the same time I want to make sure the URL ends in q=, because when John is prompted for a search term, his input will go after that last q. I want to put my modifiers before that prompt.
“q” stands for query, I believe, and you can have more than one in a Google search result URL, so I put one at the beginning for my search modifiers and left one at the end.
http://www.google.com/search?q=-site%3Aexperts-exchange.com+-site%3Apinterest.com&q=
I added in the query modifiers -site%3Aexperts-exchange.com+-site%3Apinterest.com , then added an ampersand (&) to let Google know there would be another query in the search, and then put the q= for user-input search terms.
How does this all look in one place?
javascript:q = “” + (window.getSelection ? window.getSelection() : document.getSelection ? document.getSelection() : document.selection.createRange().text); if (!q) q = prompt(“You didn’t select any text. Enter a search phrase:”, “”); if (q!=null) location=”http://www.google.com/search?q=-site%3Aexperts-exchange.com+-site%3Apinterest.com&q=” + escape(q).replace(/ /g, “+”); void 0
Now we have a bookmarklet that John can install and he’ll nevermore have to look at Pinterest or Experts Exchange in his search results. Let’s install this in Firefox. Three steps again.
Step One: Install the Bookmarklet
Open up Firefox and make sure your Bookmarks Toolbar is visible. (You can do this without having the bookmarks bar visible, but having it visible makes this part easier.)
In the Bookmarks Toolbar you might see a bookmark called Getting Started, from Firefox. I’m going to edit that one to turn it into this bookmarklet. (I find it’s easier to just bookmark a page and then edit it to make a bookmarklet, as opposed to trying to make one “from scratch.”)
Right-click that Getting Started bookmark on your toolbar and click Properties:
(Don’t ask me why I’m searching for pancakes. I think it’s because it’s almost 7:30 and I haven’t had dinner yet.)
You’ll get a properties window. Change the name of the bookmark to something distinctive, then paste the Javascript of the bookmarklet into the Location field.
Save that. You should now have a Firefox bookmark called No Pinterest, No Experts Exchange Google Search. Click on it and you should get a window prompting you for your Google Search.
Enter your search, click OK, and — there you go!
This is a somewhat simple implementation of a modified Google search — it just excludes two sites. But you could go a lot further. You could include multiple sites or include/exclude based on an URL pattern. You could restrict a search to just titles if you do general searches that tend to get too many results. And if you really want to flex, you could create a bookmarklet that searched Google for items indexed only within a specific date range.
Would you like to see me do another article with more advanced options for these bookmarks? Let me know.
Thanks to John for the interesting and fun search problem!
Categories: Learning Search
I’ve created several bookmarklets for myself and others in the last few years at work and it seems so sad how few people are aware of them or how useful they can be.
Right? It’s kind of like RSS in that regard.
This is great! These “how to” kinds of posts are really helpful. I’d love to see more of them. Something on more advanced options for bookmarks would be very welcome. And yes, RSS feeds are very useful and sadly ignored.
Thank you again, Tara!
About adding the bookmarklet to the toolbar… this works in Firefox, dunno about the others, but the way I did it was: copied the Javascript, then right-clicked the Bookmarks Toolbar and selected Paste. It created the bookmarklet automatically, “naming” it with the JS code itself. Which looked kinda dumb, so I just changed the name via the Properties dialog. (It’s called “JESSearch” because I know it’s more than a little idiosyncratic, ha.) And FWIW, it seems to port across FFox installations (just like other bookmarks) during the sync process.
SO glad I asked you. As I told you, I’ve got nothing against the two sites I wanted to exclude except that they get in the way of my own requirements (and, in the case of experts-exchange, in the way of my own parsimoniousness).