【问题标题】:Error stopping function after alert message is shown显示警报消息后错误停止功能
【发布时间】:2021-07-11 12:09:14
【问题描述】:

我正在开发代理服务,一切正常。当您按下提交按钮时,它具有 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>

抱歉,是的,我知道代码很长,但我想确保新代码不会影响任何遗漏的代码。 (一切都在循环中)感谢您的观看。

【问题讨论】:

    标签: javascript html jquery


    【解决方案1】:

    在类似的情况下,我通常在while loop 中使用try...catch 作为跟踪它的变量:

    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 并且循环在封装错误之前被破坏 在这段代码中,我在异步函数中进行了 fetch,以便能够使用 await 并从适用于 fetch 成功的 then 方法调用 redirect

    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);
    
            }
        }
    }
    

    }

    【讨论】:

    • 它可以工作,但它仍然没有摆脱新标签的打开。我想要它,以便在显示警告框后不会打开新标签。
    • 好的,问题是fetch是异步函数,所以代码不等待它的响应我已经更新了答案试试看(:
    • 您好,感谢您花费这么多时间。到目前为止,它似乎正在工作,但是每当您尝试关闭警报框时,它就会再次打开 - 它永远不会停止。如果您有广告拦截器,请尝试查看此错误codepen.io/Ri12345/pen/KKWoqmy
    • 直接删除while loop(:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    • 1970-01-01
    • 2020-10-08
    • 2023-03-06
    • 1970-01-01
    相关资源
    最近更新 更多