Skip to content

Commit

Permalink
Merge pull request #14 from GreenMeteor/develop
Browse files Browse the repository at this point in the history
Enh: AdBlocker Detection
  • Loading branch information
ArchBlood committed May 7, 2024
2 parents cd9527f + a986c00 commit 3a3de3b
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 23 deletions.
12 changes: 0 additions & 12 deletions Assets.php

This file was deleted.

5 changes: 5 additions & 0 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

class Module extends BaseModule
{
/**
* @inheritdoc
*/
public $resourcesPath = 'resources';

/**
* @inheritdoc
*/
Expand Down
12 changes: 12 additions & 0 deletions assets/Assets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace humhub\modules\adsense\assets;

use Yii;
use yii\web\AssetBundle;

class Assets extends AssetBundle
{
public $sourcePath = '@adsense/resources';

}
9 changes: 2 additions & 7 deletions module.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"id": "adsense",
"version": "1.0.6",
"version": "1.2.0-beta.1",
"name": "AdSense",
"description": "Adds Google AdSense Functionality.",
"humhub": {
"minVersion": "1.9.0"
"minVersion": "1.16.0-beta.1"
},
"keywords": ["ads", "adsense"],
"homepage": "https://github.com/GreenMeteor",
Expand All @@ -15,10 +15,5 @@
"homepage": "https://github.com/GreenMeteor",
"role": "Developer"
},
{
"name": "humhub-contrib",
"homepage": "https://github.com/humhub-contrib",
"role": "Maintainer"
}
]
}
10 changes: 10 additions & 0 deletions requirements.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

$requirements = [];

// Check if PHP version is at least 8.1
if (PHP_VERSION_ID < 80100) {
$requirements[] = 'Please upgrade to PHP Version 8.1 or later!';
}

return $requirements;
File renamed without changes
77 changes: 73 additions & 4 deletions widgets/views/adframe.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,34 @@

$module = Yii::$app->getModule('adsense');
$urlJs = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js';
$this->registerJsFile($urlJs, ['data-ad-client' => Yii::$app->getModule('adsense')->getClient(), 'src' => 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=' . Yii::$app->getModule('adsense')->getClient() . 'crossorigin="anonymous"', 'position' => View::POS_END]);

try {
// Try to register the JS file
$this->registerJsFile($urlJs, ['data-ad-client' => Yii::$app->getModule('adsense')->getClient(), 'src' => 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=' . Yii::$app->getModule('adsense')->getClient() . 'crossorigin="anonymous"', 'position' => View::POS_HEAD]);
} catch (Exception $e) {
// Handle the error here, you can log it or perform any other action
Yii::error('Error loading Google AdSense script: ' . $e->getMessage());
}

$this->registerMetaTag([
'name' => 'google-adsense-account',
'content' => Yii::$app->getModule('adsense')->getClient(),
]);
?>
<div class="panel panel-adsense panel-default" id="panel-adsense">
<style>
.ins.adsbygoogle[data-ad-status="unfilled"] {
display: none !important;
}
.adblocker-warning {
padding: 10px;
background-color: #f2dede;
color: #a94442;
border: 1px solid #ebccd1;
border-radius: 4px;
}
</style>

<div class="panel panel-adsense panel-default" id="panel-adsense">
<?= PanelMenu::widget(['id' => 'panel-adsense']); ?>
<div class="panel-heading">
<?= Yii::t('AdsenseModule.base', '<strong>Community</strong> Ad'); ?>
Expand All @@ -25,13 +49,58 @@
<?php if (empty($client && $slot)): ?>
<p><?= $module->getMessage() ?></p>
<?php else: ?>
<script async src="<?= $urlJs; ?>"></script>
<ins class="adsbygoogle"
style="display:block;"
data-ad-client="<?= $client; ?>"
data-ad-slot="<?= $slot; ?>"
data-ad-format="auto"></ins>
<script <?= Html::nonce() ?>>(adsbygoogle = window.adsbygoogle || []).push({});</script>
data-ad-format="auto"
data-full-width-responsive="true"></ins>
<div id="adblockerModal" class="modal fade" role="dialog" aria-labelledby="adblockerModalLabel" aria-hidden="true" style="display: none; background: rgba(0, 0, 0, 0.5);" data-backdrop="static">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<div class="modal-title" id="adblockerModalLabel"><b>AdBlocker Detected</b></div>
</div>
<div class="modal-body">
<div class="adblocker-warning">Please disable AdBlocker to view content.</div>
</div>
</div>
</div>
</div>
<?php endif ?>
<script <?= Html::nonce() ?>>
// Variable to track if AdSense script has been loaded
var adsenseScriptLoaded = false;
// Variable to track if adblocker check has been done
var adblockerCheckDone = false;

// Function to check if AdSense script is loaded and AdBlocker is enabled
function checkAdBlocker() {
if (!adblockerCheckDone && !adsenseScriptLoaded) {
console.log('AdBlocker enabled');
$('#adblockerModal').modal('show'); // Show modal if AdSense script is not loaded
adblockerCheckDone = true;
} else if (!adblockerCheckDone && adsenseScriptLoaded) {
console.log('AdBlocker not enabled');
$('#adblockerModal').modal('hide'); // Hide modal if AdSense script is loaded
adblockerCheckDone = true;
// Push AdSense ads if AdBlocker is not enabled
(adsbygoogle = window.adsbygoogle || []).push({});
}
}

// Check if AdSense script is loaded
window.addEventListener('load', function() {
if (typeof adsbygoogle === 'undefined') {
adsenseScriptLoaded = false;
} else {
adsenseScriptLoaded = true;
}
checkAdBlocker(); // Check initially
});

</script>
<?= Html::endTag('div') ?>
</div>
</div>

0 comments on commit 3a3de3b

Please sign in to comment.