Skip to content

Commit

Permalink
Merge pull request #18 from guiassemany/feature/debug-mode
Browse files Browse the repository at this point in the history
Add Debug mode feature
  • Loading branch information
guiassemany authored Oct 18, 2024
2 parents 48a5344 + 487cca8 commit 66e9589
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
3 changes: 2 additions & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
<script>
document.addEventListener('DOMContentLoaded', function() {
const stickToMeInstance = stickToMe({
layer: '#stickLayer'
layer: '#stickLayer',
debug: false,
// cookie: true,
// maxamount: 1,
// cookieExpiration: 2 * 60
Expand Down
16 changes: 16 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ Stick to me allows a few customisations. See options below.
| bgclickclose | Clicking on background closes the popup | true |
| escclose | pressing ESC closes the popup | true |
| onleave | function to be called when popup closes | empty fn |
| debug | Enable Debug mode to display console messages | false |

## Debug Mode

The Stick to Me plugin includes a Debug mode that provides helpful console messages to understand the plugin's behavior. When Debug mode is enabled, you will see messages related to tracking movement, detecting intent, indicating direction, and firing the popup.

To enable Debug mode, set the `debug` option to `true` when initializing the plugin:

```js
const stickToMeInstance = stickToMe({
layer: '#stickLayer',
debug: true
});
```

When Debug mode is ON, console messages will be displayed to help you understand what is happening. When Debug mode is OFF, no console messages will be shown.

## Support

Expand Down
21 changes: 20 additions & 1 deletion src/stick-to-me.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const stickToMe = function (configs) {
bgclickclose: true,
escclose: true,
onleave: function (e) {},
disableleftscroll: true // chrome disable
disableleftscroll: true, // chrome disable
debug: false
};

const settings = Object.assign({}, defaults, configs);
Expand Down Expand Up @@ -53,12 +54,20 @@ const stickToMe = function (configs) {

Object.assign(settings, reqsettings);

function debugLog(message) {
if (settings.debug) {
console.log(`[Stick to Me Debug] ${message}`);
}
}

document.addEventListener('mousemove', function(e) {
lastx = e.pageX;
lasty = e.pageY;
debugLog(`Mouse moved to (${lastx}, ${lasty})`);
});

document.addEventListener('mouseleave', function(e) {
debugLog('Mouse left the document');
setTimeout(function() { ontheleave(e); }, settings.delay);
});

Expand All @@ -72,6 +81,7 @@ const stickToMe = function (configs) {
document.addEventListener('mousemove', function bindOffset(e) {
if (offsetbind) {
document.addEventListener('mouseleave', function(e) {
debugLog('Chrome-specific mouseleave event triggered');
setTimeout(function() { ontheleave(e); }, settings.delay);
});
document.removeEventListener("mousemove", bindOffset);
Expand All @@ -83,6 +93,7 @@ const stickToMe = function (configs) {
window.addEventListener('resize', function() {
windowHeight = window.innerHeight;
windowWidth = window.innerWidth;
debugLog(`Window resized to ${windowWidth}x${windowHeight}`);
});

function ontheleave(e) {
Expand All @@ -108,8 +119,11 @@ const stickToMe = function (configs) {
leaveside = clienty >= ey2 ? "left" : "bottom";
}

debugLog(`Detected leave intent: ${leaveside}`);

if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {
if (clienty < 0 && clienty > -windowHeight && clientx > 0 && clientx < windowWidth) {
debugLog('Firefox-specific condition met, returning');
return;
}
}
Expand All @@ -123,6 +137,7 @@ const stickToMe = function (configs) {
if (chromealert) {
const cookiehowm = getamount("ck_stick_visit");
if (!settings.cookie || (settings.cookie && (cookiehowm < settings.maxamount || settings.maxamount == 0))) {
debugLog('Conditions met, firing onleave event');
settings.onleave.call(this, leaveside);

if (settings.layer != "") {
Expand All @@ -137,6 +152,7 @@ const stickToMe = function (configs) {
} else {
document.cookie = `ck_stick_visit=${cookiehowm}; path=/; SameSite=lax`;
}
debugLog(`Cookie set: ck_stick_visit=${cookiehowm}`);
}
lasttime = new Date().getTime();
}
Expand All @@ -156,6 +172,7 @@ const stickToMe = function (configs) {
const stickVarMap = new WeakMap();

function showbox() {
debugLog('Showing popup box');
if (!stickVarMap.get(document.body)) {
stickVarMap.set(document.body, 1);
const blockLayer = document.createElement('div');
Expand All @@ -180,6 +197,7 @@ const stickToMe = function (configs) {
if (settings.escclose) {
document.body.addEventListener('keyup', function(e) {
if (e.key === 'Escape') {
debugLog('Escape key pressed, closing popup');
stick_close();
}
});
Expand All @@ -204,6 +222,7 @@ const stickToMe = function (configs) {
}

function stick_close() {
debugLog('Closing popup');
const container = document.querySelector('.stick_container');
const blockLayer = document.querySelector('.stick_block_layer');
if (container) fadeOut(container, settings.fadespeed, () => container.remove());
Expand Down

0 comments on commit 66e9589

Please sign in to comment.