google-custom-search.pl 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Copyright (C) 2007 Alex Schroeder <alex@gnu.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. use strict;
  19. use v5.10;
  20. our ($q);
  21. our ($GoogleCustomSearchEngine);
  22. $GoogleCustomSearchEngine = 'http://www.google.com/cse?cx=004774160799092323420:6-ff2s0o6yi&q=';
  23. AddModuleDescription('google-custom-search.pl');
  24. # disable search form
  25. sub GetSearchForm {}
  26. # No more searching of titles
  27. *OldGoogleCustomGetSearchLink = \&GetSearchLink;
  28. *GetSearchLink = \&NewGoogleCustomGetSearchLink;
  29. sub NewGoogleCustomGetSearchLink {
  30. my ($text, $class, $name, $title) = @_;
  31. $text = NormalToFree($text);
  32. # It would be complicated to use ScriptLink here because of quoting
  33. # issues: ScriptUrl expects a quoted URL, which it then proceeds to
  34. # unquote again, etc. It's safer to just copy the necessary code
  35. # from ScriptLink.
  36. my %params;
  37. $params{'-rel'} = 'nofollow';
  38. $params{-href} = $GoogleCustomSearchEngine . UrlEncode(qq("$text"));
  39. $params{'-class'} = $class if $class;
  40. $params{'-name'} = UrlEncode($name) if $name;
  41. $params{'-title'} = $title if $title;
  42. return $q->a(\%params, $text);
  43. }
  44. *OldGoogleCustomGetHeader = \&GetHeader;
  45. *GetHeader = \&NewGoogleCustomGetHeader;
  46. sub NewGoogleCustomGetHeader {
  47. my $html = OldGoogleCustomGetHeader(@_);
  48. my $form = qq {
  49. <!-- Google CSE Search Box Begins -->
  50. <form class="tiny" action="http://www.google.com/cse" id="searchbox_004774160799092323420:6-ff2s0o6yi"><p>
  51. <input type="hidden" name="cx" value="004774160799092323420:6-ff2s0o6yi" />
  52. <input type="text" name="q" size="25" />
  53. <input type="submit" name="sa" value="Search" />
  54. </p></form>
  55. <script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=searchbox_004774160799092323420%3A6-ff2s0o6yi"></script>
  56. <!-- Google CSE Search Box Ends -->
  57. };
  58. $html =~ s{</span>}{</span>$form};
  59. return $html;
  60. }