Guest User

Untitled

a guest
Feb 21st, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var engineLink  = null;
  2. // Search Engines
  3. var engines = [
  4.     //  [Key Code], [Search URL],   [Home Page Link],   [Favicon]
  5.     ["",    "https://www.google.com/#q=",                       "https://www.google.com",           "https://www.google.com/favicon.ico"],
  6.     ["!d",  "https://www.duckduckgo.com/?q=",                   "https://www.duckduckgo.com",       "http://www.cfrank.org/f/0RIxiU.ico"],
  7.     ["!g",  "https://www.google.com/#q=",                       "https://www.google.com",           "https://www.google.com/favicon.ico"],
  8.     ["!t",  "https://translate.google.com/?vi=",                "https://translate.google.com/",    "https://translate.google.com/favicon.ico"],
  9.     ["!i",  "https://www.google.com/search?tbm=isch&q=",        "https://www.images.google.com",    "https://www.google.com/favicon.ico"],
  10.     ["!y",  "https://www.youtube.com/results?search_query=",    "https://www.youtube.com",          "https://youtube.com/favicon.ico"],
  11.     ["!w",  "https://en.wikipedia.org/w/index.php?search=",     "https://www.en.wikipedia.org",     "http://en.wikipedia.org/favicon.ico"]
  12. ];
  13.  
  14. // Handle Search
  15. function query(e, v)
  16. {
  17.     var key         = e.keyCode || e.which,
  18.         input       = document.getElementById("q");
  19.     // Check for search engine
  20.     if(v.length === 2 && key !== 13 && v.lastIndexOf("!") !== -1)
  21.     {
  22.         var en = v.lastIndexOf("!"); // Engine Selected
  23.         for(var i = 0; i < engines.length; ++i)
  24.         {
  25.             if(engines[i][0] === v.substr(en))
  26.             {
  27.                 e.preventDefault(); // Remove the leading space on the query
  28.                 engineLink                  = engines[i][1];
  29.                 var engineIcon              = '<a href="' + engines[i][2] + '"><img src="' + engines[i][3] + '" width="16" height="16" /></a>',
  30.                     grabFavIcon             = document.getElementById("favicon");
  31.                 grabFavIcon.innerHTML       = engineIcon;
  32.                 grabFavIcon.style.opacity   = "1";
  33.                 input.value = "";
  34.             }
  35.         }
  36.     }
  37.     // On enter
  38.     if(key === 13)
  39.     {
  40.         if(engineLink != null && engineLink === engines[3][1])
  41.         {
  42.             var lang    = ["ru", "en"];
  43.             if(v.match(/^[\x20-\x7E]+$/))
  44.             {
  45.                 // English Language ~ EN - RU
  46.                 window.location = engineLink + 'c#' + lang[1] + '/' + lang[0] + '/' + v;
  47.             }
  48.             else if(v.match(/[\u0400-\u04FF]/))
  49.             {
  50.                 // Russian Language ~ RU - EN
  51.                 window.location = engineLink + 'c#' + lang[0] + '/' + lang[1] + '/' + v;
  52.             }
  53.             else
  54.             {
  55.                 // Unknown Language ~ AU - EN
  56.                 window.location = engineLink + 'c#auto/en/' + v;
  57.             }
  58.         }
  59.         else if(engineLink != null && engineLink === engines[3][1])
  60.         {
  61.             var url     = "https://www.google.com/searchbyimage?image_url=",
  62.                 pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
  63.                             '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
  64.                             '((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
  65.                             '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
  66.                             '(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
  67.                             '(\\#[-a-z\\d_]*)?$','i'); // fragment locator
  68.                 // Is not a link
  69.                 if(!pattern.test(v))
  70.                 {
  71.                     window.location = engineLink + v;
  72.                 }
  73.                 // Is a link
  74.                 else
  75.                 {
  76.                     window.location = url + v;
  77.                 }
  78.         }
  79.         else if(engineLink != null)
  80.         {
  81.             window.location = engineLink + v;
  82.         }
  83.         else
  84.         {
  85.             window.location = engines[0][1] + v;
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment