DuckDuckGo on Yandex

Replaces Bing links with DuckDuckGo on Yandex search results page and updates the link text

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         DuckDuckGo on Yandex
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  Replaces Bing links with DuckDuckGo on Yandex search results page and updates the link text
// @author       nnside
// @match        https://yandex.ru/search/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to replace links
    function replaceBingLinks() {
        // Get all link elements on the page
        const links = document.querySelectorAll('a');

        // Iterate through all links
        links.forEach(link => {
            // Check if the link contains the Bing address
            if (link.href.includes("bing.com")) {
                // Replace the link with DuckDuckGo
                const searchText = new URLSearchParams(window.location.search).get('text');
                link.href = `https://duckduckgo.com/?q=${encodeURIComponent(searchText)}`;
                link.target = '_blank';  // Open in a new tab

                // Change the link text to "DuckDuckGo"
                link.textContent = link.textContent.replace("Bing", "DuckDuckGo");
            }
        });
    }

    // Create a MutationObserver to watch for changes in the document
    const observer = new MutationObserver((mutations) => {
        mutations.forEach(() => {
            replaceBingLinks(); // Call the function each time the DOM changes
        });
    });

    // Start observing the body for child additions
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    // Initial call to replace links when the script runs
    replaceBingLinks();

})();