duckduckgo-search.pl 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # Copyright (C) 2007–2013 Alex Schroeder <alex@gnu.org>
  2. #
  3. # This program is free software: you can redistribute it and/or modify it under
  4. # the terms of the GNU General Public License as published by the Free Software
  5. # Foundation, either version 3 of the License, or (at your option) any later
  6. # version.
  7. #
  8. # This program is distributed in the hope that it will be useful, but WITHOUT
  9. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License along with
  13. # this program. If not, see <http://www.gnu.org/licenses/>.
  14. use strict;
  15. use v5.10;
  16. AddModuleDescription('duckduckgo-search.pl', 'Use DuckDuckGo For Searches');
  17. our ($q, %Action, $ScriptName, @MyInitVariables);
  18. our ($DuckDuckGoSearchDomain);
  19. $DuckDuckGoSearchDomain = undef;
  20. $Action{search} = \&DoDuckDuckGoSearch;
  21. push(@MyInitVariables, \&DuckDuckGoSearchInit);
  22. sub DuckDuckGoSearchInit {
  23. # If $ScriptName does not contain a hostname, this extension will
  24. # have no effect. Domain regexp based on RFC 2396 section 3.2.2.
  25. if (!$DuckDuckGoSearchDomain) {
  26. my $alpha = '[a-zA-Z]';
  27. my $alphanum = '[a-zA-Z0-9]';
  28. my $alphanumdash = '[-a-zA-Z0-9]';
  29. my $domainlabel = "$alphanum($alphanumdash*$alphanum)?";
  30. my $toplabel = "$alpha($alphanumdash*$alphanum)?";
  31. if ($ScriptName =~ m!^(https?://)?([^/]+\.)?($domainlabel\.$toplabel)\.?(:|/|\z)!) {
  32. $DuckDuckGoSearchDomain = $3;
  33. }
  34. }
  35. if ($DuckDuckGoSearchDomain
  36. and GetParam('search', undef)
  37. and not GetParam('action', undef)
  38. and not GetParam('old', 0)) {
  39. SetParam('action', 'search');
  40. }
  41. }
  42. sub DoDuckDuckGoSearch {
  43. my $search = UrlEncode(GetParam('search', undef));
  44. print $q->redirect({-uri=>"https://www.duckduckgo.com/?q=$search+site%3A$DuckDuckGoSearchDomain"});
  45. }