deterministic-injection.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * @author munrocket / https://github.com/munrocket
  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. const now = function () {
  13. return frameId * 16;
  14. };
  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. let frameId = 0;
  21. window.chromeMaxFrameId = 1;
  22. window.chromeRenderStarted = false;
  23. window.chromeRenderFinished = false;
  24. const RAF = window.requestAnimationFrame;
  25. window.requestAnimationFrame = function ( cb ) {
  26. if ( ! chromeRenderStarted ) {
  27. setTimeout( function () {
  28. requestAnimationFrame( cb );
  29. }, 50 );
  30. } else {
  31. RAF( function () {
  32. if ( frameId ++ < chromeMaxFrameId ) {
  33. cb( now() );
  34. } else {
  35. chromeRenderFinished = true;
  36. }
  37. } );
  38. }
  39. };
  40. /* Semi-determitistic video */
  41. let play = HTMLVideoElement.prototype.play;
  42. HTMLVideoElement.prototype.play = async function () {
  43. play.call( this );
  44. this.addEventListener( 'timeupdate', () => this.pause() );
  45. function renew() {
  46. this.load();
  47. play.call( this );
  48. RAF( renew );
  49. }
  50. RAF( renew );
  51. };
  52. }() );