deterministic-injection.js 1.3 KB

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