Skip to content

Commit

Permalink
Mute audio from playing in ads
Browse files Browse the repository at this point in the history
  • Loading branch information
Geometrically committed Sep 29, 2024
1 parent e81a4ad commit 720fce4
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 8 deletions.
83 changes: 83 additions & 0 deletions apps/app/src/api/ads-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,86 @@ document.addEventListener(
window.open = (url, target, features) => {
window.top.postMessage({ modrinthOpenUrl: url }, 'https://modrinth.com')
}

function muteAudioContext() {
if (window.AudioContext || window.webkitAudioContext) {
const AudioContext = window.AudioContext || window.webkitAudioContext
const originalCreateMediaElementSource = AudioContext.prototype.createMediaElementSource
const originalCreateMediaStreamSource = AudioContext.prototype.createMediaStreamSource
const originalCreateMediaStreamTrackSource = AudioContext.prototype.createMediaStreamTrackSource
const originalCreateBufferSource = AudioContext.prototype.createBufferSource
const originalCreateOscillator = AudioContext.prototype.createOscillator

AudioContext.prototype.createGain = function () {
const gain = originalCreateGain.call(this)
gain.gain.value = 0
return gain
}

AudioContext.prototype.createMediaElementSource = function (mediaElement) {
const source = originalCreateMediaElementSource.call(this, mediaElement)
source.connect(this.createGain())
return source
}

AudioContext.prototype.createMediaStreamSource = function (mediaStream) {
const source = originalCreateMediaStreamSource.call(this, mediaStream)
source.connect(this.createGain())
return source
}

AudioContext.prototype.createMediaStreamTrackSource = function (mediaStreamTrack) {
const source = originalCreateMediaStreamTrackSource.call(this, mediaStreamTrack)
source.connect(this.createGain())
return source
}

AudioContext.prototype.createBufferSource = function () {
const source = originalCreateBufferSource.call(this)
source.connect(this.createGain())
return source
}

AudioContext.prototype.createOscillator = function () {
const oscillator = originalCreateOscillator.call(this)
oscillator.connect(this.createGain())
return oscillator
}
}
}

function muteVideo(mediaElement) {
let count = Number(mediaElement.getAttribute('data-modrinth-muted-count') ?? 0)

if (!mediaElement.muted || mediaElement.volume !== 0) {
mediaElement.muted = true
mediaElement.volume = 0

mediaElement.setAttribute('data-modrinth-muted-count', count + 1)
}

if (count > 5) {
// Video is detected as malicious, so it is removed from the page
mediaElement.remove()
}
}

function muteVideos() {
document.querySelectorAll('video, audio').forEach(function (mediaElement) {
muteVideo(mediaElement)

if (!mediaElement.hasAttribute('data-modrinth-muted')) {
mediaElement.addEventListener('volumechange', () => muteVideo(mediaElement))

mediaElement.setAttribute('data-modrinth-muted', 'true')
}
})
}

document.addEventListener('DOMContentLoaded', () => {
muteVideos()
muteAudioContext()

const observer = new MutationObserver(muteVideos)
observer.observe(document.body, { childList: true, subtree: true })
})
12 changes: 4 additions & 8 deletions apps/app/src/api/ads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub struct AdsState {
pub malicious_origins: HashSet<String>,
}

const AD_LINK: &'static str = "https://modrinth.com/wrapper/app-ads-cookie";

pub fn init<R: Runtime>() -> TauriPlugin<R> {
tauri::plugin::Builder::<R>::new("ads")
.setup(|app, _api| {
Expand All @@ -32,7 +34,7 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
if let Some(webview) = app.webviews().get_mut("ads-window")
{
let _ = webview.navigate(
"https://modrinth.com/wrapper/app-ads-cookie"
AD_LINK
.parse()
.unwrap(),
);
Expand Down Expand Up @@ -88,7 +90,7 @@ pub async fn init_ads_window<R: Runtime>(
tauri::webview::WebviewBuilder::new(
"ads-window",
WebviewUrl::External(
"https://modrinth.com/wrapper/app-ads-cookie".parse().unwrap(),
AD_LINK.parse().unwrap(),
),
)
.initialization_script(LINK_SCRIPT)
Expand All @@ -102,12 +104,6 @@ pub async fn init_ads_window<R: Runtime>(
},
LogicalSize::new(width, height),
);

if let Ok(window) = window {
window.listen_any("click", |event| {
println!("click: {:?}", event);
});
}
}

Ok(())
Expand Down

0 comments on commit 720fce4

Please sign in to comment.