deterministic-injection.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. 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 = 1;
  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. }() );