Skip to content

Commit

Permalink
use more stable detection for strict mode #16
Browse files Browse the repository at this point in the history
  • Loading branch information
abrahamjuliot committed Jun 11, 2021
1 parent 777f960 commit 4bbe489
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions modules/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,25 @@ function getBraveMode() {
strict: false
}
try {
// strict mode limits supported extensions
const canvas = document.createElement('canvas')
const gl = canvas.getContext('webgl')
const extensions = gl.getSupportedExtensions()
if (
!extensions || (
new Set(extensions).size == 1 && extensions[0] == 'WEBGL_debug_renderer_info')
) {
// strict mode adds float frequency data AnalyserNode
const strictMode = () => {
const audioContext = (
'OfflineAudioContext' in window ? OfflineAudioContext :
'webkitOfflineAudioContext' in window ? webkitOfflineAudioContext :
undefined
)
if (!audioContext) {
return false
}
const context = new audioContext(1, 1, 44100)
const analyser = context.createAnalyser()
const data = new Float32Array(analyser.frequencyBinCount)
analyser.getFloatFrequencyData(data)
const strict = new Set(data).size > 1 // native only has -Infinity
return strict
}

if (strictMode()) {
mode.strict = true
return mode
}
Expand Down

1 comment on commit 4bbe489

@abrahamjuliot
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Thorin-Oakenpants fyi

if (!gl.getSupportedExtensions()) no longer works as of Brave nightly.

This modified test, but I anticipate changes: https://github.com/brave/brave-browser/issues/15904

const extensions = gl.getSupportedExtensions()
if (!extensions || (new Set(extensions).size == 1 && extensions[0] == 'WEBGL_debug_renderer_info') {
  // strict
}

I'm now checking getFloatFrequencyData to detect strict mode.

const strictMode = () => {
	const audioContext = (
		'OfflineAudioContext' in window ? OfflineAudioContext : 
		'webkitOfflineAudioContext' in window ? webkitOfflineAudioContext :
		undefined
	)
	if (!audioContext) {
		return false
	}
	const context = new audioContext(1, 1, 44100)
	const analyser = context.createAnalyser()
	const data = new Float32Array(analyser.frequencyBinCount)
	analyser.getFloatFrequencyData(data)
	const strict = new Set(data).size > 1 // native only has -Infinity
	return strict
}

Please sign in to comment.