Super-phrase - automatic "phrase search" on DuckDuckGo.

Adds a verbatim tick-box to the DuckDuckGo search bar.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Super-phrase - automatic "phrase search" on DuckDuckGo.
// @namespace    https://github.com/josefandersson/userscripts/
// @version      1.2
// @description  Adds a verbatim tick-box to the DuckDuckGo search bar.
//               When ticked, anything typed in the box is auto-wrapped 
//               in quote marks and thus "searched as a phrase".
//		 Works on main home-page only. Use to form your initial phrase,
//               then you add additional search keywords and knock-outs in the 
//               search-box on the results page.
// @author       Josef Andersson, under paid contract to myself.
// @author       Working regex for "phrase wrapping" then supplied to me by 
//               Fechinsir on Fivver, again under paid contract to myself.
// @match        https://duckduckgo.com/*

// @run-at       document-end
// ==/UserScript==

if (location.pathname === '/') {
    const test = document.getElementById('content_homepage');
    if (test == null) {
        return;
    }

    let form = document.getElementById('search_form_homepage');
    let input = form.querySelector('input');

    form.addEventListener('submit', ev => {
        if (localStorage.getItem('forceVerbatim') === 'true') {
            input.value = input.value.replace(/(^.*$)/g, '"$1"');
        }
    }, {
        capture: true
    });

    const div = document.createElement('div');
    const label = document.createElement('label');
    const checkbox = document.createElement('input');
    label.innerText = 'Verbatim';
    label.htmlFor = 'forceVerbatim'
    checkbox.id = 'forceVerbatim';
    checkbox.type = 'checkbox';
    checkbox.checked = localStorage.getItem('forceVerbatim') === 'true';
    div.appendChild(label);
    div.appendChild(checkbox);
    Object.assign(div.style, { margin:'-30px -3px 0 0', color:'#888', fontSize:'.8em', textAlign:'right' });
    Object.assign(checkbox.style, {})

    form.parentElement.insertBefore(div, form);

    checkbox.addEventListener('change', _ => {
        localStorage.setItem('forceVerbatim', checkbox.checked);
    });
}