Custom Validation

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

Tính đến 07-06-2022. Xem phiên bản mới nhất.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

Bạn sẽ cần cài đặt một tiện ích mở rộng như Tampermonkey hoặc Violentmonkey để cài đặt kịch bản này.

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

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

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

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Custom Validation
// @namespace    https://twitter.com/kumrnm
// @version      1.0
// @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);
                }
            }
        }
    });
})();