2
0

deterministic-injection.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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() { return frameId * 16; };
  13. window.Date.now = now;
  14. window.Date.prototype.getTime = now;
  15. window.performance.wow = performance.now;
  16. window.performance.now = now;
  17. /* Deterministic RAF */
  18. let frameId = 0;
  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. }() );