Site Search for Arc Browser

Enables site search for Arc browser and others using shortcuts

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

ستحتاج إلى تثبيت إضافة مثل Stylus لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتتمكن من تثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

(لدي بالفعل مثبت أنماط للمستخدم، دعني أقم بتثبيته!)

// ==UserScript==
// @name         Site Search for Arc Browser
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Enables site search for Arc browser and others using shortcuts
// @author       Faisal Bhuiyan
// @match        *://*/*
// @grant        none
// @run-at       document-start
// @noframes
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const start = performance.now();
    
    // List of search engines with their shortcuts and URLs
    // NOTE: Avoid adding your browser's default search engine to prevent redirect loops
    const searchEngines = [
        {
            shortcut: '@perplexity',
            url: 'https://perplexity.ai/search?q=%s'
        },
        {
            shortcut: '@g',
            url: 'https://www.google.com/search?q=%s'
        },
        {
            shortcut: '@youtube',
            url: 'https://www.youtube.com/results?search_query=%s'
        },
        {
            shortcut: '@morphic',
            url: 'https://morphic.sh/search?q=%s'
        },
        {
            shortcut: '@qwant',
            url: 'https://www.qwant.com/?q=%s'
        },
        {
            shortcut: '@phind',
            url: 'https://www.phind.com/search?q=%s'
        },
        {
            shortcut: '@yandex',
            url: 'https://yandex.com/search/?text=%s'
        }
    ];

    // Fast hostname check using Set for O(1) lookup
    // NOTE: These domains should match the search engines above, excluding your default search engine
    const searchDomains = new Set([
        'perplexity.ai',
        'google.com',
        'youtube.com',
        'morphic.sh',
        'qwant.com',
        'phind.com',
        'yandex.com'
    ]);
    
    const hostname = location.hostname;
    
    // Exit immediately if we're on a search engine
    if ([...searchDomains].some(domain => hostname.includes(domain))) return;

    // Get query parameters fast
    const params = new URLSearchParams(window.location.search);
    const query = params.get('q') || params.get('search_query') || params.get('text');
    
    if (!query) return;

    const trimmedQuery = query.trim();
    
    // Fast lookup using Array.find
    const engine = searchEngines.find(e => trimmedQuery.startsWith(e.shortcut));
    
    if (engine) {
        const searchQuery = trimmedQuery.replace(engine.shortcut, '').trim();
        if (searchQuery) {
            // Stop the page load as early as possible
            if (window.stop) window.stop();
            
            // Try to prevent any further resource loading
            document.documentElement.innerHTML = '';
            
            // Use the fastest redirection method
            const redirectUrl = engine.url.replace('%s', encodeURIComponent(searchQuery));
            location.replace(redirectUrl);
            
            // Prevent any further script execution
            throw new Error('REDIRECT_COMPLETE');
        }
    }

    const end = performance.now();
    console.debug(`Arc Search executed in ${end - start}ms`);
})();