diff --git a/src/replaceAds.ts b/src/replaceAds.ts index 6d752ec623..5a8bed7d1e 100644 --- a/src/replaceAds.ts +++ b/src/replaceAds.ts @@ -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'; @@ -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) {