Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor, reduce log spam #400

Merged
merged 5 commits into from
Nov 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
267 changes: 141 additions & 126 deletions packages/frontend/src/scripts/components/ads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,25 @@ class ManagedAd {
if (value !== this._showing || (value && !this._installed)) {
if (value) {
this.adContainer.outer.style.display = '';
if (!this._installed) {
this.installAdPlacement();
}
this.adContainer.middle.prepend(this.adContainer.inner);
this.installAdPlacement();
// if (!this._installed) {
// }
// setTimeout(() => this.ad['checkVisible'](), 100);
}
else {
this.adContainer.outer.style.display = 'none';
this.adContainer.middle.removeChild(this.adContainer.inner);
}
this._showing = value;
}
}

onNavigate(): void {
this.ad?.['onNavigate']?.();
// // this.ad?.['onNavigate']?.();
// if (this.showing) {
// this.installAdPlacement();
// }
}

/**
Expand All @@ -85,7 +91,7 @@ class ManagedAd {
return;
}
setTimeout(() => {
console.debug(`createAd: ${window['nitroAds'] !== undefined}`);
console.debug(`createAd: ${this.id}`);
this.ad = window['nitroAds'].createAd(id, {
"refreshTime": 30,
"renderVisibleOnly": true,
Expand All @@ -104,7 +110,7 @@ class ManagedAd {
},
});
recordEvent('createAd', {'id': id});
});
}, 50);
this._installed = true;
}

Expand Down Expand Up @@ -196,32 +202,37 @@ extraLinksHolderInner.style.display = 'content';

window.addEventListener('resize', recheckAds);

window['currentAds'] = currentAds;

function recheckAds() {
console.debug('recheckAds');
let oneAdShown = false;
if (adsEnabled()) {
currentAds.forEach(ad => {
ad.recheck();
console.debug(`recheckAds: ${ad.id} => ${ad.showing}`);
if (ad.showing && !oneAdShown) {
oneAdShown = true;
ad.addExtras([extraLinksHolderInner]);
}
});
window['__cmp']?.('addConsentLink');
window['__uspapi']?.('addLink', 1);
}
else {
console.debug('recheckAds: not enabled');
}
if (oneAdShown) {
fallbackPrivacyArea.style.display = 'none';
}
else {
fallbackPrivacyArea.style.display = '';
moveLinksToFallbackPrivacyArea();
setTimeout(() => {

console.debug('recheckAds');
let oneAdShown = false;
if (adsEnabled()) {
currentAds.forEach(ad => {
ad.recheck();
console.debug(`recheckAds: ${ad.id} => ${ad.showing}`);
if (ad.showing && !oneAdShown) {
oneAdShown = true;
ad.addExtras([extraLinksHolderInner]);
}
});
window['__cmp']?.('addConsentLink');
window['__uspapi']?.('addLink', 1);
}
else {
console.debug('recheckAds: not enabled');
}
if (oneAdShown) {
fallbackPrivacyArea.style.display = 'none';
}
else {
fallbackPrivacyArea.style.display = '';
moveLinksToFallbackPrivacyArea();

}
}
});
}

window['recheckAds'] = recheckAds;
Expand All @@ -230,114 +241,118 @@ let firstLoad = true;

export function insertAds(element: HTMLElement) {

if (currentAds.length === 0) {
// Analytics
setTimeout(() => {
if (adsEnabled()) {
recordEvent('adsEnabled');
}
else if (Array.from(document.head.querySelectorAll('script')).find(script => script.src && script.src.includes('nitro'))) {
recordEvent('adsDisabled');
}
else {
recordEvent('adsNotInjected');
}
}, 10_000);

try {
// Display on very wide 2-column
const sideWideCond: DisplayCondition = (w, h) => w >= 1900 && h > 650;
// Display on very wide 2-column without sufficient height
const sideWideShortCond: DisplayCondition = (w, h) => w >= 1900 && h > 350 && !sideWideCond(w, h);
// Display on less wide 2-column, or wide 1-column
const sideNarrowCond: DisplayCondition = (w, h) =>
((w >= 1560 && h > 650)
|| (w <= 1210 && w >= 1012 && h > 650))
&& !sideWideCond(w, h) && !sideWideShortCond(w, h);
{
const size: AdSize = [300, 600];
const extraSizes: AdSize[] = [[160, 600], [300, 250]];
const adAreaLeftWide = new ManagedAd('float-area-wide-left', size, sideWideCond, extraSizes);
{
const outer = adAreaLeftWide.adContainer.outer;
const middle = adAreaLeftWide.adContainer.middle;
outer.style.left = '0';
middle.style.marginLeft = '-5px';
middle.style.borderRadius = '0 10px 0 0';
}
currentAds.push(adAreaLeftWide);
setTimeout(() => {

const adAreaRightWide = new ManagedAd('float-area-wide-right', size, sideWideCond, extraSizes);
{
const outer = adAreaRightWide.adContainer.outer;
const middle = adAreaRightWide.adContainer.middle;
outer.style.left = '100%';
middle.style.right = '0';
middle.style.marginRight = '-5px';
middle.style.borderRadius = '10px 0 0 0';
if (currentAds.length === 0) {
// Analytics
setTimeout(() => {
if (adsEnabled()) {
recordEvent('adsEnabled');
}
currentAds.push(adAreaRightWide);
}
{
const size: AdSize = [300, 250];
const adAreaLeftWide = new ManagedAd('float-area-wide-short-left', size, sideWideShortCond);
{
const outer = adAreaLeftWide.adContainer.outer;
const middle = adAreaLeftWide.adContainer.middle;
outer.style.left = '0';
middle.style.marginLeft = '-5px';
middle.style.borderRadius = '0 10px 0 0';
else if (Array.from(document.head.querySelectorAll('script')).find(script => script.src && script.src.includes('nitro'))) {
recordEvent('adsDisabled');
}
currentAds.push(adAreaLeftWide);

const adAreaRightWide = new ManagedAd('float-area-wide-short-right', size, sideWideShortCond);
else {
recordEvent('adsNotInjected', {'page': document.location.search});
}
}, 10_000);

try {
// Display on very wide 2-column
const sideWideCond: DisplayCondition = (w, h) => w >= 1900 && h > 650;
// Display on very wide 2-column without sufficient height
const sideWideShortCond: DisplayCondition = (w, h) => w >= 1900 && h > 350 && !sideWideCond(w, h);
// Display on less wide 2-column, or wide 1-column
const sideNarrowCond: DisplayCondition = (w, h) =>
((w >= 1560 && h > 650)
|| (w <= 1210 && w >= 1012 && h > 650))
&& !sideWideCond(w, h) && !sideWideShortCond(w, h);
{
const outer = adAreaRightWide.adContainer.outer;
const middle = adAreaRightWide.adContainer.middle;
outer.style.left = '100%';
middle.style.right = '0';
middle.style.marginRight = '-5px';
middle.style.borderRadius = '10px 0 0 0';
const size: AdSize = [300, 600];
const extraSizes: AdSize[] = [[160, 600], [300, 250]];
const adAreaLeftWide = new ManagedAd('float-area-wide-left', size, sideWideCond, extraSizes);
{
const outer = adAreaLeftWide.adContainer.outer;
const middle = adAreaLeftWide.adContainer.middle;
outer.style.left = '0';
middle.style.marginLeft = '-5px';
middle.style.borderRadius = '0 10px 0 0';
}
currentAds.push(adAreaLeftWide);

const adAreaRightWide = new ManagedAd('float-area-wide-right', size, sideWideCond, extraSizes);
{
const outer = adAreaRightWide.adContainer.outer;
const middle = adAreaRightWide.adContainer.middle;
outer.style.left = '100%';
middle.style.right = '0';
middle.style.marginRight = '-5px';
middle.style.borderRadius = '10px 0 0 0';
}
currentAds.push(adAreaRightWide);
}
currentAds.push(adAreaRightWide);
}
{
const size: AdSize = [160, 600];
const adAreaLeftNarrow = new ManagedAd('float-area-narrow-left', size, sideNarrowCond);
{
const outer = adAreaLeftNarrow.adContainer.outer;
const middle = adAreaLeftNarrow.adContainer.middle;
outer.style.left = '0';
middle.style.marginLeft = '-5px';
middle.style.borderRadius = '0 10px 0 0';
const size: AdSize = [300, 250];
const adAreaLeftWide = new ManagedAd('float-area-wide-short-left', size, sideWideShortCond);
{
const outer = adAreaLeftWide.adContainer.outer;
const middle = adAreaLeftWide.adContainer.middle;
outer.style.left = '0';
middle.style.marginLeft = '-5px';
middle.style.borderRadius = '0 10px 0 0';
}
currentAds.push(adAreaLeftWide);

const adAreaRightWide = new ManagedAd('float-area-wide-short-right', size, sideWideShortCond);
{
const outer = adAreaRightWide.adContainer.outer;
const middle = adAreaRightWide.adContainer.middle;
outer.style.left = '100%';
middle.style.right = '0';
middle.style.marginRight = '-5px';
middle.style.borderRadius = '10px 0 0 0';
}
currentAds.push(adAreaRightWide);
}
currentAds.push(adAreaLeftNarrow);

const adAreaRightNarrow = new ManagedAd('float-area-narrow-right', size, sideNarrowCond);
{
const outer = adAreaRightNarrow.adContainer.outer;
const middle = adAreaRightNarrow.adContainer.middle;
outer.style.left = '100%';
middle.style.right = '0';
middle.style.marginRight = '-5px';
middle.style.borderRadius = '10px 0 0 0';
const size: AdSize = [160, 600];
const adAreaLeftNarrow = new ManagedAd('float-area-narrow-left', size, sideNarrowCond);
{
const outer = adAreaLeftNarrow.adContainer.outer;
const middle = adAreaLeftNarrow.adContainer.middle;
outer.style.left = '0';
middle.style.marginLeft = '-5px';
middle.style.borderRadius = '0 10px 0 0';
}
currentAds.push(adAreaLeftNarrow);

const adAreaRightNarrow = new ManagedAd('float-area-narrow-right', size, sideNarrowCond);
{
const outer = adAreaRightNarrow.adContainer.outer;
const middle = adAreaRightNarrow.adContainer.middle;
outer.style.left = '100%';
middle.style.right = '0';
middle.style.marginRight = '-5px';
middle.style.borderRadius = '10px 0 0 0';
}
currentAds.push(adAreaRightNarrow);
}
currentAds.push(adAreaRightNarrow);
}
catch (e) {
console.error(e);
}
}
catch (e) {
console.error(e);
currentAds.forEach(ad => ad.showing = false);
element.prepend(...currentAds.map(a => a.adContainer.outer));
setTimeout(recheckAds);
setTimeout(recheckAds, 2_000);
if (firstLoad) {
firstLoad = false;
}
}
element.prepend(...currentAds.map(a => a.adContainer.outer));
setTimeout(recheckAds);
setTimeout(recheckAds, 2_000);
if (firstLoad) {
firstLoad = false;
}
else {
currentAds.forEach(ad => ad.onNavigate());
}
else {
setTimeout(() => currentAds.forEach(ad => ad.onNavigate()));
}
}, 200);
}

// set up extra links
Expand Down
Loading