-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1008 from madfish-solutions/TW-1104-integrate-sli…
…se-xyz-to-temple-wallet-extension Tw 1104 integrate slise xyz to temple wallet extension
- Loading branch information
Showing
11 changed files
with
143 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
interface AdContainerProps { | ||
element: HTMLElement; | ||
width: number; | ||
} | ||
|
||
const getFinalWidth = (element: Element) => { | ||
const elementStyle = getComputedStyle(element); | ||
const rawWidthFromStyle = elementStyle.width; | ||
const rawWidthFromAttribute = element.getAttribute('width'); | ||
|
||
return Number((rawWidthFromAttribute || rawWidthFromStyle).replace('px', '') || element.clientWidth); | ||
}; | ||
|
||
export const getAdsContainers = () => { | ||
const builtInAdsImages = [...document.querySelectorAll('span + img')].filter(element => { | ||
const { width, height } = element.getBoundingClientRect(); | ||
const label = element.previousElementSibling?.innerHTML ?? ''; | ||
|
||
return (width > 0 || height > 0) && ['Featured', 'Ad'].includes(label); | ||
}); | ||
const coinzillaBanners = [...document.querySelectorAll('.coinzilla')]; | ||
const bitmediaBanners = [...document.querySelectorAll('iframe[src*="media.bmcdn"], iframe[src*="cdn.bmcdn"]')]; | ||
|
||
return builtInAdsImages | ||
.map((image): AdContainerProps | null => { | ||
const element = image.closest('div'); | ||
|
||
return element && { element, width: getFinalWidth(image) }; | ||
}) | ||
.concat( | ||
[...bitmediaBanners, ...coinzillaBanners].map(banner => { | ||
const parentElement = banner.parentElement; | ||
const closestDiv = parentElement?.closest('div') ?? null; | ||
const element = bitmediaBanners.includes(banner) ? closestDiv : parentElement; | ||
const widthDefinedElement = element?.parentElement ?? parentElement; | ||
const bannerFrame = banner.tagName === 'iframe' ? banner : banner.querySelector('iframe'); | ||
|
||
return element && { element, width: getFinalWidth(bannerFrame || widthDefinedElement!) }; | ||
}) | ||
) | ||
.filter((element): element is AdContainerProps => Boolean(element)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const getSlotId = () => { | ||
const hostnameParts = window.location.hostname.split('.').filter(part => part !== 'www'); | ||
const serviceId = hostnameParts[0]; | ||
const pathnameParts = window.location.pathname.split('/').filter(part => part !== '' && !/0x[0-9a-f]+/i.test(part)); | ||
|
||
return [serviceId, ...pathnameParts].join('-'); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React, { memo } from 'react'; | ||
|
||
import { SliseAd as OriginalSliseAd, SliseAdProps } from '@slise/embed-react'; | ||
|
||
import { useDidMount } from 'lib/ui/hooks/useDidMount'; | ||
|
||
interface CunningSliseAdProps extends Omit<SliseAdProps, 'format' | 'style'> { | ||
width: number; | ||
height: number; | ||
} | ||
|
||
export const SliseAd = memo(({ width, height, ...restProps }: CunningSliseAdProps) => { | ||
useDidMount(() => require('./slise-ad.embed')); | ||
|
||
return <OriginalSliseAd {...restProps} format={`${width}x${height}`} style={{ width, height }} />; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import React from 'react'; | ||
|
||
import debounce from 'debounce'; | ||
import { createRoot } from 'react-dom/client'; | ||
import browser from 'webextension-polyfill'; | ||
|
||
import { WEBSITES_ANALYTICS_ENABLED } from 'lib/constants'; | ||
import { getAdsContainers } from 'lib/slise/get-ads-containers'; | ||
import { getSlotId } from 'lib/slise/get-slot-id'; | ||
import { SliseAd } from 'lib/slise/slise-ad'; | ||
|
||
const availableAdsResolutions = [ | ||
{ width: 270, height: 90 }, | ||
{ width: 728, height: 90 } | ||
]; | ||
|
||
const replaceAds = debounce( | ||
() => { | ||
try { | ||
const adsContainers = getAdsContainers(); | ||
|
||
adsContainers.forEach(({ element: adContainer, width: containerWidth }) => { | ||
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 = createRoot(adContainer); | ||
adRoot.render( | ||
<SliseAd slotId={getSlotId()} pub="pub-25" width={adsResolution.width} height={adsResolution.height} /> | ||
); | ||
}); | ||
} catch {} | ||
}, | ||
100, | ||
true | ||
); | ||
|
||
// Prevents the script from running in an Iframe | ||
if (window.frameElement === null) { | ||
browser.storage.local.get(WEBSITES_ANALYTICS_ENABLED).then(storage => { | ||
if (storage[WEBSITES_ANALYTICS_ENABLED]) { | ||
// Replace ads with those from Slise | ||
window.addEventListener('load', () => replaceAds()); | ||
window.addEventListener('ready', () => replaceAds()); | ||
setInterval(() => replaceAds(), 1000); | ||
replaceAds(); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters