Skip to content

Commit 7a8af3d

Browse files
authoredOct 17, 2024··
Merge pull request #214 from readthedocs/davidfischer/visibilitychange-options
Make visibilitychange rotation an option
2 parents e5d3d51 + ebab6ee commit 7a8af3d

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed
 

Diff for: ‎dist/ethicalads.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ‎dist/ethicalads.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: ‎index.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,11 @@ const VIEWPORT_FUDGE_FACTOR = -3; // px
457457
const MIN_VIEW_TIME_ROTATION_DURATION = 45; // seconds
458458
const MAX_ROTATIONS = 3;
459459

460+
// Enable ad rotation on hash change (intra-site nav)
461+
const HASHCHANGE_ROTATION_ENABLE = true;
462+
460463
// Seconds after a tab comes back into focus to rotate an ad.
464+
const VISIBILITYCHANGE_ROTATION_ENABLE = false;
461465
const VISIBILITYCHANGE_ROTATION_DELAY = 3; // seconds
462466

463467
/* Placement object to query decision API and return an Element node
@@ -648,7 +652,9 @@ export class Placement {
648652
placement.rotate();
649653
}
650654
};
651-
window.addEventListener("hashchange", placement.hashchange_listener);
655+
if (HASHCHANGE_ROTATION_ENABLE) {
656+
window.addEventListener("hashchange", placement.hashchange_listener);
657+
}
652658

653659
// Listens to the window visibility
654660
// Rotates the ad when the window comes back into focus if
@@ -683,10 +689,12 @@ export class Placement {
683689
}
684690
}
685691
};
686-
document.addEventListener(
687-
"visibilitychange",
688-
placement.visibilitychange_listener
689-
);
692+
if (VISIBILITYCHANGE_ROTATION_ENABLE) {
693+
document.addEventListener(
694+
"visibilitychange",
695+
placement.visibilitychange_listener
696+
);
697+
}
690698

691699
return this;
692700
});
@@ -698,11 +706,11 @@ export class Placement {
698706
clearInterval(this.view_time_counter);
699707
}
700708

701-
if (this.hashchange_listener) {
709+
if (this.hashchange_listener && HASHCHANGE_ROTATION_ENABLE) {
702710
window.removeEventListener("hashchange", this.hashchange_listener);
703711
}
704712

705-
if (this.visibilitychange_listener) {
713+
if (this.visibilitychange_listener && VISIBILITYCHANGE_ROTATION_ENABLE) {
706714
document.removeEventListener(
707715
"visibilitychange",
708716
this.visibilitychange_listener

0 commit comments

Comments
 (0)
Please sign in to comment.