javascript - 显示警报消息后错误停止功能
问题描述
我正在开发代理服务,一切都很好。当您按下提交按钮时,它具有 onclick 功能。我现在也让它检测何时启用 adblock,如果检测到 adblock,我不希望该功能通过,(这意味着我想要它,所以如果你启用了 adblock,代理实际上不会启动,我想要仅当您按下按钮直到禁用广告拦截时才会弹出警报消息。)
如果您有广告拦截,这是我正在寻找的示例。( http://fastp.org/ ) 在这个网站上,如果你启用了adblock,你不能提交表格。在警报框上按“确定”后,我的仍然通过。在我的 javascript 代码中,我尝试执行“return false”;还有一个“其他”,但似乎没有任何效果。如果您启用了广告拦截,我不希望提交表单。
我想要它,所以如果启用了 adblock,它会显示警告框,当您按“确定”时,我不希望它仍然在新选项卡中启动代理。我希望它在禁用 adblock 时启动。
这是我的代码:
$(document).ready(function() {
$('#browsebtn').click(function() {
val = $('#urlinput').val();
$('#urlinput').val(val.replace($('#remove1').val(), ""));
});
});
$(document).ready(function() {
$('#browsebtn').click(function() {
val = $('#urlinput').val();
$('#urlinput').val(val.replace($('#remove2').val(), ""));
});
});
$(document).ready(function() {
$('#browsebtn').click(function() {
val = $('#urlinput').val();
$('#urlinput').val(val.replace($('#remove3').val(), ""));
});
});
function forceLower(strInput) {
strInput.value = strInput.value.toLowerCase();
}
function checkAdBlocker() {
try {
fetch(
new Request("https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js", {
method: 'HEAD',
mode: 'no-cors'
})).catch(error => {
showNotification();
});
} catch (e) {
// Request failed, likely due to ad blocker
showNotification();
}
var x;
x = document.getElementById("urlinput").value;
if (x == "" || x == "https://" || x == "http://" || x == "www." || x == "http://www." || x == "https://www.") {
$("#error").show().delay(3000).fadeOut();
return false;
} else {
var ddl = document.getElementById("servertype");
var selectedValue = ddl.options[ddl.selectedIndex].value;
if (selectedValue == "server1") {
setTimeout(function() {
window.open('http://server1.com/' + document.getElementById('urlinput').value);
}, 200);
}
if (selectedValue == "server2") {
setTimeout(function() {
window.open('http://server2.com/' + document.getElementById('urlinput').value);
}, 200);
}
if (selectedValue == "server3") {
setTimeout(function() {
window.open('http://server3.com/' + document.getElementById('urlinput').value);
}, 200);
}
if (selectedValue == "server4") {
setTimeout(function() {
window.open('http://server4.com/' + document.getElementById('urlinput').value);
}, 200);
}
}
}
function showNotification() {
alert("Please disable adBlocker");
}
<form action="javascript:void(0);" method="POST">
<input id="remove1" type="hidden" value="https://" /><input id="remove2" type="hidden" value="http://" /><input id="remove3" type="hidden" value="www." />
<input type="text" name="name" placeholder="Enter web address.." id="urlinput" onkeyup="return forceLower(this);" /><button type="submit" id="browsebtn" onclick="return checkAdBlocker()" name="submit" value="Browse">BROWSE</button>
<div class="serverselect"><label>Select Server:</label></div>
<div class="selectserver">
<select id="servertype" name="dropdown">
<option value="server1" data-sort="1"> US Server</option>
<option value="server2" data-sort="2"> CA Server</option>
<option value="server3" data-sort="3"> DE Server</option>
<option value="server4" data-sort="4"> GB Server</option>
</select>
</div>
<p id="error">Please enter a valid URL address.</p>
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
抱歉,是的,我知道代码很长,但我想确保新代码不会影响任何遗漏的代码。(一切都是循环在一起的)谢谢你看一看。
解决方案
在类似的情况下,我通常使用try...catch
inwhile loop
作为跟踪它的变量:
while (true) {
var adBlocker = false
try {
fetch(
new Request("https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js", {
method: 'HEAD',
mode: 'no-cors'
})).catch(error => {
showNotification();
adBlocker = true
});
} catch (e) {
// Request failed, likely due to ad blocker
showNotification();
adBlocker = true
}
if (adBlocker == false) {
break
}
}
更新:
fetch
是异步函数,所以adBlocker
变量是false
,循环在cathing错误之前被打破在这段代码中,我在异步函数中进行了fetch,以便能够使用await
和调用成功redirect
的then
方法fetch
:
function checkAdBlocker() {
async function AdBlock() {
try {
// Wait for fetch
let adTest = await fetch("https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js")
.then(response => {
redirect()
adBlocker = false;
})
.catch(error => {
showNotification();
});
} catch (e) {
// Request failed, likely due to ad blocker
showNotification();
}
}
AdBlock()
function redirect() {
var x;
x = document.getElementById("urlinput").value;
if (x == "" || x == "https://" || x == "http://" || x == "www." || x == "http://www." || x == "https://www.") {
$("#error").show().delay(3000).fadeOut();
return false;
} else {
var ddl = document.getElementById("servertype");
var selectedValue = ddl.options[ddl.selectedIndex].value;
if (selectedValue == "server1") {
setTimeout(function () {
window.open('https://devoooo-proxy.herokuapp.com/proxy/' + document.getElementById('urlinput').value);
}, 200);
}
if (selectedValue == "server2") {
setTimeout(function () {
window.open('https://google-github.herokuapp.com/http/' + document.getElementById('urlinput').value);
}, 200);
}
if (selectedValue == "server3") {
setTimeout(function () {
window.open('https://proxy.bibliotecavirtualalergia.com/browse.php?u=http://' + document.getElementById('urlinput').value);
}, 200);
}
if (selectedValue == "server4") {
setTimeout(function () {
window.open('http://server3main.epizy.com/browse.php?u=http://' + document.getElementById('urlinput').value);
}, 200);
}
}
}
}
推荐阅读
- sql - 如何使用 stu_class 和 parent_id 区分选择
- javascript - 从 axios 响应 reactjs 填充输入字段
- html - 我的文字背景变得透明
- ios - 在哪里可以将 ImageView 添加到 Main.storyboard 的视图中?
- docker - 用于密钥斗篷的 Nginx 反向代理
- node.js - Elasticsearch 查询以返回那些也缺少索引值的文档
- css - ASP.NET Gridview 中的文本框未对齐
- javascript - Javascript - 将用户输入从前端发送到后端服务器
- verilog - 如何在systemVerilog中返回一个数组
- python - self.rect.midbottom 在这里是什么意思?screen.blit() 在这里做什么?