Skip to content

Commit

Permalink
detect adblock installed or not
Browse files Browse the repository at this point in the history
  • Loading branch information
jesselau76 committed Dec 1, 2018
1 parent 3d6a6f8 commit c763107
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README-zh.md
Expand Up @@ -6,6 +6,7 @@




# Hugo W3 SIMPLE


Expand Down Expand Up @@ -33,6 +34,7 @@
- 辉光式LOGO,可选
- Cookie设置提醒,根据欧盟法律需显示提示站点采用cookie。
- Google Adsense autoads
- 反反广告插件支持,滑屏2000px若检测到用户装有adblock,则弹出提醒窗口。
- Lazy Load图片 - 异步加载网站上的图像 - 也就是说先用一个极小的图像文件占住位置,只有当用户滑动屏幕到该图片出现的位置时,才调用真实的图片。 如果页面有很多图片,启用此功能可以使页面加载速度非常快。 [实例演示](https://jesselau.com/21-wordpress-plugins-activated-in-my-website/). 这个网页含有21个图片。不用lazy load前文件大小为854kb,加载时间3.5s.![Before lazy load](https://raw.githubusercontent.com/jesselau76/hugo-w3-simple/master/images/beforelazyload.png)使用lazy load后文件大小为524kb,加载时间就只有2.8s
![After lazy load](https://raw.githubusercontent.com/jesselau76/hugo-w3-simple/master/images/afterlazyload.png)

Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -34,6 +34,7 @@
- Glowing style logo
- Scroll indicator
- Google Adsense autoads
- Detect adblock and display allow ads notice on scrolling over 2000px if the users have adblock installed.
- Cookie law support.
- Lazy Load images - loading images on websites asynchronously — that is, after the above-the-fold content is fully loaded, or even conditionally, only when they appear in the browser's viewport. This means that if users don't scroll all the way down, images placed at the bottom of the page won't even be loaded. Enable this function to make the page load very fast if it has lots of images. [Live Demo Here](https://jesselau.com/21-wordpress-plugins-activated-in-my-website/). This page includes 21 images. Before lazy load the page size is 854kb and load time is 3.5s.![Before lazy load](https://raw.githubusercontent.com/jesselau76/hugo-w3-simple/master/images/beforelazyload.png)After lazy load the page size is 524kb and load time is 2.8s
![After lazy load](https://raw.githubusercontent.com/jesselau76/hugo-w3-simple/master/images/afterlazyload.png)
Expand Down
1 change: 1 addition & 0 deletions exampleSite/config.toml
Expand Up @@ -98,6 +98,7 @@ contentDir = "content/chinese"
description = "Hugo W3 Simple Theme"
onesignalid = "" #one signal push app id
# adsenseid="ca-pub-123456789"
allowads = true
logotext = "HUGO W3 SIMPLE" # logo text
footer = "© [John Doe](https://johndoe.com) 2018 | [Github](https://github.com/johndoe) | [Twitter](https://twitter.com/johndoe) | [RSS](/index.xml)"

Expand Down
8 changes: 8 additions & 0 deletions layouts/_default/baseof.html
Expand Up @@ -224,6 +224,14 @@


{{ end }}

<!-- allowads -->
{{ if and (.Site.Params.allowads) (.IsPage) }}
if (document.body.scrollTop > 2000 || document.documentElement.scrollTop > 2000) {
detectads();
}

{{ end }}
}


Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/header.html
Expand Up @@ -14,7 +14,7 @@
);
}
</script>
<script src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
<script async src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
{{ end }}

{{ if .Site.Params.searchfunction }}
Expand Down
42 changes: 42 additions & 0 deletions layouts/post/single.html
Expand Up @@ -155,7 +155,49 @@ <h2>{{ T "relatedposts" }}</h2>
</script>
{{- end -}}

<!-- allowads -->
{{ if (.Site.Params.allowads) }}

<div id="wrapads">
<div class="adBanner">

</div>
</div>

<div id="allowads" class="allowads-overlay">

<div class="allowads-overlay-content">
<div class="w3-panel w3-pink w3-large">


<p> We notice you're using an adblocker. If you like our webite please keep us running by whitelisting this site in your ad blocker. We’re serving quality, related ads only. Thank you!</p>

<div class="w3-btn w3-green" onclick="closeOverlay()">I've whitelisted your website.</div><br>
<div class="w3-button w3-small" onclick="closeOverlay()">Not now</div>
</div>
</div>
</div>

<script>
function closeOverlay() {
document.getElementById("allowads").style.width = "0%";
document.getElementById("wrapads").style.height="1px";
}

function detectads() {
var h = document.getElementById("wrapads").clientHeight;

if (h==0){ document.getElementById("allowads").style.width = "100%";
}
}




</script>

{{- end -}}




Expand Down
31 changes: 31 additions & 0 deletions static/css/style.css
Expand Up @@ -367,3 +367,34 @@ position: fixed;

}

/* allowads */

.adBanner {

height: 1px;
width: 1px;
}

.allowads-overlay {
height: 100%;
width: 0;
position: fixed;
z-index: 2;
top: 0;
left: 0;
background-color: rgb(255,255,255);
background-color: rgba(255,255,255, 0.9);
overflow-x: hidden;
transition: 0.5s;
}

.allowads-overlay-content {
position: relative;
top: 25%;
width: 61.8%;
text-align: center;
margin-top: 30px;
margin-left:auto;
margin-right:auto;

}

0 comments on commit c763107

Please sign in to comment.