Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Add Mozilla-like keyword functionality to Safari's search bar' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Add Mozilla-like keyword functionality to Safari's search bar
Authored by: tokek on May 18, '05 06:01:30AM
Thanks for the tip. It's really brilliant since the hack still works for Safari 1.3. I've made my own changes (my first try at PHP):
  • parameter-less keywords (eg. "bbc" goes to BBC News)
  • Parse strings into any specified character encoding (cf. EUC-JP example)
    *Mozilla's can't do this.
  • more use of array lookups, fewer while loops, preg matching
  • uses stripslashes to undo automatic addslashes.

<?
/*
Add Mozilla-like keyword functionality to Safari's search bar.

Author: sapporo@land.ru (inspired by a hint from nimatoad at
http://www.macosxhints.com/article.php?story=20030514035516436)

TO DO: german umlauts don't show up the way they should at the
target site

There's another method of redirecting Safari searches by editing
/etc -> hosts (as proposed here:
http://www.macosxhints.com/article.php?story=20030509004243460),
but this seemed to be the easiest one. You'll have to modifiy
Safari.app after you update it (and pray they didn't change the
way searching works).

*/
/*
Originally from:
	http://www.macosxhints.com/article.php?story=20030519070642235

Some of the changes made:
	+ parameter-less keywords (eg. "bbc" goes to BBC News)
	+ Parse strings into any specified character encoding (cf. EUC-JP example)
	+ more use of array lookups, fewer while loops, preg matching
	+ uses stripslashes to undo automatic addslashes.
-- 20050518 Tokek
*/

// add your own keywords here
$keywords = array(
	"az"   => "http://www.amazon.com/exec/obidos/external-search?mode=blended&keyword=%query%",
	"azj"  => "http://www.amazon.co.jp/exec/obidos/external-search?mode=blended&keyword=%query%",
	"c"    => "http://www.%query%.com/",
	"d"    => "http://dictionary.reference.com/search?q=%query%",
	"en"   => "http://www.google.com/search?ie=UTF-8&oe=UTF-8&lr=lang_en&q=%query%",
	"f"    => "http://froogle.google.com/froogle?q=%query%",
	"g"    => "http://www.google.com/search?ie=UTF-8&oe=UTF-8&q=%query%",
	"gi"   => "http://images.google.com/images?ie=UTF-8&oe=UTF-8&safe=off&q=%query%",
	"gj"   => "http://www.google.com/search?ie=UTF-8&oe=UTF-8&lr=lang_ja&hl=ja&q=%query%",
	"gij"  => "http://images.google.com/images?ie=UTF-8&oe=UTF-8&safe=off&lr=lang_ja&hl=ja&q=%query%",
	"gji"  => "http://images.google.com/images?ie=UTF-8&oe=UTF-8&safe=off&lr=lang_ja&hl=ja&q=%query%",
	"m"    => "http://www.google.com/search?ie=UTF-8&oe=UTF-8&q=%query%+site:macosxhints.com",
	"q"    => "http://www.google.com/search?ie=UTF-8&oe=UTF-8&q=%22%query%%22",
	"w"    => "http://en.wikipedia.org/w/wiki.phtml?search=%query%&go=Go",
	"wj"   => "http://ja.wikipedia.org/wiki/%query%",
);

// shortword - self-made neologism meaning parameter-less custom keyword
$shortwords = array(
	"g"    => "http://www.google.com/",
	"ym"   => "http://mail.yahoo.com/",
	"gm"   => "http://gmail.google.com/gmail",
	"goo"   => "http://dictionary.goo.ne.jp/",
	"bbc"   => "http://news.bbc.co.uk/",
);


// the original search term
// remove slashes that were automatically added
$q = urldecode(stripslashes($_GET['q']));

// show help screen
if ("help" == $q) {
	echo "<html><body><center><h2>Search help:</h2><table>";
	while (list ($key, $location) = each ($keywords)) {
		echo "<tr><td>$key </td><td>$location</td></tr>\n";
	}
	echo "</table><p>Cmd+Option+F for the searchbox.";
	echo "<p>http://www.macosxhints.com/article.php?story=20030519070642235";
	echo "<p><a href='mailto:sapporo@land.ru'>sapporo@land.ru</a></body></html>";
	exit;
}

// E3 80 80 is ideographic space character (U+3000) in UTF-8 byte sequence
if (preg_match("/^([a-z]+)(\xE3\x80\x80|\s)(.*)/", $q, $matches)) {
	$key = $matches[1];
	$query = $matches[3];
	$location = $keywords[$key];
	if ($location != "") {
		$val = str_replace("%query%", urlencode($query), $location);
		header("Location: $val");
		exit;
	}
	if ($key == "goo") {
		$val = str_replace(
			"%query%", 
			//  url encode EUC-JP byte sequence obtained from reading UTF-8 byte sequence.
			urlencode(mb_convert_encoding($query, "EUC-JP", "UTF-8")),
			"http://dictionary.goo.ne.jp/search.php?MT=%query%&kind=&mode=0"
		);
		header("Location: $val");
		exit;
	}
}
else {
	// search for matching shortword and redirect upon success
	$location = $shortwords[$q];
	if ($location != "") {
		header("Location: $location");
		exit;
	}
}

// default to google.com
header("Location: http://www.google.com/search?&ie=UTF-8&oe=UTF-8&q=".urlencode($q));

?>


[ Reply to This | # ]
Update
Authored by: tokek on May 20, '05 06:32:12PM
Updated version:

<?
/*
Add Mozilla-like keyword functionality to Safari's search bar.

Author: sapporo@land.ru (inspired by a hint from nimatoad at
http://www.macosxhints.com/article.php?story=20030514035516436)

TO DO: german umlauts don't show up the way they should at the
target site

There's another method of redirecting Safari searches by editing
/etc -> hosts (as proposed here:
http://www.macosxhints.com/article.php?story=20030509004243460),
but this seemed to be the easiest one. You'll have to modifiy
Safari.app after you update it (and pray they didn't change the
way searching works).

*/
/*
Originally from:
	http://www.macosxhints.com/article.php?story=20030519070642235

Some of the changes made:
	+ parameter-less keywords (eg. "bbc" goes to BBC News)
	+ Parse strings into any specified character encoding (UTF-8 when unspecified)
	+ more use of array lookups, fewer while loops, preg matching
	+ uses stripslashes to undo the effects of automatic addslashes.
-- 20050520 Tokek
*/


// add your own keywords here

$keywords = array(
	"az"   => array("http://www.amazon.com/exec/obidos/external-search?mode=blended&keyword=%query%"),
	"azj"  => array("http://www.amazon.co.jp/exec/obidos/external-search?mode=blended&keyword=%query%"),
	"c"    => array("http://www.%query%.com/"),
	"en"   => array("http://www.google.com/search?ie=UTF-8&oe=UTF-8&lr=lang_en&q=%query%"),
	"f"    => array("http://froogle.google.com/froogle?q=%query%"),
	"g"    => array("http://www.google.com/search?ie=UTF-8&oe=UTF-8&q=%query%"),
	"gi"   => array("http://images.google.com/images?ie=UTF-8&oe=UTF-8&safe=off&q=%query%"),
	"gj"   => array("http://www.google.com/search?ie=UTF-8&oe=UTF-8&lr=lang_ja&hl=ja&q=%query%"),
	"gij"  => array("http://images.google.com/images?ie=UTF-8&oe=UTF-8&safe=off&lr=lang_ja&hl=ja&q=%query%"),
	"gji"  => array("http://images.google.com/images?ie=UTF-8&oe=UTF-8&safe=off&lr=lang_ja&hl=ja&q=%query%"),
	"m"    => array("http://www.google.com/search?ie=UTF-8&oe=UTF-8&q=%query%+site:macosxhints.com"),
	"q"    => array("http://www.google.com/search?ie=UTF-8&oe=UTF-8&q=%22%query%%22"),
	"wj"   => array("http://ja.wikipedia.org/wiki/%query%"),
	"d"    => array("http://dictionary.reference.com/search?q=%query%", "ISO-8859-1"),
	"w"    => array("http://en.wikipedia.org/w/wiki.phtml?search=%query%&go=Go", "ISO-8859-1"),
	"goo"  => array("http://dictionary.goo.ne.jp/search.php?MT=%query%&kind=jn&mode=0", "EUC-JP"),
);



// shortword - self-made neologism meaning parameter-less custom keyword
$shortwords = array(
	"g"    => "http://www.google.com/",
	"ym"   => "http://mail.yahoo.com/",
	"gm"   => "http://gmail.google.com/gmail",
	"goo"   => "http://dictionary.goo.ne.jp/",
	"bbc"   => "http://news.bbc.co.uk/",
);


// the original search term
// remove slashes that were automatically added
$q = urldecode(stripslashes($_GET['q']));

// show help screen
if ("help" == $q) {

	echo "<html><body><center><h2>Search help:</h2><table>";
	while (list ($key, $location) = each ($keywords)) {
		echo "<tr><td>$key </td><td>$location</td></tr>\n";
	}
	echo "</table><p>Cmd+Option+F for the searchbox.";
	echo "<p>http://www.macosxhints.com/article.php?story=20030519070642235";
	echo "<p><a href='mailto:sapporo@land.ru'>sapporo@land.ru</a></body></html>";
	exit;
}

// E3 80 80 is ideographic space character (U+3000) in UTF-8 byte sequence
if (preg_match("/^([a-z]+)(\xE3\x80\x80|\s)(.*)/", $q, $matches)) {
	$key = $matches[1];
	$query = $matches[3];
	list($location, $charset) = $keywords[$key];
	if ($location != "") {
		if ($charset != "") {
			$query = mb_convert_encoding($query, $charset, "UTF-8");
		}
		header("Location: $val".str_replace("%query%", urlencode($query), $location));
		exit;
	}
}
else {
	// search for matching shortword and redirect upon success
	$location = $shortwords[$q];
	if ($location != "") {
		header("Location: $location");
		exit;
	}
}

// default to google.com
header("Location: http://www.google.com/search?&ie=UTF-8&oe=UTF-8&q=".urlencode($q));

?>



[ Reply to This | # ]
Add Mozilla-like keyword functionality to Safari's search bar
Authored by: zazzman on Mar 05, '06 07:47:45AM

I love this tip! I installed it and was enjoying the Mozilla-like search bar goodness, but then I had to throw a wrench into the works. After doing this tip, I decided to install PHP5 and Apache2.

I used these two tutorials: http://www.phpmac.com/articles.php?view=237 and http://phpmac.com/articles.php?view=214.

Needless to say, Safari doesn't like this tip any more and when I attempt to do it, it just returns an error message saying it can't find search.php.

Thanks for any help you can give!



[ Reply to This | # ]