Skip to content

Commit

Permalink
TW-1180: Replace existing Ads to SliseXYZ. + flag
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-tsx committed Nov 30, 2023
1 parent e50689e commit 83004c9
Showing 1 changed file with 48 additions and 43 deletions.
91 changes: 48 additions & 43 deletions src/replaceAds.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import debounce from 'debounce';
import browser from 'webextension-polyfill';

import { AdType, ContentScriptType, ETHERSCAN_BUILTIN_ADS_WEBSITES, WEBSITES_ANALYTICS_ENABLED } from 'lib/constants';
Expand All @@ -11,56 +10,62 @@ const availableAdsResolutions = [

let oldHref = '';

const replaceAds = debounce(
async () => {
try {
const adsContainers = getAdsContainers();
const adsContainersToReplace = adsContainers.filter(
({ width, height }) =>
((width >= 600 && width <= 900) || (width >= 180 && width <= 430)) && height >= 60 && height <= 120
);
let processing = false;

const newHref = window.parent.location.href;
if (oldHref !== newHref && adsContainersToReplace.length > 0) {
oldHref = newHref;
const replaceAds = async () => {
if (processing) return;
processing = true;

browser.runtime.sendMessage({
type: ContentScriptType.ExternalAdsActivity,
url: window.parent.location.origin
});
}
try {
const adsContainers = getAdsContainers();
const adsContainersToReplace = adsContainers.filter(
({ width, height }) =>
((width >= 600 && width <= 900) || (width >= 180 && width <= 430)) && height >= 60 && height <= 120
);

const newHref = window.parent.location.href;
if (oldHref !== newHref && adsContainersToReplace.length > 0) {
oldHref = newHref;

if (!adsContainersToReplace.length) return;
browser.runtime.sendMessage({
type: ContentScriptType.ExternalAdsActivity,
url: window.parent.location.origin
});
}

const ReactDomModule = await import('react-dom/client');
const SliceAdModule = await import('lib/slise/slise-ad');
if (!adsContainersToReplace.length) {
processing = false;
return;
}

adsContainersToReplace.forEach(({ element: adContainer, width: containerWidth, type }) => {
let adsResolution = availableAdsResolutions[0];
for (let i = 1; i < availableAdsResolutions.length; i++) {
const candidate = availableAdsResolutions[i];
if (candidate.width <= containerWidth && candidate.width > adsResolution.width) {
adsResolution = candidate;
}
}
const ReactDomModule = await import('react-dom/client');
const SliceAdModule = await import('lib/slise/slise-ad');

if (
ETHERSCAN_BUILTIN_ADS_WEBSITES.some(urlPrefix => newHref.startsWith(urlPrefix)) &&
type === AdType.Coinzilla
) {
adContainer.style.textAlign = 'left';
adsContainersToReplace.forEach(({ element: adContainer, width: containerWidth, type }) => {
let adsResolution = availableAdsResolutions[0];
for (let i = 1; i < availableAdsResolutions.length; i++) {
const candidate = availableAdsResolutions[i];
if (candidate.width <= containerWidth && candidate.width > adsResolution.width) {
adsResolution = candidate;
}
}

const adRoot = ReactDomModule.createRoot(adContainer);
adRoot.render(SliceAdModule.buildSliceAdReactNode(adsResolution.width, adsResolution.height));
});
} catch (error) {
console.error('Replacing Ads error:', error);
}
},
100,
true
);
if (
ETHERSCAN_BUILTIN_ADS_WEBSITES.some(urlPrefix => newHref.startsWith(urlPrefix)) &&
type === AdType.Coinzilla
) {
adContainer.style.textAlign = 'left';
}

const adRoot = ReactDomModule.createRoot(adContainer);
adRoot.render(SliceAdModule.buildSliceAdReactNode(adsResolution.width, adsResolution.height));
});
} catch (error) {
console.error('Replacing Ads error:', error);
}

processing = false;
};

// Prevents the script from running in an Iframe
if (window.frameElement === null) {
Expand Down

0 comments on commit 83004c9

Please sign in to comment.