Startpage Domain Filter

Filter Startpage search results on domain

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Startpage Domain Filter
// @namespace    https://news.ycombinator.com/
// @version      1.1
// @license      GPLv3
// @description  Filter Startpage search results on domain
// @author       xdpirate
// @match        https://www.startpage.com/sp/search*
// @match        https://www.startpage.com/do/search*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=startpage.com
// @run-at       document-end
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_addStyle
// ==/UserScript==

window.setTimeout(function() {
    GM_addStyle(`
        #SPDFToggleButton {
            cursor: pointer;
        }

        #SPDFBlacklistArea {
            background-color: black;
            color: white;
            font-family: monospace;
        }

        #SPDFOuterDiv {
            float: left;
            background-color: black;
            color: white;
            padding: 5px;
            border: 1px solid white;
            border-radius: 10px;
            z-index: 2147483647;
            position: absolute;
            top: 50px;
            left: 100px;
        }

        #SPDFCloseButton {
            cursor: pointer;
        }

        .hidden {
            display: none;
        }
    `);

    let blacklist = GM_getValue("blacklist", ["quora.com"]);

    let empty = false;
    if(blacklist.length == 0 || (blacklist.length == 1 && blacklist[0] == "") || blacklist == undefined || blacklist == null) {
        empty = true;
    }

    let regexStr = "^https?:\\/\\/(.+\\.)?(";
    if(!empty) {
        for(let j = 0; j < blacklist.length; j++) {
            // Don't start with a pipe
            if(j > 0) {
                regexStr = regexStr + "|";
            }
            regexStr = regexStr + blacklist[j].replaceAll(".","\\.");
        }
    }
    regexStr = regexStr + ")\\/";

    let regex = new RegExp(regexStr, "i");

    let newBox = document.createElement("div");
    newBox.innerHTML = `
        <div id="SPDFOuterDiv" class="hidden">
            <div id="SPDFInnerDiv">
                <span id="SPDFCloseButton" title="Close">❌</span> <b>Startpage Domain Filter</b><br />
                Comma-separated list of blacklisted domains:<br />
                <textarea id="SPDFBlacklistArea" rows="10" cols="80";">${blacklist}</textarea><br />
                <input type="button" value="Save and reload" id="SPDFSaveButton"></input>
            <div>
        </div>
    `;

    document.body.append(newBox);

    document.getElementById("SPDFCloseButton").onclick = function() {
        document.getElementById("SPDFOuterDiv").classList.toggle("hidden");
    };

    document.getElementById("SPDFBlacklistArea").onkeydown = function(e) {
        if (e.key === "Escape") {
            document.getElementById("SPDFOuterDiv").classList.add("hidden");
        }
    };

    document.getElementById("SPDFSaveButton").onclick = function() {
        GM_setValue("blacklist", document.getElementById("SPDFBlacklistArea").value.split(","));
        location.reload();
    };

    let c = 0;
    if(!empty) {
        let results = document.querySelectorAll("div.result");
        if(results.length == 0) {
            results = document.querySelectorAll("div.w-gl__result");
        }

        for(let i = 0; i < results.length; i++) {
            let resultURL = results[i].querySelector("div.upper > a > span.link-text");
            if(!resultURL) {
                resultURL = results[i].querySelector("a.result-link").href;
            } else {
                resultURL = resultURL.innerText;
            }
            resultURL = resultURL.trim();

            if(regex.test(resultURL)) {
                results[i].style.display = "none";
                c++;
            }
        }
        console.log(`SPDF hid ${c} posts on this page (${Math.round((c / results.length) * 100)}%)`);
    }

    let pageTop = document.querySelector("div#filters-container > div.filters");
    if(!pageTop) {
        pageTop = document.querySelector("div.search-filters-toolbar__filters-container");
    }
    let filterLink = document.createElement("div");

    filterLink.innerHTML = `<div style="cursor: pointer; border-radius: 10px; vertical-align: center; padding: 10px; background: #000; color: #fff; border: 2px solid #fff;">Filter (${c})</div>`;
    filterLink.onclick = function() {
        document.getElementById("SPDFOuterDiv").classList.toggle("hidden");
    };
    pageTop.appendChild(filterLink);
}, 2000);