🎉 欢迎访问GreasyFork.Org 镜像站!本镜像站由公众号【爱吃馍】搭建,用于分享脚本。联系邮箱📮

Greasy fork 爱吃馍镜像

Greasy Fork is available in English.

📂 缓存分发状态(共享加速已生效)
🕒 页面同步时间:2026/01/20 18:53:22
🔄 下次更新时间:2026/01/20 19:53:22
手动刷新缓存

Protect Textarea

Protect from closing or navigating away from a web page with changed textareas.

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

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

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==UserScript==
// @name        Protect Textarea
// @namespace   https://arantius.com/misc/greasemonkey/
// @description Protect from closing or navigating away from a web page with changed textareas.
// @include     http*
// @version     1.3
// ==/UserScript==

// Version History:
//
// 1.3   (2011-06-06): Use pagehide/pageshow events for cache friendliness.
//                     https://developer.mozilla.org/en-US/Firefox/Releases/1.5/Using_Firefox_1.5_caching#Page_caching_despite_unload_and_beforeunload_handlers
// 1.2.1 (2010-02-16): Reduce false-positive warning rate.
// 1.2   (2009-09-15): Complete rewrite, less false positives for hidden
//                     fields controlled by scripts, better handling of
//                     dynamic/ajax sites.
// 1.1   (2006-09-18): Add the "noprotect" class detection
//

var CHANGED_MARK=String(Math.random()).substr(2);

window.addEventListener('keypress', handleKeypress, true);
window.addEventListener('submit', handleSubmit, true);
window.addEventListener('beforeunload', handleUnload, true);

window.addEventListener('pageshow', function() {
  window.addEventListener('beforeunload', handleUnload, true);
}, true);

function handleKeypress(event) {
	if (
		// Ignore events not in a textarea.
		!event.target
		|| !event.target.tagName
		|| 'TEXTAREA'!=event.target.tagName
		// Ignore non-character keypresses.
		|| 0==event.charCode
		// Ignore "noprotect" textareas.
		|| event.target.className.match(/\bnoprotect\b/)
	) {
		return;
	}

	if (0 /* debug? */) {
		console.log('saw keypress in', event.target);
		console.dir(event);
	}

	// At this point we have noticed a keypress to a textarea.  Record it.
	var textarea = event.target;
	textarea.setAttribute('changed_mark', CHANGED_MARK);
	if (!textarea.hasAttribute('orig_value')) {
		textarea.setAttribute('orig_value', textarea.value);
	}
}

function handleSubmit(event) {
	var textareas = event.target.getElementsByTagName('textarea');
	for (var i=0, textarea=null; textarea=textareas[i]; i++) {
		textarea.removeAttribute('changed_mark');
	}
}

function handleUnload(event) {
  // Re-add later via pageshow.  See https://developer.mozilla.org/en-US/Firefox/Releases/1.5/Using_Firefox_1.5_caching#Page_caching_despite_unload_and_beforeunload_handlers
  window.removeEventListener('beforeunload', handleUnload, true);
	
	var textareas = event.target.getElementsByTagName('textarea');
	
	textarealoop:
	for (var i=0, textarea=null; textarea=textareas[i]; i++) {
		// Check for presence in the document.
		var parent=textarea.parentNode;
		while (true) {
			if ('BODY'==parent.tagName) break;
			// Skip if we climbed the parent tree and fell out before getting to the
			// <body>; this textarea was removed from the document, probably by the
			// script that submitted it via AJAX (or some such).
			if (!parent || !parent.tagName) continue textarealoop;
			parent=parent.parentNode;
		}

		// Skip if we haven't marked this as changed.
		if (!textarea.hasAttribute('changed_mark')) continue;
		if (textarea.getAttribute('changed_mark') != CHANGED_MARK) continue;
		
		// Skip if the value is the same as the first we observed.  (I.E. only
		// arrow or Ctrl-C keypresses.)
		if (textarea.value == textarea.getAttribute('orig_value')) continue;
		
		// Skip if the value is empty. (Nothing to lose!)
		if ('' == textarea.value) continue;
		
		// Didn't skip, so do interrupt leaving the page.
		return event.returnValue='You have modified a textarea, and ' +
			'have not submitted the form.';
	}
}