Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
62 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import React from 'react' | ||
|
||
function isMobile() { | ||
return /mobile/i.test(navigator.userAgent) | ||
} | ||
|
||
function removeNode(node) { | ||
node.parentNode.removeChild(node) | ||
} | ||
|
||
let hasDocumentCurrentScript = false | ||
|
||
if (typeof window !== 'undefined') { | ||
hasDocumentCurrentScript = !!document.currentScript | ||
} | ||
|
||
export default function EthicalAd({ condition, ...props }) { | ||
if (typeof window === 'undefined') { | ||
return ( | ||
<> | ||
<div data-ea-publisher="emotion-sh" {...props} /> | ||
<script | ||
dangerouslySetInnerHTML={{ | ||
__html: ` | ||
var removeNode = ${removeNode}; | ||
if (document.currentScript) { | ||
var isMobile = (${isMobile})(); | ||
var prevNode = document.currentScript.previousSibling; | ||
${condition === 'desktop' ? `isMobile && removeNode(prevNode);` : ''} | ||
${condition === 'mobile' ? `!isMobile && removeNode(prevNode);` : ''} | ||
removeNode(document.currentScript); | ||
} else { | ||
[].slice.call(document.querySelectorAll('[data-ea-publisher]')).forEach(function (node) { | ||
removeNode(node.nextSibling); | ||
removeNode(node); | ||
}); | ||
} | ||
` | ||
}} | ||
/> | ||
</> | ||
) | ||
} | ||
|
||
if ( | ||
!hasDocumentCurrentScript || | ||
(condition === 'desktop' && isMobile()) || | ||
(condition === 'mobile' && !isMobile()) | ||
) { | ||
return null | ||
} | ||
|
||
return <div data-ea-publisher="emotion-sh" {...props} /> | ||
} |
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