所以我做了这个IP脚本,当你上了我的网站,它显示你的IP。
我想做的是做一个简单的IP阻止器,我想让它找到包含IP地址的标题的值作为文本,并使它使如果ipheader.text或什么==“goodip”报警(“Right IP!”);else window.location=“something”
...我知道这是糟糕的JavaScript,但它只是一个示例。
这是我的当前代码。
<h3 id="testIp"></h3>
<script>
function text(url) {
return fetch(url).then(res => res.text());
}
text('https://www.cloudflare.com/cdn-cgi/trace').then(data => {
let ipRegex = /[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}/
let ip = data.match(ipRegex)[0];
document.getElementById('testIp').innerHTML = (ip);
});
-->NEED HELP PAST HERE<---
function validate() {
if (document.getElementById("testIp").innerHTML == "The Right IP") {
alert("RIGHT IP!");
} else {
alert('WRONG IP!');
}
}
</script>
谢谢!
您可以更改一些内容来更正代码。
\.
).textContent
仅从
中提取原始文本。===
)null
const
PATTERN_IP_ADDRESS = /[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/,
blockedIpAddress = '127.0.0.1';
const fetchText = url => fetch(url).then(res => res.text());
const validate = () => {
const testIp = document.getElementById('testIp');
const ipAddress = testIp.textContent;
if (ipAddress === blockedIpAddress) {
console.log('RIGHT IP!');
} else {
console.log('WRONG IP!');
testIp.classList.add('wrong');
}
}
fetchText('https://www.cloudflare.com/cdn-cgi/trace').then(text => {
const [ ip ] = text.match(PATTERN_IP_ADDRESS);
document.getElementById('testIp').innerHTML = ip;
validate();
});
.wrong { color: red; }
<h3 id="testIp"></h3>