Added polyfill for apply and call

I really hope this works
This commit is contained in:
FlamedDogo99 2024-06-03 17:31:47 -06:00
parent 603e2971f7
commit a4a90072e2

View File

@ -30,25 +30,44 @@ window.sprintLock = false;
var previousTouchX = null; var previousTouchX = null;
var previousTouchY = null; var previousTouchY = null;
var startTouchX = null; var startTouchX = null;
// Polyfill for apply
Function.prototype.polyfillApply = function(currentContext = {}, arg = []) {
if (typeof this !== 'function') {
throw new Error(this + "it's not callable");
}
if (!Array.isArray(arg)) {
throw new TypeError('CreateListFromArrayLike called on non-object')
}
currentContext.fn = this;
currentContext.fn(...arg);
}
// Polyfill for call
Function.prototype.polyfillCall = function(currentContext = {}, ...arg) {
if (typeof this !== 'function') {
throw new Error(this + "it's not callable");
}
currentContext.fn = this;
currentContext.fn(...arg);
}
// Ignores any keydown event that doesn't have the isValid parameter. // Ignores any keydown event that doesn't have the isValid parameter.
const proto = EventTarget.prototype;
const _addEventListener = EventTarget.prototype.addEventListener; const _addEventListener = EventTarget.prototype.addEventListener;
Object.defineProperty(proto, "addEventListener", { Object.defineProperty(EventTarget.prototype, "addEventListener", {
value: function (type, fn, ...rest) { value: function (type, fn, ...rest) {
_addEventListener.call(this, type, function(...args) { _addEventListener.call(this, type, function(...args) {
if(type === 'keydown' && (!args[0].isValid)) { if(type === 'keydown' && (!args[0].isValid)) {
return; return;
} }
return fn.apply(this, args); return fn.polyfillApply(this, args);
}, ...rest); }, ...rest);
} }
}); });
//Allows keyboard to type in input
const _preventDefault = Event.prototype.preventDefault; const _preventDefault = Event.prototype.preventDefault;
// Allows typing in #hiddenInput
Object.defineProperty(Event.prototype, "preventDefault", { Object.defineProperty(Event.prototype, "preventDefault", {
value: function () { value: function () {
if(document.activeElement.id != "hiddenInput") { if(document.activeElement.id != "hiddenInput") {
_preventDefault.call(this); _preventDefault.polyfillCall(this);
} }
} }
}); });
@ -99,55 +118,62 @@ function setButtonVisibility(pointerLocked) {
// When requestpointerlock is called, this dispatches an event, saves the requested element to window.fakelock, and unhides the touch controls // When requestpointerlock is called, this dispatches an event, saves the requested element to window.fakelock, and unhides the touch controls
window.fakelock = null; window.fakelock = null;
Element.prototype.requestPointerLock = function() { Object.defineProperty(Element.prototype, "requestPointerLock", {
value: function() {
window.fakelock = this window.fakelock = this
document.dispatchEvent(new Event('pointerlockchange')); document.dispatchEvent(new Event('pointerlockchange'));
console.log("requested pointerlock")
setButtonVisibility(true); setButtonVisibility(true);
return true return true
} }
});
// Makes pointerLockElement return window.fakelock // Makes pointerLockElement return window.fakelock
Object.defineProperty(document, "pointerLockElement", { Object.defineProperty(Document.prototype, "pointerLockElement", {
get: function() { get: function() {
return window.fakelock; return window.fakelock;
} }
}); });
// When exitPointerLock is called, this dispatches an event, clears the // When exitPointerLock is called, this dispatches an event, clears the
document.exitPointerLock = function() { Object.defineProperty(Document.prototype, "exitPointerLock", {
value: function() {
window.fakelock = null window.fakelock = null
document.dispatchEvent(new Event('pointerlockchange')); document.dispatchEvent(new Event('pointerlockchange'));
setButtonVisibility(false); setButtonVisibility(false);
return true return true
} }
});
// FULLSCREEN // FULLSCREEN
window.fakefull = null; window.fakefull = null;
// Stops the client from crashing when fullscreen is requested // Stops the client from crashing when fullscreen is requested
Element.prototype.requestFullscreen = function() { Object.defineProperty(Element.prototype, "requestFullscreen", {
value: function() {
window.fakefull = this window.fakefull = this
document.dispatchEvent(new Event('fullscreenchange')); document.dispatchEvent(new Event('fullscreenchange'));
return true return true
} }
});
Object.defineProperty(document, "fullscreenElement", { Object.defineProperty(document, "fullscreenElement", {
get: function() { get: function() {
return window.fakefull; return window.fakefull;
} }
}); });
document.exitFullscreen = function() { Object.defineProperty(Document.prototype, "exitFullscreen", {
value: function() {
window.fakefull = null window.fakefull = null
document.dispatchEvent(new Event('fullscreenchange')); document.dispatchEvent(new Event('fullscreenchange'));
return true return true
} }
});
// FILE UPLOADING // FILE UPLOADING
// Safari doesn't recognize the element.click() used to display the file uploader as an action performed by the user, so it ignores it. // Safari doesn't recognize the element.click() used to display the file uploader as an action performed by the user, so it ignores it.
// This hijacks the element.createElement() function to add the file upload to the DOM, so the user can manually press the button again. // This hijacks the element.createElement() function to add the file upload to the DOM, so the user can manually press the button again.
var oldCreate = document.createElement; const _createElement = document.createElement;
document.createElement = function(type, ignore) { document.createElement = function(type, ignore) {
this.oldCreate = oldCreate; this._createElement = _createElement;
let element = this.oldCreate(type); let element = this._createElement(type);
if(type == "input" && !ignore) { if(type == "input" && !ignore) {
let newElement = document.querySelector('input'); let newElement = document.querySelector('input');
if(!newElement) { if(!newElement) {
@ -162,7 +188,7 @@ document.createElement = function(type, ignore) {
newElement.hidden = false; newElement.hidden = false;
return newElement; return newElement;
} }
return this.oldCreate(type); return this._createElement(type);
} }
// Lazy way to hide touch controls through CSS. // Lazy way to hide touch controls through CSS.