[英]Greasemonkey script gives error on wrong inputs
我有一個Greasemonkey腳本,其邏輯如下:
除非已登錄的用戶在特權組(JOHN,LUKE,JEFF,MAX和ANDY)中,否則禁用<input>
並使用name="prio"
。
如果不是特權用戶,請阻止<input name="dest">
選擇值。
具體來說,在塊“ 12”或“ 22”中寫一條錯誤消息。
該腳本可以正常運行,但存在一個錯誤:
當我在其他輸入字段中插入文本或更改選擇字段並按Enter時 ,提交停止,並且在控制台中收到錯誤消息:
錯誤:destInput [0]沒有屬性源文件:file:/// E:/FirefoxPortable2.x/Data/profile/extensions/%7Be4a8a97b-f2ed-450b-b12d-ee082ba24781%7D/components/greasemonkey.js行: 379
var err = this.findError(script,line-lineFinder.lineNumber-1);
我認為name="dest"
的規則適用於所有輸入字段和選擇字段? 但是我只希望“輸入”規則。
這是我的腳本代碼 :
// ==UserScript==
// @name _SO Block submit on custom FF2 page.
// @namespace PC
// @include file:///D:/temp/foo%20SO/task*
// @include file:///D:/temp/foo%20SO/fixture*
// @include file:///D:/temp/foo%20SO/pallet*
// ==/UserScript==
GM_log ("Script start.");
unsafeWindow._oldSubmit = unsafeWindow.document.forms[0].submit;
unsafeWindow.document.forms[0].submit = function () {
GM_log ("Submit function fired.");
var destInput = document.getElementsByName ("dest");
if ( ! destInput || destInput.length == 0) {
//unsafeWindow._oldSubmit ();
}
var destValue = destInput[0].value;
if ( /^\s*$/.test (destValue)
|| excludedDestinations.indexOf (destValue) > -1
) {
GM_log ("Submit should be blocked! (1)");
}
else {
//unsafeWindow._oldSubmit ();
}
};
//--- Make sure this list of names is all uppercase.
var usersWhoCanSetPriority = ['JOHN', 'LUKE', 'JEFF', 'MAX', 'ANDY'];
var excludedDestinations = ['12', '22'];
var bDisablePrio = true;
var tdNodes = document.getElementsByTagName ("TD");
for (var J = tdNodes.length - 1; J >= 0; --J) {
var tdNode = tdNodes[J];
if (tdNode.className == "user") {
var userName = tdNode.textContent.replace (
/^(?:.|\n|\r)+\(User:\s+([^)]+)\)(?:.|\n|\r)+$/i, "$1"
).toUpperCase ();
if (usersWhoCanSetPriority.indexOf (userName) > -1) {
bDisablePrio = false;
}
}
}
if (bDisablePrio) {
var oldInput = document.getElementsByName ("prio");
if (oldInput && oldInput.length) {
oldInput[0].setAttribute ("disabled", "disabled");
}
var destInput = document.getElementsByName ("dest");
if (destInput && destInput.length) {
destInput[0].addEventListener (
"change",
function (zEvent) {
bCheckdestinationValue (destInput[0]);
GM_log ("Change handler fired.");
},
false
);
destInput[0].form.addEventListener (
"submit",
function (zEvent) {
GM_log ("Submit handler fired.");
var destValue = destInput[0].value;
if ( /^\s*$/.test (destValue)
|| excludedDestinations.indexOf (destValue) > -1
) {
//--- Stop the submit
zEvent.preventDefault ();
//zEvent.stopPropagation ();
GM_log ("Submit should be blocked! (2)");
return false;
}
},
true
);
}
}
function bCheckdestinationValue (destInputNd) {
//--- Returns true if value is bad.
if (excludedDestinations.indexOf (destInputNd.value) > -1) {
destInputNd.value = ''; // Blank input
//--- Add or show Error message.
var destErrNode = document.getElementById ("gmDestErrorDisp");
if (destErrNode) {
destErrNode.style.display = "inline";
}
else {
destErrNode = document.createElement ('b');
destErrNode.id = "gmDestErrorDisp";
destErrNode.style.color = "red";
destErrNode.textContent = "12 and 22 are forbidden";
destInputNd.parentNode.appendChild (destErrNode);
}
return true;
}
else {
var destErrNode = document.getElementById ("gmDestErrorDisp");
if (destErrNode) {
destErrNode.style.display = "none";
}
}
return false;
}
PS:pastebin代碼(unsafeWindow ..)是我的舊系統上唯一可用的版本。 系統運行FF 2.0.0.11和Greasemonkey 0.8,無法更新。
有沒有辦法消除這個錯誤? :D
將表單[0] .submit代碼更改為:
if (unsafeWindow.document.forms[0]) {
unsafeWindow._oldSubmit = unsafeWindow.document.forms[0].submit;
unsafeWindow.document.forms[0].submit = function () {
GM_log ("Submit function fired.");
var destInput = document.getElementsByName ("dest");
if ( ! destInput || destInput.length == 0) {
unsafeWindow._oldSubmit ();
}
else {
var destValue = destInput[0].value;
if ( /^\s*$/.test (destValue)
|| excludedDestinations.indexOf (destValue) > -1
) {
GM_log ("Submit should be blocked! (1)");
}
else {
unsafeWindow._oldSubmit ();
}
}
};
}
它可以確保在使用前確保存在的存在。
另外,取消注釋此行:
//zEvent.stopPropagation ();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.