/*
MIT License
Copyright 2023 CY Fung
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// ==UserScript==
// @name YouTube Native - Vanilla Engine
// @namespace UserScript
// @match https://www.youtube.com/*
// @grant none
// @version 0.1.2
// @license MIT License
// @author CY Fung
// @run-at document-start
// @unwrap
// @inject-into page
// @allFrames true
// @description (YouTube Experimental) Disable the YouTube engine hacks and just use the native APIs
// ==/UserScript==
((__CONTEXT__) => {
let { frames, requestAnimationFrame, cancelAnimationFrame, animate, getComputedStyle, defineProperty, window,
setInterval, clearInterval, Promise, CDATASection, ProcessingInstruction, FocusEvent, setTimeout, clearTimeout } = __CONTEXT__;
Promise = ((async () => { })()).constructor;
const ENABLE_NATIVE_CONSTRUCTOR_CHECK = false;
let cids = {};
function cleanCId(k) {
Promise.resolve().then(() => clearInterval(cids[k]));
}
let framesFound = false;
let frameAdd = false;
let iframeLoaded = false;
function iframeOnLoad() {
iframeLoaded = true;
}
function tryFindFrames() {
if (!frameAdd && document.documentElement) {
frameAdd = true;
let iframe = document.createElement('iframe');
iframe.id = 'zihrS';
iframe.name = 'zihrS'
iframe.sandbox = 'allow-scripts allow-same-origin';
Object.assign(iframe.style, {
position: 'fixed',
height: '1px',
width: '1px',
top: '-300vh',
left: '-300vh',
pointerEvents: 'none',
opacity: '0',
contain: 'strict'
})
iframe.onload = iframeOnLoad;
document.documentElement.appendChild(iframe);
}
if (framesFound) return;
const n = frames.length;
if (!n) return;
for (let i = 0; i < n; i++) {
const frame = frames[i];
if (frame === window) continue;
if (frame.name !== 'zihrS') continue;
if (frame.closed === false) {
let accessible = false;
try { accessible = !!frame.location.href } catch (e) { }
if (accessible) {
framesFound = true;
console.groupCollapsed("YouTube Native - Vanilla Engine: Vanilla Functions");
console.log(621, frames.length)
try {
console.log(622, frame.requestAnimationFrame, requestAnimationFrame, frame.requestAnimationFrame === requestAnimationFrame,)
if (typeof frame.requestAnimationFrame === 'function') {
requestAnimationFrame = frame.requestAnimationFrame;
cancelAnimationFrame = frame.cancelAnimationFrame;
animate = frame.Element.prototype.animate;
getComputedStyle = frame.getComputedStyle;
setInterval = frame.setInterval;
clearInterval = frame.clearInterval;
setTimeout = frame.setTimeout;
clearTimeout = frame.clearTimeout;
console.log(712, requestAnimationFrame)
Promise.resolve().then(setVJS)
}
} catch (e) { }
try {
document.getElementById('zihrS').remove();
} catch (e) { }
console.groupEnd();
return;
}
}
}
}
Object.defineProperty = function (o, p, opts) {
if (arguments.length !== 3) return defineProperty.apply(this, arguments);
if (o instanceof Window) {
if (p === 'getComputedStyle') return;
if (p === 'Promise' && (p in o)) return; // WaterFox Classic
if (p === 'customElements' || p === 'Polymer') {
if (p in o) return; // duplicate declaration?
}
const value = opts.value;
if (value) {
opts.writable = true;
opts.configurable = true;
opts.enumerable = true;
}
if (p === 'ytInitialPlayerResponse' || p === 'playerResponse') {
// Firefox Chatroom? TBC
} else {
console.log(923, 'window[p]=', p, opts);
}
return defineProperty.call(this, o, p, opts);
}
const nativeConstructorCheck = ENABLE_NATIVE_CONSTRUCTOR_CHECK ? (o.constructor + "").indexOf('native code') > 0 : true;
if (p.startsWith('__shady_')) {
const { get, value } = opts;
if (!get) {
o[p] = value;
return;
}
if (p === '__shady_native_eventPhase') {
// Event -> __shady_native_eventPhase
return defineProperty.call(this, o, p, opts);
}
let constructor = o instanceof Node ? Node : o instanceof DocumentFragment ? DocumentFragment : o instanceof Document ? Document : null;
if (!constructor) {
let constructorName = (o.constructor || 0).name;
if (constructorName === 'Node') {
constructor = Node;
}
}
if (constructor && opts && (typeof opts.get === 'function')) {
if (!(p in o.constructor.prototype) && !(p in o)) {
defineProperty.call(this, o.constructor.prototype, p, opts);
}
return;
}
console.log(926, o, p, opts, !!constructor, !!opts, !!(typeof opts.get === 'function'))
// return;
}
if ((p in o) && nativeConstructorCheck) {
if (o instanceof Text) return;
if (o instanceof Comment) return;
if (CDATASection && o instanceof CDATASection) return;
if (ProcessingInstruction && o instanceof ProcessingInstruction) return;
if (o instanceof Event) return;
if (FocusEvent && o instanceof FocusEvent) return;
}
return defineProperty.call(this, o, p, opts);
}
const asserter = (f) => Promise.resolve().then(() => console.assert(f(), f + ""));
const setVJS = () => {
if (!framesFound) tryFindFrames();
if (window.Promise !== Promise) window.Promise = Promise;
if (window.getComputedStyle !== getComputedStyle) window.getComputedStyle = getComputedStyle;
if (Element.prototype.animate !== animate) Element.prototype.animate = animate;
if (window.requestAnimationFrame !== requestAnimationFrame) window.requestAnimationFrame = requestAnimationFrame
if (window.cancelAnimationFrame !== cancelAnimationFrame) window.cancelAnimationFrame = cancelAnimationFrame
};
cids.timeVJS = setInterval(() => {
if (!cids.finish && ('Polymer' in window) && iframeLoaded) cids.finish = setTimeout(() => {
setVJS();
try {
document.getElementById('zihrS').remove();
} catch (e) { }
cleanCId('timeVJS');
if (document.isConnected === false) return;
setTimeout(() => {
if (document.isConnected === false) return;
asserter(() => window.Promise === Promise);
asserter(() => window.getComputedStyle === getComputedStyle);
asserter(() => Element.prototype.animate === animate);
asserter(() => window.requestAnimationFrame === requestAnimationFrame);
asserter(() => window.cancelAnimationFrame === cancelAnimationFrame);
}, 800);
}, 40);
setVJS();
}, 1);
let isInnerFrame = false;
try {
isInnerFrame = window !== top && window.document.domain === top.document.domain;
} catch (e) { }
if (!isInnerFrame) {
console.groupCollapsed(
"%cYouTube Native - Vanilla Engine (Experimental)",
"background-color: #e0005a ; color: #ffffff ; font-weight: bold ; padding: 4px ;"
);
console.log("Script is loaded.");
console.log("This is an experimental script.");
console.log("If you found any issue in using YouTube, please disable this script to check whether the issue is due to this script or not.");
console.groupEnd();
}
})({
frames,
requestAnimationFrame, cancelAnimationFrame, animate: Element.prototype.animate, getComputedStyle: window.getComputedStyle, defineProperty: Object.defineProperty,
window, setInterval, clearInterval, Promise, CDATASection, ProcessingInstruction, FocusEvent, setTimeout, clearTimeout
});