下列代码点击出现无效请进行代码重新编写并且写成优化后的注释scriptfunction checkLink const maxAttempts = 3; const today = new Date; todaysetHours0 0 0 0; const expires = new DatetodaygetTime + 24 60 60 1000; const attem
// 定义常量,最大尝试次数为3次 const maxAttempts = 3;
// 获取当天日期,将时分秒毫秒均设置为0 const today = new Date(); today.setHours(0, 0, 0, 0);
// 设置cookie过期时间为当天结束 const expires = new Date(today.getTime() + (24 * 60 * 60 * 1000));
// 获取尝试次数 function getAttempts() { const attempts = getCookie("attempts"); // 获取cookie中的attempts值 return attempts === null ? 0 : Number(attempts); // 如果获取不到,则默认为0次尝试 }
// 设置尝试次数cookie
function setAttempts(value, expires) {
const cookieOptions = { expires, path: '/', sameSite: 'lax' }; // 设置cookie参数
const signedValue = signCookie(value); // 对value进行签名
document.cookie = attempts=${signedValue}; ${cookieOptions}; // 设置cookie
}
// 获取cookie
function getCookie(name) {
const pattern = new RegExp((?:(?:^|.*;)\\s*${name}\\s*\\=\\s*([^;]*).*$)|^.*$); // 正则表达式匹配cookie
return decodeURIComponent(document.cookie.replace(pattern, "$1")) || null; // 返回匹配的cookie值
}
// 对cookie值进行签名
function signCookie(value) {
const secret = 'mySecretKey'; // 定义密钥
const message = value.toString(); // 将value转化为字符串
const hmac = crypto.subtle.importKey('raw', new TextEncoder().encode(secret), { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']) // 使用HMAC-SHA256算法进行签名
.then((key) => {
return crypto.subtle.sign({ name: 'HMAC', hash: 'SHA-256' }, key, new TextEncoder().encode(message)); // 对message进行签名
})
.then((signature) => {
const hexSignature = Array.from(new Uint8Array(signature)).map((b) => b.toString(16).padStart(2, '0')).join(''); // 将签名结果转化为16进制字符串
return ${message}.${hexSignature}; // 返回签名结果
})
.catch(() => {
return value; // 如果签名出错,则返回原始值
});
return hmac;
}
// 判断链接是否有效 function isValidLink(link) { const pattern = /^https://.+/; // 链接格式正则表达式 return pattern.test(link); // 判断是否符合格式要求 }
// 显示loading动画 function showLoading() { const loading = document.createElement('div'); // 创建loading元素 loading.innerHTML = 'Loading...'; // 设置loading文本 document.body.appendChild(loading); // 将loading元素添加到body中 }
// 进行链接安全检查 function checkLinkSecurity(link) { return new Promise((resolve, reject) => { // 执行安全检查 // 如有问题,使用错误消息拒绝promise // 否则,解决promise const isSafe = true; // placeholder value,此处为了演示直接赋值为true if (isSafe) { resolve(); } else { reject(new Error('输入的链接存在安全风险,请重新输入。')); } }); }
// 检查链接是否有效
function checkLink() {
const attempts = getAttempts(); // 获取当前尝试次数
if (attempts >= maxAttempts) { // 如果超过最大尝试次数,弹出提示信息
alert(免费用户只能查询${maxAttempts}次,请明天再来,或者找管理员赞助吧);
return;
}
const linkInput = document.getElementById("input-link").value.trim(); // 获取用户输入的链接 if (!isValidLink(linkInput)) { // 如果链接无效,弹出提示信息 alert("输入的链接格式不正确,请重新输入。"); return; }
showLoading(); // 显示loading动画
checkLinkSecurity(linkInput) // 进行链接安全检查
.then(() => {
const newLink = https://dy.wygzs.top/dy/api.php?url=${encodeURIComponent(linkInput)}; // 对链接进行编码,生成新链接
window.location.href = newLink; // 跳转到新链接
setAttempts(attempts + 1, expires); // 设置尝试次数cookie
})
.catch((error) => {
alert(error.message); // 如果安全检查出错,则弹出错误消息
});
}
原文地址: https://www.cveoy.top/t/topic/ZXj 著作权归作者所有。请勿转载和采集!