使用Cookie设置访问计数

时间:2019-03-01 12:42:20

标签: javascript cookies

我的网站使用了阻止用户进入的反广告,但没人喜欢看广告,因此,我决定寻找一种方法,让新访客在禁用该消息之前先使用该网站几次adblock出现。

我尝试使用cookie记录一个数字,每次访问该人时,都会减去该数字,删除旧的cookie,然后创建一个新的数字,减去该数字,因此当数字等于0时,会出现反adblock ,但令我感到困惑的是,我才刚开始学习javascript。

有没有更简单的方法可以做到这一点,或者cookie是最佳选择?我该怎么做?

我的反广告拦截的示例页面:https://www.stackexample.ml/adblock

我的反禁止代码:

  

注意::我的网站是静态的。

window.onload = function a() {
  var div1 = document.getElementById("off");
  var div2 = document.getElementById("on");
  var div3 = document.getElementById("carregando");
  var span = document.getElementById("v");
  var b = document.getElementById("b");
  var url = new URL(window.location);
  var r = url.searchParams.get("r");
  var enc1 = window.atob(r);

if( window.canRunAds === undefined ){
  div2.style.display="block"
  div3.style.display="none"
  b.style.display="block"
  span.style.color="#d06079"
  span.innerHTML="Desative o Adblock"
}else{
  div1.style.display="block"
  div3.style.display="none"
  span.innerHTML="Adblock desativado"
  //código para liberar a pagina aqui
}  
};
.cont {
  width:320px;
  margin:18% auto;
}
#b {
  border:1px solid #ba5269;
  background-color:#d06079;
  color:white;
  border-radius:6px;
  padding:15px;
  font-size:18px;
  display:none;
  margin:25px auto;
}
#on{
  display:none;
  margin:22px auto;
}
.check_mark {
  width: 80px;
  height: 130px;
  margin: 0 auto;
  display:none;
}
#v {
  font-family:arial;
  font-size:35px;
  color:green;
  display:block;
  text-align:center;
}

button {
  cursor: pointer;
  margin-left: 15px;
}

.hide{
  display:none;
}

.sa-icon {
  width: 80px;
  height: 80px;
  border: 4px solid gray;
  -webkit-border-radius: 40px;
  border-radius: 40px;
  border-radius: 50%;
  margin: 20px auto;
  padding: 0;
  position: relative;
  box-sizing: content-box;
}

.sa-icon.sa-success {
  border-color: #4CAF50;
}

.sa-icon.sa-success::before, .sa-icon.sa-success::after {
  content: '';
  -webkit-border-radius: 40px;
  border-radius: 40px;
  border-radius: 50%;
  position: absolute;
  width: 60px;
  height: 120px;
  background: white;
  -webkit-transform: rotate(45deg);
  transform: rotate(45deg);
}

.sa-icon.sa-success::before {
  -webkit-border-radius: 120px 0 0 120px;
  border-radius: 120px 0 0 120px;
  top: -7px;
  left: -33px;
  -webkit-transform: rotate(-45deg);
  transform: rotate(-45deg);
  -webkit-transform-origin: 60px 60px;
  transform-origin: 60px 60px;
}

.sa-icon.sa-success::after {
  -webkit-border-radius: 0 120px 120px 0;
  border-radius: 0 120px 120px 0;
  top: -11px;
  left: 30px;
  -webkit-transform: rotate(-45deg);
  transform: rotate(-45deg);
  -webkit-transform-origin: 0px 60px;
  transform-origin: 0px 60px;
}

.sa-icon.sa-success .sa-placeholder {
  width: 80px;
  height: 80px;
  border: 4px solid rgba(76, 175, 80, .5);
  -webkit-border-radius: 40px;
  border-radius: 40px;
  border-radius: 50%;
  box-sizing: content-box;
  position: absolute;
  left: -4px;
  top: -4px;
  z-index: 2;
}

.sa-icon.sa-success .sa-fix {
  width: 5px;
  height: 90px;
  background-color: white;
  position: absolute;
  left: 28px;
  top: 8px;
  z-index: 1;
  -webkit-transform: rotate(-45deg);
  transform: rotate(-45deg);
}

.sa-icon.sa-success.animate::after {
  -webkit-animation: rotatePlaceholder 4.25s ease-in;
  animation: rotatePlaceholder 4.25s ease-in;
}

.sa-icon.sa-success {
  border-color: transparent\9;
}
.sa-icon.sa-success .sa-line.sa-tip {
  -ms-transform: rotate(45deg) \9;
}
.sa-icon.sa-success .sa-line.sa-long {
  -ms-transform: rotate(-45deg) \9;
}

.animateSuccessTip {
  -webkit-animation: animateSuccessTip 0.75s;
  animation: animateSuccessTip 0.75s;
}

.animateSuccessLong {
  -webkit-animation: animateSuccessLong 0.75s;
  animation: animateSuccessLong 0.75s;
}

@-webkit-keyframes animateSuccessLong {
  0% {
    width: 0;
    right: 46px;
    top: 54px;
  }
  65% {
    width: 0;
    right: 46px;
    top: 54px;
  }
  84% {
    width: 55px;
    right: 0px;
    top: 35px;
  }
  100% {
    width: 47px;
    right: 8px;
    top: 38px;
  }
}
@-webkit-keyframes animateSuccessTip {
  0% {
    width: 0;
    left: 1px;
    top: 19px;
  }
  54% {
    width: 0;
    left: 1px;
    top: 19px;
  }
  70% {
    width: 50px;
    left: -8px;
    top: 37px;
  }
  84% {
    width: 17px;
    left: 21px;
    top: 48px;
  }
  100% {
    width: 25px;
    left: 14px;
    top: 45px;
  }
}
@keyframes animateSuccessTip {
  0% {
    width: 0;
    left: 1px;
    top: 19px;
  }
  54% {
    width: 0;
    left: 1px;
    top: 19px;
  }
  70% {
    width: 50px;
    left: -8px;
    top: 37px;
  }
  84% {
    width: 17px;
    left: 21px;
    top: 48px;
  }
  100% {
    width: 25px;
    left: 14px;
    top: 45px;
  }
}

@keyframes animateSuccessLong {
  0% {
    width: 0;
    right: 46px;
    top: 54px;
  }
  65% {
    width: 0;
    right: 46px;
    top: 54px;
  }
  84% {
    width: 55px;
    right: 0px;
    top: 35px;
  }
  100% {
    width: 47px;
    right: 8px;
    top: 38px;
  }
}

.sa-icon.sa-success .sa-line {
  height: 5px;
  background-color: #4CAF50;
  display: block;
  border-radius: 2px;
  position: absolute;
  z-index: 2;
}

.sa-icon.sa-success .sa-line.sa-tip {
  width: 25px;
  left: 14px;
  top: 46px;
  -webkit-transform: rotate(45deg);
  transform: rotate(45deg);
}

.sa-icon.sa-success .sa-line.sa-long {
  width: 47px;
  right: 8px;
  top: 38px;
  -webkit-transform: rotate(-45deg);
  transform: rotate(-45deg);
}

@-webkit-keyframes rotatePlaceholder {
  0% {
    transform: rotate(-45deg);
    -webkit-transform: rotate(-45deg);
  }
  5% {
    transform: rotate(-45deg);
    -webkit-transform: rotate(-45deg);
  }
  12% {
    transform: rotate(-405deg);
    -webkit-transform: rotate(-405deg);
  }
  100% {
    transform: rotate(-405deg);
    -webkit-transform: rotate(-405deg);
  }
}
@keyframes rotatePlaceholder {
  0% {
    transform: rotate(-45deg);
    -webkit-transform: rotate(-45deg);
  }
  5% {
    transform: rotate(-45deg);
    -webkit-transform: rotate(-45deg);
  }
  12% {
    transform: rotate(-405deg);
    -webkit-transform: rotate(-405deg);
  }
  100% {
    transform: rotate(-405deg);
    -webkit-transform: rotate(-405deg);
  }
}

.loading {
    border: 4px solid #3a3;
    border-right: 4px solid #a5d7a7;
    border-bottom: 4px solid #a5d7a7;
    height: 80px;
    width: 80px;
    border-radius: 50%;
    -webkit-animation: loading 1s infinite linear;
    -moz-animation: loading 1s infinite linear;
    -o-animation: loading 1s infinite linear;
    animation: loading 1s infinite linear;
    margin:22px auto;
}
@-webkit-keyframes loading {
    from {
        -webkit-transform: rotate(0deg);
    }
    to {
        -webkit-transform: rotate(360deg);
    }
}
@-moz-keyframes loading {
    from {
        -moz-transform: rotate(0deg);
    }
    to {
        -moz-transform: rotate(360deg);
    }
}
@-o-keyframes loading {
    from {
        -o-transform: rotate(0deg);
    }
    to {
        -o-transform: rotate(360deg);
    }
}
@keyframes loading {
    from {
        transform: rotate(0deg);
    }
    to {
        transform: rotate(360deg);
    }
}
.abgne-loading-20140104-2 {
    position: relative;
    height: 100px;
    width: 100px;
}
.abgne-loading-20140104-2 .loading {
    border: 6px solid #168;
    border-right: 6px solid #fff;
    border-bottom: 6px solid #fff;
    height: 100%;
    width: 100%;
    border-radius: 50%;
    -webkit-animation: loading 1s infinite linear;
    -moz-animation: loading 1s infinite linear;
    -ms-animation: loading 1s infinite linear;
    -o-animation: loading 1s infinite linear;
    animation: loading 1s infinite linear;
}
.abgne-loading-20140104-2 .word {
    color: #168;
    position: absolute;
    top: 0;
    left: 0;
    display: inline-block;
    text-align: center;
    font-size: 72px;
    line-height: 72px;
    font-family: arial;
    margin: 18px 0 0 20px;
    padding: 0;
}
<!DOCTYPE html>
<html>
<head>
<title>Verificando Adblock</title>
<script src="https://www.stackexample.ml/js/ads.js"></script>
</head>
<body>
<div class="cont">
<span id="v">Verificando adblock</span>
<div id="carregando" class="loading"></div>
<img src="https://www.stackexample.ml/falhou.png" alt="Smiley face" height="90" width="90" id="on">
<div id="off" class="check_mark">
  <div class="sa-icon sa-success animate">
    <span class="sa-line sa-tip animateSuccessTip"></span>
    <span class="sa-line sa-long animateSuccessLong"></span>
    <div class="sa-placeholder"></div>
    <div class="sa-fix"></div>
  </div>
</div>
<button id="b" onclick="window.location.reload()">Já desativei, Continuar...</button>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

如果您希望维护计数器,则可以将Local Storage用作同一计数器。例如,

  

注意:由于StackOverflow的限制,您将无法在此处运行此代码,就像使用localStorage

您可以在这里查看其运行情况(将页面刷新3次,您会看到)-https://codepen.io/anon/pen/qvaYQK

在这里,如果没有找到adcounter,则将其计数器初始化为1;如果找到,我将在广告被阻止的情况下将其递增;如果超过3,则向用户显示模式。如果用户禁用了adblock,我会将计数器重置为0。

我还对您的代码进行了一些重构,其中仍有大量代码仍可以重构。

window.onload = function a() {
  var div1 = document.getElementById("off");
  var div2 = document.getElementById("on");
  var div3 = document.getElementById("carregando");
  var span = document.getElementById("v");
  var b = document.getElementById("b");
  var url = new URL(window.location);
  var r = url.searchParams.get("r");
  var enc1 = window.atob(r);
  var getWrapper = document.getElementById('ad-blocker-modal');
  var showModalAfterVisits = 3; //show modal after 3 visits
  var getCounter = localStorage.getItem('adcounter');
  
  if(!getCounter) {
    localStorage.setItem('adcounter', 0); // initialize Local Storage
  }

  function checkForAdBlocker() {
    if (!window.canRunAds) {
      if(getCounter < 3) {
        //keep incrementing the counter unless the counter reaches 3 and return
        localStorage.setItem('adcounter', ++getCounter);
        return;
      }

      getWrapper.classList.remove('hide');
      div2.style.display = "block"
      div3.style.display = "none"
      b.style.display = "block"
      span.style.color = "#d06079"
      span.innerHTML = "Desative o Adblock"
    } else {
      localStorage.setItem('adcounter', 0); //reset if adblock is disabled
      div1.style.display = "block"
      div3.style.display = "none"
      span.innerHTML = "Adblock desativado"
    }
  }

  checkForAdBlocker();
};
.cont {
  width: 320px;
  margin: 18% auto;
}

.hide {
  display: none;
}

#ad-blocker-modal {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #fff;
}

#b {
  border: 1px solid #ba5269;
  background-color: #d06079;
  color: white;
  border-radius: 6px;
  padding: 15px;
  font-size: 18px;
  display: none;
  margin: 25px auto;
}

#on {
  display: none;
  margin: 22px auto;
}

.check_mark {
  width: 80px;
  height: 130px;
  margin: 0 auto;
  display: none;
}

#v {
  font-family: arial;
  font-size: 35px;
  color: green;
  display: block;
  text-align: center;
}

button {
  cursor: pointer;
  margin-left: 15px;
}

.hide {
  display: none;
}

.sa-icon {
  width: 80px;
  height: 80px;
  border: 4px solid gray;
  border-radius: 40px;
  border-radius: 50%;
  margin: 20px auto;
  padding: 0;
  position: relative;
  box-sizing: content-box;
}

.sa-icon.sa-success {
  border-color: #4CAF50;
}

.sa-icon.sa-success::before,
.sa-icon.sa-success::after {
  content: '';
  border-radius: 40px;
  border-radius: 50%;
  position: absolute;
  width: 60px;
  height: 120px;
  background: white;
  transform: rotate(45deg);
}

.sa-icon.sa-success::before {
  border-radius: 120px 0 0 120px;
  top: -7px;
  left: -33px;
  transform: rotate(-45deg);
  transform-origin: 60px 60px;
}

.sa-icon.sa-success::after {
  border-radius: 0 120px 120px 0;
  top: -11px;
  left: 30px;
  transform: rotate(-45deg);
  transform-origin: 0px 60px;
}

.sa-icon.sa-success .sa-placeholder {
  width: 80px;
  height: 80px;
  border: 4px solid rgba(76, 175, 80, .5);
  border-radius: 40px;
  border-radius: 50%;
  box-sizing: content-box;
  position: absolute;
  left: -4px;
  top: -4px;
  z-index: 2;
}

.sa-icon.sa-success .sa-fix {
  width: 5px;
  height: 90px;
  background-color: white;
  position: absolute;
  left: 28px;
  top: 8px;
  z-index: 1;
  transform: rotate(-45deg);
}

.sa-icon.sa-success.animate::after {
  animation: rotatePlaceholder 4.25s ease-in;
}

.animateSuccessTip {
  animation: animateSuccessTip 0.75s;
}

.animateSuccessLong {
  animation: animateSuccessLong 0.75s;
}

@keyframes animateSuccessTip {
  0% {
    width: 0;
    left: 1px;
    top: 19px;
  }
  54% {
    width: 0;
    left: 1px;
    top: 19px;
  }
  70% {
    width: 50px;
    left: -8px;
    top: 37px;
  }
  84% {
    width: 17px;
    left: 21px;
    top: 48px;
  }
  100% {
    width: 25px;
    left: 14px;
    top: 45px;
  }
}

@keyframes animateSuccessLong {
  0% {
    width: 0;
    right: 46px;
    top: 54px;
  }
  65% {
    width: 0;
    right: 46px;
    top: 54px;
  }
  84% {
    width: 55px;
    right: 0px;
    top: 35px;
  }
  100% {
    width: 47px;
    right: 8px;
    top: 38px;
  }
}

.sa-icon.sa-success .sa-line {
  height: 5px;
  background-color: #4CAF50;
  display: block;
  border-radius: 2px;
  position: absolute;
  z-index: 2;
}

.sa-icon.sa-success .sa-line.sa-tip {
  width: 25px;
  left: 14px;
  top: 46px;
  transform: rotate(45deg);
}

.sa-icon.sa-success .sa-line.sa-long {
  width: 47px;
  right: 8px;
  top: 38px;
  transform: rotate(-45deg);
}

@keyframes rotatePlaceholder {
  0% {
    transform: rotate(-45deg);
  }
  5% {
    transform: rotate(-45deg);
  }
  12% {
    transform: rotate(-405deg);
  }
  100% {
    transform: rotate(-405deg);
  }
}

.loading {
  border: 4px solid #3a3;
  border-right-color: a5d7a7;
  border-bottom-color: #a5d7a7;
  height: 80px;
  width: 80px;
  border-radius: 50%;
  animation: loading 1s infinite linear;
  margin: 22px auto;
}

@keyframes loading {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.abgne-loading-20140104-2 {
  position: relative;
  height: 100px;
  width: 100px;
}

.abgne-loading-20140104-2 .loading {
  border: 6px solid #168;
  border-right-color: #fff;
  border-bottom: #fff;
  height: 100%;
  width: 100%;
  border-radius: 50%;
  animation: loading 1s infinite linear;
}

.abgne-loading-20140104-2 .word {
  color: #168;
  position: absolute;
  top: 0;
  left: 0;
  display: inline-block;
  text-align: center;
  font-size: 72px;
  line-height: 72px;
  font-family: arial;
  margin: 18px 0 0 20px;
  padding: 0;
}
<!DOCTYPE html>
<html>

<head>
  <title>Verificando Adblock</title>
  <script src="https://www.stackexample.ml/js/ads.js"></script>
</head>

<body>
  Some content on my Website
  <div id="ad-blocker-modal" class="hide">
    <div class="cont">
      <span id="v">Verificando adblock</span>
      <div id="carregando" class="loading"></div>
      <img src="https://www.stackexample.ml/falhou.png" alt="Smiley face" height="90" width="90" id="on">
      <div id="off" class="check_mark">
        <div class="sa-icon sa-success animate">
          <span class="sa-line sa-tip animateSuccessTip"></span>
          <span class="sa-line sa-long animateSuccessLong"></span>
          <div class="sa-placeholder"></div>
          <div class="sa-fix"></div>
        </div>
      </div>
      <button id="b" onclick="window.location.reload()">Já desativei, Continuar...</button>
    </div>
  </div>
</body>
</html>