Apply Binarize Filter to Any Page

Inject and apply thread filter to the body of any webpage

目前為 2024-02-26 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Apply Binarize Filter to Any Page
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Inject and apply thread filter to the body of any webpage
// @author       hatrd
// @match        *://*/*
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_registerMenuCommand
// @run-at       document-end
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // 获取当前滤镜反转状态
    var filterInverted = GM_getValue('filterInverted', false);

    // 创建 SVG 和 filter 元素
    var svgHtml = `
    <svg id="filterSvg" height="0" style="position:absolute; width:0; height:0">
        <filter id="normalThreshold">
            <feColorMatrix type="matrix" values="0.21 0.72 0.07 0 0
                                                   0.21 0.72 0.07 0 0
                                                   0.21 0.72 0.07 0 0
                                                   0    0    0    1 0" />
            <feComponentTransfer>
                <feFuncR type="discrete" tableValues="0 1" />
                <feFuncG type="discrete" tableValues="0 1" />
                <feFuncB type="discrete" tableValues="0 1" />
            </feComponentTransfer>
        </filter>
        <filter id="invertedThreshold">
            <feColorMatrix type="matrix" values="0.21 0.72 0.07 0 0
                                                   0.21 0.72 0.07 0 0
                                                   0.21 0.72 0.07 0 0
                                                   0    0    0    1 0" />
            <feComponentTransfer>
                <feFuncR type="discrete" tableValues="1 0" />
                <feFuncG type="discrete" tableValues="1 0" />
                <feFuncB type="discrete" tableValues="1 0" />
            </feComponentTransfer>
        </filter>
    </svg>
    `;

    // 将 SVG 插入页面中
    document.body.insertAdjacentHTML('beforeend', svgHtml);

    // 应用滤镜到 body
    applyFilter();

    // 注册菜单命令以切换滤镜反转
    GM_registerMenuCommand("Toggle Filter Inversion", function() {
        filterInverted = !filterInverted;
        GM_setValue('filterInverted', filterInverted); // 更新存储的状态
        applyFilter(); // 应用新的滤镜
    }, "t");

    function applyFilter() {
        // 根据 filterInverted 的值选择使用哪一个滤镜
        document.body.style.filter = filterInverted ? 'url(#invertedThreshold)' : 'url(#normalThreshold)';
    }
})();