2020-01-24 19:31:49 +01:00
|
|
|
// @flow
|
2020-10-01 22:59:11 +02:00
|
|
|
import { DOMAIN, SHOW_ADS } from 'config';
|
2020-01-24 19:31:49 +01:00
|
|
|
import * as PAGES from 'constants/pages';
|
2020-01-23 17:21:36 +01:00
|
|
|
import React, { useEffect } from 'react';
|
2020-01-24 19:31:49 +01:00
|
|
|
import { withRouter } from 'react-router';
|
|
|
|
import I18nMessage from 'component/i18nMessage';
|
|
|
|
import Button from 'component/button';
|
2020-03-26 22:47:07 +01:00
|
|
|
import classnames from 'classnames';
|
2021-01-23 22:21:25 +01:00
|
|
|
|
2021-12-01 01:53:23 +01:00
|
|
|
const ADS_URL =
|
|
|
|
'https://cdn.vidcrunch.com/integrations/618bb4d28aac298191eec411/Lbry_Odysee.com_Responsive_Floating_DFP_Rev70_1011.js';
|
2021-11-23 16:21:33 +01:00
|
|
|
const ADS_TAG = 'vidcrunchJS537102317';
|
2021-12-01 01:53:23 +01:00
|
|
|
const IOS_ADS_URL =
|
|
|
|
'https://cdn.vidcrunch.com/integrations/618bb4d28aac298191eec411/Lbry_Odysee.com_Mobile_Floating_DFP_Rev70_1611.js';
|
2021-11-23 16:21:33 +01:00
|
|
|
const IOS_ADS_TAG = 'vidcrunchJS199212779';
|
2021-12-01 01:53:23 +01:00
|
|
|
const HOMEPAGE_ADS_URL =
|
|
|
|
'https://cdn.vidcrunch.com/integrations/618bb4d28aac298191eec411/Lbry_Odysee.com_Responsive_Floating_300x169_DFP_Rev70_1211.js';
|
2021-11-30 23:01:03 +01:00
|
|
|
const HOMEPAGE_ADS_TAG = 'vidcrunchJS330442776';
|
|
|
|
|
2022-01-14 20:21:17 +01:00
|
|
|
const EU_AD_URL = 'https://tg1.vidcrunch.com/api/adserver/spt?AV_TAGID=61dff05c599f1e20b01085d4&AV_PUBLISHERID=6182c8993c8ae776bd5635e9';
|
|
|
|
const EU_AD_TAG = 'AV61dff05c599f1e20b01085d4';
|
|
|
|
|
2021-11-23 16:21:33 +01:00
|
|
|
const IS_IOS =
|
|
|
|
(/iPad|iPhone|iPod/.test(navigator.platform) ||
|
|
|
|
// for iOS 13+ , platform is MacIntel, so use this to test
|
|
|
|
(navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1)) &&
|
|
|
|
!window.MSStream;
|
|
|
|
|
2021-12-13 17:25:39 +01:00
|
|
|
const IS_ANDROID = /Android/i.test(navigator.userAgent);
|
|
|
|
|
|
|
|
const IS_FIREFOX = /Firefox/i.test(navigator.userAgent);
|
|
|
|
|
|
|
|
const isFirefoxAndroid = IS_ANDROID && IS_FIREFOX;
|
|
|
|
|
2020-01-24 19:31:49 +01:00
|
|
|
type Props = {
|
|
|
|
location: { pathname: string },
|
2020-03-26 22:47:07 +01:00
|
|
|
type: string,
|
|
|
|
small: boolean,
|
2021-03-21 20:02:50 +01:00
|
|
|
claim: Claim,
|
2021-01-31 04:47:06 +01:00
|
|
|
isMature: boolean,
|
2021-12-01 15:52:03 +01:00
|
|
|
authenticated: boolean,
|
2022-01-05 09:13:39 +01:00
|
|
|
triggerBlacklist: boolean,
|
2020-01-24 19:31:49 +01:00
|
|
|
};
|
|
|
|
|
2021-12-02 19:22:51 +01:00
|
|
|
function removeIfExists(querySelector) {
|
|
|
|
const element = document.querySelector(querySelector);
|
|
|
|
if (element) element.remove();
|
|
|
|
}
|
|
|
|
|
2020-01-24 19:31:49 +01:00
|
|
|
function Ads(props: Props) {
|
|
|
|
const {
|
|
|
|
location: { pathname },
|
2021-11-30 23:01:03 +01:00
|
|
|
type = 'video',
|
2020-03-26 22:47:07 +01:00
|
|
|
small,
|
2021-12-01 15:52:03 +01:00
|
|
|
authenticated,
|
2021-12-06 19:01:40 +01:00
|
|
|
triggerBlacklist,
|
2020-01-24 19:31:49 +01:00
|
|
|
} = props;
|
2020-03-26 22:47:07 +01:00
|
|
|
|
2022-01-04 17:55:59 +01:00
|
|
|
const shouldShowAds = SHOW_ADS && !authenticated;
|
|
|
|
|
2022-01-14 20:21:17 +01:00
|
|
|
// this is populated from app based on location
|
|
|
|
let isInEu = localStorage.getItem('gdprRequired');
|
|
|
|
// cant store booleans so we store it as a string
|
|
|
|
isInEu = isInEu === 'true';
|
|
|
|
|
2021-11-30 23:01:03 +01:00
|
|
|
// load ad and tags here
|
2021-11-23 16:21:33 +01:00
|
|
|
let scriptUrlToUse;
|
|
|
|
let tagNameToUse;
|
2021-11-30 23:01:03 +01:00
|
|
|
if (type === 'video') {
|
2022-01-14 20:21:17 +01:00
|
|
|
if (isInEu) {
|
|
|
|
tagNameToUse = EU_AD_TAG;
|
|
|
|
scriptUrlToUse = EU_AD_URL;
|
|
|
|
} else if (IS_IOS) {
|
2021-11-30 23:01:03 +01:00
|
|
|
tagNameToUse = IOS_ADS_TAG;
|
|
|
|
scriptUrlToUse = IOS_ADS_URL;
|
|
|
|
} else {
|
|
|
|
tagNameToUse = ADS_TAG;
|
|
|
|
scriptUrlToUse = ADS_URL;
|
|
|
|
}
|
|
|
|
} else if (type === 'homepage') {
|
2022-01-14 20:21:17 +01:00
|
|
|
if (isInEu) {
|
|
|
|
tagNameToUse = EU_AD_TAG;
|
|
|
|
scriptUrlToUse = EU_AD_URL;
|
|
|
|
} else {
|
|
|
|
tagNameToUse = HOMEPAGE_ADS_TAG;
|
|
|
|
scriptUrlToUse = HOMEPAGE_ADS_URL;
|
|
|
|
}
|
2021-11-23 16:21:33 +01:00
|
|
|
}
|
|
|
|
|
2021-11-30 23:01:03 +01:00
|
|
|
// add script to DOM
|
2020-01-23 17:21:36 +01:00
|
|
|
useEffect(() => {
|
2022-01-04 17:55:59 +01:00
|
|
|
if (isFirefoxAndroid) return;
|
|
|
|
|
|
|
|
if (shouldShowAds) {
|
2021-01-22 00:57:29 +01:00
|
|
|
let script;
|
2020-03-26 22:47:07 +01:00
|
|
|
try {
|
2021-11-23 16:21:33 +01:00
|
|
|
script = document.createElement('script');
|
|
|
|
script.src = scriptUrlToUse;
|
2020-03-26 22:47:07 +01:00
|
|
|
// $FlowFixMe
|
2021-12-01 04:17:28 +01:00
|
|
|
document.head.appendChild(script);
|
2020-03-26 22:47:07 +01:00
|
|
|
|
2021-11-30 23:01:03 +01:00
|
|
|
return () => {
|
2020-02-05 06:13:29 +01:00
|
|
|
// $FlowFixMe
|
2021-11-30 23:01:03 +01:00
|
|
|
document.head.removeChild(script);
|
2021-12-02 19:22:51 +01:00
|
|
|
|
|
|
|
// clear aniview state to allow ad reload
|
2021-12-02 18:04:40 +01:00
|
|
|
delete window.aniplayerPos;
|
|
|
|
delete window.storageAni;
|
|
|
|
delete window.__VIDCRUNCH_CONFIG_618bb4d28aac298191eec411__;
|
|
|
|
delete window.__player_618bb4d28aac298191eec411__;
|
2021-12-02 19:22:51 +01:00
|
|
|
|
|
|
|
// clean DOM elements from ad related elements
|
|
|
|
removeIfExists('[src^="https://cdn.vidcrunch.com/618bb4d28aac298191eec411.js"]');
|
|
|
|
removeIfExists('[src^="https://player.aniview.com/script/6.1/aniview.js"]');
|
|
|
|
removeIfExists('[id^="AVLoaderaniplayer_vidcrunch"]');
|
|
|
|
removeIfExists('#av_css_id');
|
|
|
|
removeIfExists('#customAniviewStyling');
|
2021-11-30 23:01:03 +01:00
|
|
|
};
|
|
|
|
} catch (e) {}
|
2020-02-05 06:13:29 +01:00
|
|
|
}
|
2021-12-01 04:17:28 +01:00
|
|
|
}, []);
|
2020-03-26 22:47:07 +01:00
|
|
|
|
2021-11-30 23:01:03 +01:00
|
|
|
// display to say "sign up to not see these"
|
2020-03-26 22:47:07 +01:00
|
|
|
const adsSignInDriver = (
|
|
|
|
<I18nMessage
|
|
|
|
tokens={{
|
2021-01-03 14:55:24 +01:00
|
|
|
log_in_to_domain: (
|
2020-07-14 21:08:54 +02:00
|
|
|
<Button
|
|
|
|
button="link"
|
2021-01-03 14:55:24 +01:00
|
|
|
label={__('Log in to %domain%', { domain: DOMAIN })}
|
2020-07-14 21:08:54 +02:00
|
|
|
navigate={`/$/${PAGES.AUTH}?redirect=${pathname}`}
|
|
|
|
/>
|
2020-03-26 22:47:07 +01:00
|
|
|
),
|
|
|
|
}}
|
|
|
|
>
|
2021-11-11 23:15:50 +01:00
|
|
|
Hate these? %log_in_to_domain% for an ad free experience.
|
2020-03-26 22:47:07 +01:00
|
|
|
</I18nMessage>
|
|
|
|
);
|
2020-01-23 17:21:36 +01:00
|
|
|
|
2021-11-30 23:01:03 +01:00
|
|
|
// ad shown in the related videos area
|
2021-01-22 00:57:29 +01:00
|
|
|
const videoAd = (
|
2020-03-26 22:47:07 +01:00
|
|
|
<div className="ads__claim-item">
|
2022-01-06 20:28:27 +01:00
|
|
|
<div className="ad__container">
|
|
|
|
<div id={tagNameToUse} className="ads__injected-video" style={{ display: 'none' }} />
|
|
|
|
</div>
|
2020-03-26 22:47:07 +01:00
|
|
|
<div
|
|
|
|
className={classnames('ads__claim-text', {
|
|
|
|
'ads__claim-text--small': small,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
<div>Ad</div>
|
|
|
|
<p>{adsSignInDriver}</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-01-22 00:57:29 +01:00
|
|
|
);
|
|
|
|
|
2021-11-30 23:01:03 +01:00
|
|
|
// homepage ad in a card
|
|
|
|
const homepageCardAd = (
|
2021-12-01 01:53:23 +01:00
|
|
|
<div className="homepageAdContainer media__thumb" style={{ display: 'none' }}>
|
|
|
|
<div id={tagNameToUse} className="homepageAdDiv media__thumb" style={{ display: 'none' }} />
|
2021-11-30 23:01:03 +01:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
2021-12-13 18:05:58 +01:00
|
|
|
// dont show if ads disabled on instance, blacklist word matched
|
2021-12-06 19:01:40 +01:00
|
|
|
if (!SHOW_ADS || triggerBlacklist) {
|
2021-01-22 00:57:29 +01:00
|
|
|
return false;
|
|
|
|
}
|
2021-12-13 18:05:58 +01:00
|
|
|
// disable ads for firefox android because they don't work properly
|
|
|
|
if (isFirefoxAndroid) return false;
|
|
|
|
|
|
|
|
// sidebar ad (in recommended videos)
|
2021-01-22 00:57:29 +01:00
|
|
|
if (type === 'video') {
|
|
|
|
return videoAd;
|
|
|
|
}
|
2021-11-30 23:01:03 +01:00
|
|
|
if (type === 'homepage') {
|
|
|
|
return homepageCardAd;
|
|
|
|
}
|
2020-01-23 19:44:55 +01:00
|
|
|
}
|
2020-01-23 17:21:36 +01:00
|
|
|
|
2022-01-04 18:43:35 +01:00
|
|
|
// returns true if passed element is fully visible on screen
|
|
|
|
function isScrolledIntoView(el) {
|
|
|
|
const rect = el.getBoundingClientRect();
|
|
|
|
const elemTop = rect.top;
|
|
|
|
const elemBottom = rect.bottom;
|
|
|
|
|
|
|
|
// Only completely visible elements return true:
|
|
|
|
const isVisible = elemTop >= 0 && elemBottom <= window.innerHeight;
|
|
|
|
return isVisible;
|
|
|
|
}
|
|
|
|
|
2022-01-04 19:18:37 +01:00
|
|
|
async function injectAd(shouldShowAds: boolean) {
|
|
|
|
// don't inject on firefox android or for authenticated users or no ads on instance
|
|
|
|
if (isFirefoxAndroid || !shouldShowAds) return;
|
2022-01-04 18:43:35 +01:00
|
|
|
// test if adblock is enabled
|
|
|
|
let adBlockEnabled = false;
|
|
|
|
const googleAdUrl = 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js';
|
|
|
|
try {
|
|
|
|
await fetch(new Request(googleAdUrl)).catch((_) => {
|
|
|
|
adBlockEnabled = true;
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
adBlockEnabled = true;
|
|
|
|
} finally {
|
|
|
|
if (!adBlockEnabled) {
|
|
|
|
// select the cards on page
|
|
|
|
let cards = document.getElementsByClassName('card claim-preview--tile');
|
|
|
|
// eslint-disable-next-line no-inner-declarations
|
|
|
|
function checkFlag() {
|
|
|
|
if (cards.length === 0) {
|
|
|
|
window.setTimeout(checkFlag, 100);
|
|
|
|
} else {
|
|
|
|
// find the last fully visible card
|
|
|
|
let lastCard;
|
|
|
|
|
|
|
|
// width of browser window
|
|
|
|
const windowWidth = window.innerWidth;
|
|
|
|
|
|
|
|
// on small screens, grab the second item
|
|
|
|
if (windowWidth <= 900) {
|
|
|
|
lastCard = cards[1];
|
|
|
|
} else {
|
|
|
|
// otherwise, get the last fully visible card
|
|
|
|
for (const card of cards) {
|
|
|
|
const isFullyVisible = isScrolledIntoView(card);
|
|
|
|
if (!isFullyVisible) break;
|
|
|
|
lastCard = card;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if no last card was found, just exit the function to not cause errors
|
|
|
|
if (!lastCard) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// clone the last card
|
|
|
|
// $FlowFixMe
|
|
|
|
const clonedCard = lastCard.cloneNode(true);
|
|
|
|
|
|
|
|
// insert cloned card
|
|
|
|
// $FlowFixMe
|
|
|
|
lastCard.parentNode.insertBefore(clonedCard, lastCard);
|
|
|
|
|
|
|
|
// change the appearance of the cloned card
|
|
|
|
// $FlowFixMe
|
|
|
|
clonedCard.querySelector('.claim__menu-button').remove();
|
|
|
|
|
|
|
|
// $FlowFixMe
|
|
|
|
clonedCard.querySelector('.truncated-text').innerHTML = __(
|
|
|
|
'Hate these? Login to Odysee for an ad free experience'
|
|
|
|
);
|
|
|
|
|
|
|
|
// $FlowFixMe
|
|
|
|
clonedCard.querySelector('.claim-tile__info').remove();
|
|
|
|
|
|
|
|
// $FlowFixMe
|
|
|
|
clonedCard.querySelector('[role="none"]').removeAttribute('href');
|
|
|
|
|
|
|
|
// $FlowFixMe
|
|
|
|
clonedCard.querySelector('.claim-tile__header').firstChild.href = '/$/signin';
|
|
|
|
|
|
|
|
// $FlowFixMe
|
|
|
|
clonedCard.querySelector('.claim-tile__title').firstChild.removeAttribute('aria-label');
|
|
|
|
|
|
|
|
// $FlowFixMe
|
|
|
|
clonedCard.querySelector('.claim-tile__title').firstChild.removeAttribute('title');
|
|
|
|
|
|
|
|
// $FlowFixMe
|
|
|
|
clonedCard.querySelector('.claim-tile__header').firstChild.removeAttribute('aria-label');
|
|
|
|
|
|
|
|
// $FlowFixMe
|
|
|
|
clonedCard
|
|
|
|
.querySelector('.media__thumb')
|
|
|
|
.replaceWith(document.getElementsByClassName('homepageAdContainer')[0]);
|
|
|
|
|
|
|
|
// show the homepage ad which is not displayed at first
|
|
|
|
document.getElementsByClassName('homepageAdContainer')[0].style.display = 'block';
|
|
|
|
|
|
|
|
const thumbnail = window.getComputedStyle(lastCard.querySelector('.media__thumb'));
|
|
|
|
|
|
|
|
const styles = `#av-container, #AVcontent, #aniBox {
|
|
|
|
height: ${thumbnail.height} !important;
|
|
|
|
width: ${thumbnail.width} !important;
|
|
|
|
}`;
|
|
|
|
|
|
|
|
const styleSheet = document.createElement('style');
|
|
|
|
styleSheet.type = 'text/css';
|
|
|
|
styleSheet.id = 'customAniviewStyling';
|
|
|
|
styleSheet.innerText = styles;
|
|
|
|
|
|
|
|
// $FlowFixMe
|
|
|
|
document.head.appendChild(styleSheet);
|
|
|
|
|
|
|
|
// delete last card to not introduce layout shifts
|
|
|
|
lastCard.remove();
|
|
|
|
|
|
|
|
// addresses bug where ad doesn't show up until a scroll event
|
|
|
|
document.dispatchEvent(new CustomEvent('scroll'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
checkFlag();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 19:31:49 +01:00
|
|
|
export default withRouter(Ads);
|
2022-01-04 18:43:35 +01:00
|
|
|
export { injectAd };
|