Site Search for Arc Browser for Google users

Enables site search for Arc browser and others using shortcuts

  1. // ==UserScript==
  2. // @name Site Search for Arc Browser for Google users
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Enables site search for Arc browser and others using shortcuts
  6. // @author Faisal Bhuiyan
  7. // @match *://*/*
  8. // @grant none
  9. // @run-at document-start
  10. // @license MIT
  11. // @noframes
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. const start = performance.now();
  18. // List of search engines with their shortcuts and URLs
  19. // NOTE: Avoid adding your browser's default search engine to prevent redirect loops
  20. const searchEngines = [
  21. {
  22. shortcut: '@perplexity',
  23. url: 'https://perplexity.ai/search?q=%s'
  24. },
  25. {
  26. shortcut: '@youtube',
  27. url: 'https://www.youtube.com/results?search_query=%s'
  28. },
  29. {
  30. shortcut: '@morphic',
  31. url: 'https://morphic.sh/search?q=%s'
  32. },
  33. {
  34. shortcut: '@qwant',
  35. url: 'https://www.qwant.com/?q=%s'
  36. },
  37. {
  38. shortcut: '@phind',
  39. url: 'https://www.phind.com/search?q=%s'
  40. },
  41. {
  42. shortcut: '@yandex',
  43. url: 'https://yandex.com/search/?text=%s'
  44. }
  45. ];
  46.  
  47. // Fast hostname check using Set for O(1) lookup
  48. // NOTE: These domains should match the search engines above, excluding your default search engine
  49. const searchDomains = new Set([
  50. 'perplexity.ai',
  51. 'youtube.com',
  52. 'morphic.sh',
  53. 'qwant.com',
  54. 'phind.com',
  55. 'yandex.com'
  56. ]);
  57. const hostname = location.hostname;
  58. // Exit immediately if we're on a search engine
  59. if ([...searchDomains].some(domain => hostname.includes(domain))) return;
  60.  
  61. // Get query parameters fast
  62. const params = new URLSearchParams(window.location.search);
  63. const query = params.get('q') || params.get('search_query') || params.get('text');
  64. if (!query) return;
  65.  
  66. const trimmedQuery = query.trim();
  67. // Fast lookup using Array.find
  68. const engine = searchEngines.find(e => trimmedQuery.startsWith(e.shortcut));
  69. if (engine) {
  70. const searchQuery = trimmedQuery.replace(engine.shortcut, '').trim();
  71. if (searchQuery) {
  72. // Stop the page load as early as possible
  73. if (window.stop) window.stop();
  74. // Try to prevent any further resource loading
  75. document.documentElement.innerHTML = '';
  76. // Use the fastest redirection method
  77. const redirectUrl = engine.url.replace('%s', encodeURIComponent(searchQuery));
  78. location.replace(redirectUrl);
  79. // Prevent any further script execution
  80. throw new Error('REDIRECT_COMPLETE');
  81. }
  82. }
  83.  
  84. const end = performance.now();
  85. console.debug(`Arc Search executed in ${end - start}ms`);
  86. })();