Skip to content

Instantly share code, notes, and snippets.

@cwelsys
Last active March 8, 2026 17:50
Show Gist options
  • Select an option

  • Save cwelsys/89bf8dbb1eeea673614de41f55e707dd to your computer and use it in GitHub Desktop.

Select an option

Save cwelsys/89bf8dbb1eeea673614de41f55e707dd to your computer and use it in GitHub Desktop.
AudioContext Suspender
// ==UserScript==
// @name AudioContext Suspender
// @description Suspends newly created AudioContexts after 2 seconds. Ported from the firefox extension created by h43z.
// @version 1.4
// @author cwel
// @license MPL-2.0
// @homepageURL https://github.com/h43z/audiocontextsuspender
// @match *://*/*
// @run-at document-start
// @grant none
// @updateURL https://gist.githubusercontent.com/cwelsys/89bf8dbb1eeea673614de41f55e707dd/raw/audiocontext-suspender.user.js
// @downloadURL https://gist.githubusercontent.com/cwelsys/89bf8dbb1eeea673614de41f55e707dd/raw/audiocontext-suspender.user.js
// ==/UserScript==
(() => {
if (localStorage.getItem('disableAudioContextSuspender'))
return
const originalAudioContext = AudioContext
const originalCreateBufferSource = AudioContext.prototype.createBufferSource
const map = new Map()
const autoSuspend = ctx => {
const timer = map.get(ctx)
if (timer) clearTimeout(timer)
map.set(ctx, setTimeout(function () {
map.delete(ctx)
if (ctx.state === 'suspended') return
ctx.suspend().then(_ => console.log('Auto-suspended AudioContext'))
}, 2000))
}
AudioContext.prototype.createBufferSource = function () {
const buffer = originalCreateBufferSource.call(this)
buffer._audioContext = this
buffer.start = function () {
autoSuspend(this._audioContext)
if (this._audioContext.state === 'running')
return AudioScheduledSourceNode.prototype.start.call(this)
this._audioContext.resume().then(_ => {
console.log('Resumed auto-suspended AudioContext')
return AudioScheduledSourceNode.prototype.start.call(this)
})
}
return buffer
}
AudioContext = function () {
const ctx = new originalAudioContext()
autoSuspend(ctx)
return ctx
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment