pub(crate) fn normalized_canvas_noise(noise: f64) -> f64 {
if noise.is_finite() {
noise.clamp(0.0, 1.0)
} else {
0.0
}
}
pub(crate) fn canvas_noise_js(seed: u32, noise: f64) -> String {
format!(
r#"(function() {{
try {{
const baseSeed = {seed};
const noise = {noise};
function next(state) {{
return (((state >>> 0) * 1664525 + 1013904223) >>> 0);
}}
function noisyClone(source) {{
if (!source || source.width === 0 || source.height === 0) return source;
const clone = document.createElement('canvas');
clone.width = source.width;
clone.height = source.height;
const ctx = clone.getContext('2d');
if (!ctx) return source;
ctx.drawImage(source, 0, 0);
const imageData = ctx.getImageData(0, 0, clone.width, clone.height);
const data = imageData.data;
let state = (baseSeed ^ clone.width ^ (clone.height << 16)) >>> 0;
for (let i = 0; i < data.length; i += 4) {{
state = next(state);
const delta = (((state / 4294967296) * 2 - 1) * noise * 255);
data[i] = Math.max(0, Math.min(255, data[i] + delta));
}}
ctx.putImageData(imageData, 0, 0);
return clone;
}}
const origToDataURL = HTMLCanvasElement.prototype.toDataURL;
HTMLCanvasElement.prototype.toDataURL = __seal(function(type, quality) {{
try {{
const clone = noisyClone(this);
return origToDataURL.call(clone, type, quality);
}} catch (_) {{
return origToDataURL.call(this, type, quality);
}}
}}, 'toDataURL');
if (HTMLCanvasElement.prototype.toBlob) {{
const origToBlob = HTMLCanvasElement.prototype.toBlob;
HTMLCanvasElement.prototype.toBlob = __seal(function(callback, type, quality) {{
try {{
const clone = noisyClone(this);
return origToBlob.call(clone, callback, type, quality);
}} catch (_) {{
return origToBlob.call(this, callback, type, quality);
}}
}}, 'toBlob');
}}
}} catch (_) {{}}
}})()"#
)
}
pub(crate) fn audio_noise_js(seed: u32) -> String {
format!(
r#"(function() {{
try {{
const baseSeed = {seed};
function patchAudioContext(Ctor) {{
if (!Ctor || !Ctor.prototype || !Ctor.prototype.createAnalyser) return;
const origCreateAnalyser = Ctor.prototype.createAnalyser;
Ctor.prototype.createAnalyser = __seal(function() {{
const analyser = origCreateAnalyser.call(this);
if (!analyser || !analyser.getFloatFrequencyData) return analyser;
const origGetFloatFrequencyData = analyser.getFloatFrequencyData;
analyser.getFloatFrequencyData = __seal(function(array) {{
origGetFloatFrequencyData.call(this, array);
let state = baseSeed >>> 0;
for (let i = 0; i < array.length; i++) {{
state = (((state >>> 0) * 1664525 + 1013904223) >>> 0);
array[i] += ((state / 4294967296) * 2 - 1) * 0.001;
}}
}}, 'getFloatFrequencyData');
return analyser;
}}, 'createAnalyser');
}}
patchAudioContext(window.AudioContext);
patchAudioContext(window.webkitAudioContext);
}} catch (_) {{}}
}})()"#
)
}
pub(crate) fn font_noise_js(seed: u32) -> String {
format!(
r#"(function() {{
try {{
const baseSeed = {seed};
if (document.fonts && document.fonts.constructor && document.fonts.constructor.prototype) {{
const proto = document.fonts.constructor.prototype;
if (proto.forEach) {{
const origForEach = proto.forEach;
proto.forEach = __seal(function(callback, thisArg) {{
let count = 0;
return origForEach.call(this, function(font, key, parent) {{
const state = (((baseSeed + count) >>> 0) * 2654435761) >>> 0;
count++;
if ((state % 10) !== 0) {{
callback.call(thisArg, font, key, parent);
}}
}});
}}, 'forEach');
}}
}}
}} catch (_) {{}}
}})()"#
)
}
pub(crate) fn performance_noise_js(seed: u32) -> String {
format!(
r#"(function() {{
try {{
let state = {seed};
const nextNoise = function() {{
state = (((state >>> 0) * 1664525 + 1013904223) >>> 0);
return (state / 4294967296) * 0.05;
}};
const target = typeof Performance !== 'undefined' && Performance.prototype
? Performance.prototype
: performance;
const origNow = target.now;
target.now = __seal(function() {{
const real = origNow.call(this);
return Math.round(real * 10) / 10 + nextNoise();
}}, 'now');
}} catch (_) {{}}
}})()"#
)
}
pub(crate) fn hardware_concurrency_js(cores: u8) -> String {
format!(
r#"(function() {{
try {{
Object.defineProperty(Navigator.prototype, 'hardwareConcurrency', {{
get: __seal(() => {cores}, 'get hardwareConcurrency'),
configurable: true,
}});
}} catch (_) {{}}
}})()"#
)
}
pub(crate) fn device_memory_js(mem: u8) -> String {
format!(
r#"(function() {{
try {{
Object.defineProperty(Navigator.prototype, 'deviceMemory', {{
get: __seal(() => {mem}, 'get deviceMemory'),
configurable: true,
}});
}} catch (_) {{}}
}})()"#
)
}
pub(crate) fn webgl_shape_js() -> String {
r#"(function() {
try {
function patch(proto) {
if (!proto) return;
if (proto.getShaderPrecisionFormat) {
const origPrecision = proto.getShaderPrecisionFormat;
proto.getShaderPrecisionFormat = __seal(function(shaderType, precisionType) {
const result = origPrecision.call(this, shaderType, precisionType);
if (result) {
Object.defineProperty(result, 'precision', { value: 23, configurable: true });
Object.defineProperty(result, 'rangeMin', { value: 127, configurable: true });
Object.defineProperty(result, 'rangeMax', { value: 127, configurable: true });
}
return result;
}, 'getShaderPrecisionFormat');
}
if (proto.getSupportedExtensions) {
const origExtensions = proto.getSupportedExtensions;
proto.getSupportedExtensions = __seal(function() {
const list = origExtensions.call(this) || [];
if (!list.includes('WEBGL_debug_renderer_info')) {
list.push('WEBGL_debug_renderer_info');
}
return list;
}, 'getSupportedExtensions');
}
}
if (typeof WebGLRenderingContext !== 'undefined') patch(WebGLRenderingContext.prototype);
if (typeof WebGL2RenderingContext !== 'undefined') patch(WebGL2RenderingContext.prototype);
} catch (_) {}
})()"#
.to_string()
}