Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/app/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ fn main() {
"hide_ads_window",
"scroll_ads_window",
"show_ads_window",
"show_ads_consent_overlay",
"hide_ads_consent_overlay",
"record_ads_click",
"open_link",
"get_ads_personalization",
Expand Down
97 changes: 94 additions & 3 deletions apps/app/src/api/ads-init.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const MODRINTH_ORIGIN = 'https://modrinth.com'

document.addEventListener(
'click',
function (e) {
window.top.postMessage({ modrinthAdClick: true }, 'https://modrinth.com')
window.top.postMessage({ modrinthAdClick: true }, MODRINTH_ORIGIN)

let target = e.target
while (target != null) {
if (target.matches('a')) {
e.preventDefault()
if (target.href) {
window.top.postMessage({ modrinthOpenUrl: target.href }, 'https://modrinth.com')
window.top.postMessage({ modrinthOpenUrl: target.href }, MODRINTH_ORIGIN)
}
break
}
Expand All @@ -19,7 +21,93 @@ document.addEventListener(
)

window.open = (url, target, features) => {
window.top.postMessage({ modrinthOpenUrl: url }, 'https://modrinth.com')
window.top.postMessage({ modrinthOpenUrl: url }, MODRINTH_ORIGIN)
}

let modrinthAdsConsentOverlayShown = false
let modrinthTcfListenerInstalled = false
let modrinthTcfListenerAttempts = 0

function installAdsConsentOverlayStyle() {
if (document.getElementById('modrinth-ads-consent-overlay-style')) {
return
}
const style = document.createElement('style')
style.id = 'modrinth-ads-consent-overlay-style'
style.textContent = `
html.modrinth-ads-consent-overlay #modrinth-rail-1 {
visibility: hidden !important;
}
`
document.documentElement.appendChild(style)
}

function getTauriInvoke() {
return window.__TAURI__?.core?.invoke ?? window.__TAURI_INTERNALS__?.invoke
}

function invokeAdsConsentOverlayCommand(shown) {
const invoke = getTauriInvoke()

if (typeof invoke !== 'function') {
return
}

const command = shown ? 'show_ads_consent_overlay' : 'hide_ads_consent_overlay'
const args = shown ? {} : { dpr: window.devicePixelRatio }

invoke(`plugin:ads|${command}`, args).catch(() => {})
}

function setAdsConsentOverlay(shown) {
if (modrinthAdsConsentOverlayShown === shown) return

modrinthAdsConsentOverlayShown = shown
installAdsConsentOverlayStyle()
document.documentElement.classList.toggle('modrinth-ads-consent-overlay', shown)

if (window.top === window) {
invokeAdsConsentOverlayCommand(shown)
} else {
window.top.postMessage({ modrinthAdsConsentOverlay: shown }, MODRINTH_ORIGIN)
}
}

if (window.top === window) {
window.addEventListener('message', (event) => {
if (
event.origin === MODRINTH_ORIGIN &&
typeof event.data?.modrinthAdsConsentOverlay === 'boolean'
) {
setAdsConsentOverlay(event.data.modrinthAdsConsentOverlay)
}
})
}

function handleTcfConsentEvent(tcData, success) {
if (!success || !tcData) return

if (tcData.eventStatus === 'cmpuishown') {
setAdsConsentOverlay(true)
} else if (tcData.eventStatus === 'useractioncomplete' || tcData.eventStatus === 'tcloaded') {
setAdsConsentOverlay(false)
}
}

// polling to install listener on tcf api
function installTcfConsentListener() {
if (modrinthTcfListenerInstalled) return

if (typeof window.__tcfapi === 'function') {
modrinthTcfListenerInstalled = true
window.__tcfapi('addEventListener', 2, handleTcfConsentEvent)
return
}

if (modrinthTcfListenerAttempts < 60) {
modrinthTcfListenerAttempts += 1
setTimeout(installTcfConsentListener, 500)
}
}

function muteAudioContext() {
Expand Down Expand Up @@ -100,7 +188,10 @@ function muteVideos() {
document.addEventListener('DOMContentLoaded', () => {
muteVideos()
muteAudioContext()
installTcfConsentListener()

const observer = new MutationObserver(muteVideos)
observer.observe(document.body, { childList: true, subtree: true })
})

installTcfConsentListener()
Loading
Loading