Site Search for Arc Browser

Enables site search for Arc browser and others using shortcuts

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

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

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

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

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

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==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`);
})();