Do the Right Thing
Description
Use a wiki page to create and edit link shortcuts to a number of predefined custom searches. The feature is similar to the InterMap feature, but is directly embedded in your browser.
This is a productivity enhancement for those who use Internet a lot and PmWiki for Personal Information Management. The user can type a "keyword" and some "search terms" in her browser address bar, and the browser will automatically open the search results page based on the keyword. For example, typing in the address bar g some search
will search Google, w some term
will search Wikipedia, php some function
will search the PHP.net documentation, and so on.
There is a benefit to use a wiki page instead of browser "bookmark shortcuts" if you use more than one computer or more than one browser. Your shortcuts are very easy to add and edit, and will always be up-to-date in all browsers.
Installation
- Copy dtrt.phpΔ to your
/cookbook/
directory (alt. download: dtrt.php). - Add to a selected file
local/Main.DTRT.php
such a line:
<?php include_once("$FarmD/cookbook/dtrt.php");
(You can select any page you wish. This page will contain your shortcut definitions.)
Shortcut configuration
In your selected wiki page, for example Main.DTRT, add the link shortcuts in this format:
gi: http://images.google.com/images?q=$1 : Google images fm: http://freshmeat.net/search/?section=projects&q=$1 : Freshmeat (software) php: http://php.net/search.php?lang=en&show=quickref&pattern=$1 : PHP manual map,maps: http://www.google.com/maps?q=$1 : Search Google Maps pm: http://www.pmwiki.org/wiki/?action=search&q=$1 : search PmWiki.org imdb: http://www.imdb.com/find?s=all&q=$1 : search IMDB for movies |
On the shortcut line, there is a "keyword" followed by a ":" colon, spaces and the url address of the search related to this keyword. The address contains "$1" which will be replaced with the search terms. At last, after another colon, you can have some optional comment.
While most search engines are in UTF-8, some private sites still use the older encoding ISO-8859-1. For these cases you can use $2 instead of $1 in your url address:
am: http://www.amazon.fr/s/?field-keywords=$2 : Amazon France |
You can have more than one shortcut for an address, separate them with commas like the map,maps:
line in the example, or put them on different lines.
The shortcuts are case sensitive, you can have different URLs for "a" and for "A" shortcuts.
You can define shortcuts to use the POST method instead of the default GET: after the URL, (append a double ampersand "&&" then a name=url_encoded_value parameter to be posted) and repeat as needed:
_default: https://startpage.com/do/search&&cat=web&&query=$1
The form above will be posted to the address https://startpage.com/do/search
and will contain an element 'cat' with a value 'web', and another element 'query' with a value what you typed in the browser field.
The recipe has 3 default shortcuts built-in: "g" to search Google, "w" for Wikipedia in English, and a "_default" search when no shortcut was recognized. You can change the default shortcuts by simply redefining them in the Main.DTRT page:
_default: https://startpage.com/do/search?cat=web&query=$1 : Set StartPage as default
There is a special keyword "setdefault" where you can temporarily set a different default search prefix. For example, requesting "setdefault w" will cause the "Wikipedia" search to be launched for the following searches. This is a session-only setting for the current browser only. All search prefixes will work, but if there is no recognized prefix, the "w" prefix will be assumed. To reset the search to the "_default" prefix, request simply "setdefault" with nothing else. (I recently needed this to launch many searches from a list of cities to a google maps directions from my home to each city, simply select the city, right-click and search.)
If you want DTRT to not immediately redirect to the destination page, but instead to pause and display a link, waiting for you to click, precede the destination URL address with a comma, for example:
maps: ,http://www.google.com/maps?q=$1 : Search Google Maps |
This way you can, for example, switch on/off a proxy, or enable/disable JavaScript before opening the destination page.
On that same page, you can add a form allowing searches from browsers that are not configured. For example, you can place at the top of the page:
(:input form action="{$PageUrl}" method=get:) (:input hidden action dtrt:)(:input hidden n {$FullName}:) (:input text q size=40:)(:input submit value="Do!":) (:input end:) |
Custom functions
The recipe allows you to define custom functions that will process the query string and return the redirect URL. Here is an example:
# or set "cad: function:cadastre" in your wiki page $DTRT_links['cad'] = 'function:cadastre'; function DTRT_cadastre($q) { $q = trim($q); if(preg_match('/(\\d+)? *([a-z].+?), (\\S.*)$/i', $q, $m)) { $mm = array(); for($i=1; $i<count($m); $i++) $mm[$i] = rawurlencode(trim($m[$i])); $url = "http://cadastre.gouv.fr/scpc/rechercherPlan.do&&numeroVoie=$mm[1]&&nomVoie=$mm[2]&&ville=$mm[3]"; return $url; } Abort(__function__ . "() cannot process query \"$q\""); }
This function responds to the keyword "cad" then the regular expression matches a street address, e.g. "14 rue Baguette, Paris". If found, it redirects to the French Cadastral map website. If the string does not match the expected pattern, the function aborts.
The function name needs to start with "DTRT_", like "DTRT_cadastre" but in the wiki page write "function:" then the part after "DTRT_" e.g.:
cad,cadastre: function:cadastre |
If the function returns a URL in the "POST" format outlined above, the form will be posted, otherwise the GET method will be used in the redirection.
Browser configuration
You need to instruct your browser to send searches to your Main.DTRT wiki page (the recipe will then redirect to the selected search).
Your custom address is that of the Main.DTRT page followed by ?action=dtrt&q=
for example, http://yourwiki.net/wiki/Main/DTRT?action=dtrt&q=
.
- Mozilla Firefox version 23 or newer : on your DTRT wiki page, pull down the arrow on your browser's search field and select "Add DTRT for this page". (After, you can hide the search field and type your search directly in the location field.)
- Mozilla, Firefox (versions up to 22) : type in the address bar "about:config", double-click on the line
keyword.URL
and replace the default url with your custom address. - Rekonq : Wrench - Configure Rekonq - Search engines - enable the checkboxes "Enable web shortcuts" and "Use preferred shortcuts only", then press "New". In the dialog form, fill a name (DTRT), in the URL field enter your custom address followed by "\{@}" like "...?action=dtrt&q=\{@}", pick a keyword ("g"), press OK. At the bottom of the dialog box, select "Default web shortcut: DTRT" and "Keyword delimiter: Space" then press OK.
- Konqueror : Settings - Configure Konqueror - Web Shortcuts - New. In the box select a name ("DTRT"), pick a keyword ("d"), enter your custom address followed by "\{@}" like "...?action=dtrt&q=\{@}", pick a keyword ("g"), press "Ok". Select Default search engine - DTRT and Keyword delimiter - Space, press Apply and Ok.
- Midori : Click on the icon in the search box, select "Manage search engines", press "Add". In the dialog box fill in Name: DTRT, Address: your custom address followed by "%s" like "...?action=dtrt&q=%s". Press Add, then select it from the list and press Use as default. You may want to remove other search engines which may conflict with your own DTRT shortcuts.
Older versions of Midori: Tools - Manage search engines - Add. In the dialog box fill in Name: DTRT, Address: your custom address followed by "%s" like "...?action=dtrt&q=%s". Press Add, then Use as default. - Chromium, Google Chrome : Menu - Settings -or- Preferences - Manage search engines... - Other search engines - Add a new search engine. In the empty text boxes select a name ("DTRT"), pick a keyword ("d"), entrer your custom address followed by "%s" like "...?action=dtrt&q=%s", press "Done", then again "Manage search engines..." and press "Make Default".
- MS Internet Explorer 8 : When you visit your DTRT page, in the program interface at the top right, press the arrow after the search field, click on "Add search provider - DTRT for this page". In the dialog box you may check "Make default" and press "Add".
- Opera : Settings - Preferences - Search - Add. In the box select a name ("DTRT"), pick a keyword ("d"), entrer your custom address followed by "%s" like "...?action=dtrt&q=%s", check the two checkboxes about a default search engine.
Usage
To search Internet, in your browser press F6 or Ctrl+T to focus the address bar, then enter the search shortcut, a space, then the search terms, and press Enter. For example, entering fm PmWiki
will search the Freshmeat software catalog for "PmWiki". The "fm" keyword is defined in the DTRT wiki page.
Notes
- In an open wiki, your Main.DTRT wiki page should probably be password-protected for editing.
- Some browsers have built-in search shortcuts. If you enter a shortcut which is defined in your browser, your browser will take care of the search, and not your DTRT wiki page. In most cases you can disable or change the conflicting shortcut in your browser settings.
Release notes
- 20230622 - encode search strings with rawurlencode instead of urlencode, for better compatibility with some services.
- 20160707 - change default shortcut URLs to HTTPS protocol.
- 20150711 - fixed a bug with POST method when a query contains apostrophes.
- 20150630a - add POST method.
- 20150127 - add setdefault.
- 20140627 - add comma prefix to wait for a user click before redirecting to destination.
- 20130823 - fix OpenSearch auto discovery link to work for more browsers, incl. MSIE 8.
- 20130813 - add OpenSearch auto discovery link for some browsers (recent Firefox 23+).
- 20130210 - fix RecipeInfo key, reported by Peter Bowers.
- 20120721 - added $2.
- 20110408 - the keyword definitions can now be regular expressions if they are wrapped with '/' and an optional 'i' modifier.
- 20110112 - first public release after several months of usage by the author.
See also
Contributors
- Petko (5ko [snail] 5ko [period] fr) is the author and the maintainer of the recipe. I've been using and advocating in-browser search shortcuts for years but the idea to use a wiki page came to me when I found "DTRT: A CGI Script to Do The Right Thing" by Gerald Oskoboiny.
- If this recipe helps you or saves you time, you can help support its continued development by ♥ .
Comments
See discussion at DoTheRightThing-Talk
User notes +3: If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.