我对JavaScript或jQuery没有多少经验。
我试图使用Tamper猴子自动更正MAC地址的输入字段。
该站点需要一个格式化为00:00:00:00:00:00的MAC地址。
因此,我为Tamper猴子编写了这个脚本,以便在我输入时自动添加冒号:
// ==UserScript==
// @name Name
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Adds colons to the mac adress of the Mac Field
// @author You
// @match Somesite
// @grant none
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==
document.getElementById("MAC").addEventListener('keyup', function() {
var mac = document.getElementById('MAC').value;
var macs = mac.split('');
var colons = ["2", "5", "8", "11", "14"];
for (var col in colons) {
if (macs[col] == "-") {
macs[col] = ":";
} else if (macs[col] != "") {
} else if (macs[col] != ":") {
var colo = col + 1;
macs[colo] = macs[col];
macs[col] = ":";
}
}
mac = macs.toString();
});<input id=MAC />
但不管用。输入字段的ID是MAC。
我在哪里做了多少错事?
解决方案
感谢我-摔跤-熊-一次和@freginold为我的观点,最好的解决方案。
我现在使用的是@freginold略有改变的版本
var back = true;
function isHex(char) {
// check if char is a hex char
if (!isNaN(parseInt(char))) {
return true;
} else {
switch (char.toLowerCase()) {
case "a":
case "b":
case "c":
case "d":
case "e":
case "f":
return true;
}
return false;
}
}
document.getElementById("MAC").addEventListener('keydown', function() {
var key = event.keyCode || event.charCode;
if( key == 8 || key == 46 ) {
back = false;
}
});
document.getElementById("MAC").addEventListener('keyup', function() {
var key = event.keyCode || event.charCode;
var mac = document.getElementById('MAC').value;
var newMac = mac.replace("-", ""); // remove any dashes
if ((isHex(mac[mac.length - 1]) && (isHex(mac[mac.length - 2])) && (mac.length <= 16) && (back))) {
// if last two chars are numbers, insert a colon
newMac = newMac + ":";
}
back = true;
document.getElementById('MAC').value = newMac; // put new value into input field
});发布于 2018-01-24 14:07:17
您可以简化它,并检查字符串中的最后两个字符是否为十六进制字符(0-9,a),如果是,则插入一个:。如果您(或其他人)键入破折号而不是冒号,您还可以使用.replace()删除任何出现的-。
这样,您就可以包括插入冒号,如果您根本不键入它们,也可以将任何类型的破折号转换为冒号。
下面是一个有用的例子:
// ==UserScript==
// @name Name
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Adds colons to the mac adress of the Mac Field
// @author You
// @match Somesite
// @grant none
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==
function isHex(char) {
if (!isNaN(parseInt(char))) {
return true;
} else {
switch (char.toLowerCase()) {
case "a":
case "b":
case "c":
case "d":
case "e":
case "f":
return true;
break;
}
return false;
}
}
document.getElementById("MAC").addEventListener('keyup', function() {
var mac = document.getElementById('MAC').value;
if (mac.length < 2) {
return;
}
var newMac = mac.replace("-", "");
if ((isHex(mac[mac.length - 1]) && (isHex(mac[mac.length - 2])))) {
newMac = newMac + ":";
}
document.getElementById('MAC').value = newMac;
});
document.getElementById('MAC').focus(); // autofocus for testing<input id=MAC />
发布于 2018-01-24 14:07:16
replace(/[^\d|A-Z]/g, '')移除任何非字母数字字符match(/.{1,2}/g)将字符串分解为2的块。join(":")加入这些块并在它们之间放置一个冒号。
// ==UserScript==
// @name Name
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Adds colons to the mac adress of the Mac Field
// @author You
// @match Somesite
// @grant none
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==
document.getElementById("MAC").addEventListener('keyup', function() {
// remove non digits, break it into chunks of 2 and join with a colon
this.value =
(this.value.toUpperCase()
.replace(/[^\d|A-Z]/g, '')
.match(/.{1,2}/g) || [])
.join(":")
});<input id=MAC />
发布于 2018-01-24 14:04:46
您可以使用函数chunk:(不是我的代码,我只是包含了它,并修改了您的代码以使用它。)
function chunk(str, n) {
var ret = [];
var i;
var len;
for(i = 0, len = str.length; i < len; i += n) {
ret.push(str.substr(i, n))
}
return ret
};那么您的代码应该如下所示:
document.getElementById("MAC").addEventListener('keyup', function() {
var mac = document.getElementById('MAC').value;
var macs = mac.split(':').join('');
macs = chunk(macs, 2).join(':');
document.getElementById('MAC').value = macs.toString();
});演示
// ==UserScript==
// @name Name
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Adds colons to the mac adress of the Mac Field
// @author You
// @match Somesite
// @grant none
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==
document.getElementById("MAC").addEventListener('keyup', function() {
var mac = document.getElementById('MAC').value;
var macs = mac.split(':').join('');
macs = chunk(macs, 2).join(':');
document.getElementById('MAC').value = macs.toString();
});
function chunk(str, n) {
var ret = [];
var i;
var len;
for(i = 0, len = str.length; i < len; i += n) {
ret.push(str.substr(i, n))
}
return ret
};<input id=MAC maxlength="17"/>
https://stackoverflow.com/questions/48424006
复制相似问题