deterministic-injection.js 1.3 KB

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