deterministic-injection.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. ( function () {
  2. /* Deterministic random */
  3. window.Math._random = window.Math.random;
  4. let seed = Math.PI / 4;
  5. window.Math.random = function () {
  6. const x = Math.sin( seed ++ ) * 10000;
  7. return x - Math.floor( x );
  8. };
  9. /* Deterministic timer */
  10. let frameId = 0;
  11. const now = () => frameId * 16;
  12. window.Date.now = now;
  13. window.Date.prototype.getTime = now;
  14. window.performance.wow = performance.now;
  15. window.performance.now = now;
  16. /* Deterministic RAF */
  17. window.chromeMaxFrameId = 2;
  18. window.chromeRenderStarted = false;
  19. window.chromeRenderFinished = false;
  20. const RAF = window.requestAnimationFrame;
  21. window.requestAnimationFrame = function ( cb ) {
  22. if ( ! chromeRenderStarted ) {
  23. setTimeout( function () {
  24. requestAnimationFrame( cb );
  25. }, 50 );
  26. } else {
  27. RAF( function () {
  28. if ( frameId ++ < chromeMaxFrameId ) {
  29. cb( now() );
  30. } else {
  31. chromeRenderFinished = true;
  32. }
  33. } );
  34. }
  35. };
  36. /* Semi-determitistic video */
  37. let play = HTMLVideoElement.prototype.play;
  38. HTMLVideoElement.prototype.play = async function () {
  39. play.call( this );
  40. this.addEventListener( 'timeupdate', () => this.pause() );
  41. function renew() {
  42. this.load();
  43. play.call( this );
  44. RAF( renew );
  45. }
  46. RAF( renew );
  47. };
  48. TESTING = true;
  49. }() );