Custom Validation

入力欄に禁止語句を入力した際に警告文を表示する

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Custom Validation
// @namespace    https://twitter.com/kumrnm
// @version      1.0.1
// @description  入力欄に禁止語句を入力した際に警告文を表示する
// @author       蝙蝠の目
// @license      MIT
// @match        *://*/*
// ==/UserScript==

(function() {
    'use strict';

    // 検出する語句をここで指定します(記述方法は「正規表現」でググってください)
    const pattern = /あいうえおあいうえお/;

    const excludes = new Set();

    window.addEventListener("keyup", () => {
        const elm = document.activeElement;
        if (!elm) return;
        if (excludes.has(elm)) return;
        if (elm.tagName === "INPUT" || elm.tagName === "TEXTAREA") {
            const match = elm.value.match(pattern);
            if (match) {
                if (window.confirm("[Custom Validation]\n禁止語句の入力を検出しました。削除しますか?\n(「いいえ」を選択すると、ページを閉じるまでこの入力欄に対する検査を停止します。)")) {
                    elm.value = elm.value.substring(0, match.index) + elm.value.substring(match.index + match[0].length);
                } else {
                    excludes.add(elm);
                }
            }
        }
    });

    console.log("[Custom Validation] Activated.");
})();