Greasy Fork

Greasy Fork is available in English.

YouTube Native - Vanilla Engine

(YouTube Experimental) Disable the YouTube engine hacks and just use the native APIs

当前为 2023-07-17 提交的版本,查看 最新版本

/*

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.0
// @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__) => {

  const { requestAnimationFrame, cancelAnimationFrame, animate, getComputedStyle, defineProperty, window,
    setInterval, clearInterval, Promise, CDATASection, ProcessingInstruction, FocusEvent, setTimeout, clearTimeout } = __CONTEXT__;

  const ENABLE_NATIVE_CONSTRUCTOR_CHECK = false;
  let cids = {};
  function cleanCId(k) {
    Promise.resolve().then(() => clearInterval(cids[k]));
  }
  // let shadyNativeMap = {}
  // const getSymbol = Symbol('get')
  // function generator(p) {
  //   return function () {
  //     const v = shadyNativeMap[p].get(this);
  //     if (typeof v === 'function' && (getSymbol in v)) {
  //       // console.log(55,v.call(this))
  //       return v.call(this);
  //     }
  //     return v;
  //   };

  // }
  // const generators = {};
  // function defineTo(m, p) {
  //   let f = generators[p];
  //   if (!f) f = generators[p] = generator(p);

  //   defineProperty.call(Object, m, p, {
  //     get() {
  //       return f.call(this);
  //     },
  //     set() { },
  //     // writable: true,
  //     configurable: true,
  //     enumerable: true
  //   })


  // }
  Object.defineProperty = function (o, p, opts) {
    if (o === window && p === 'getComputedStyle') {
      //cleanCId('animate');
      return;
    }

    if (o instanceof Window) {
      const value = opts.value;
      if (value) {
        opts.writable = true;
        opts.configurable = true;
        opts.enumerable = true;
      }
      if (p === 'customElements' || p === 'Polymer') {
        if (p in o) return; // duplicate declaration?
      }
      console.log(923, 'window[p]=', p, opts);
      return defineProperty.apply(this, arguments);
    }


    const nativeConstructorCheck = ENABLE_NATIVE_CONSTRUCTOR_CHECK ? (o.constructor + "").indexOf('native code') > 0 : true;

    if (p.startsWith('__shady_')) {

      // let constructor = o instanceof Node ? Node : o instanceof DocumentFragment ? DocumentFragment : o instanceof Document ? Document : null;
      // if (constructor && opts && (typeof opts.get === 'function' || opts.value)) {

      //   if (!shadyNativeMap[p]) shadyNativeMap[p] = new WeakMap();
      //   const { get, value } = opts;
      //   if (get) get[getSymbol] = true;
      //   shadyNativeMap[p].set(o, get || value);

      //   /*
      //   if(!(p in o) && !(p in constructor.prototype)){

      //   console.log(321, p in o)
      //   console.log(322, p in o)

      //     return;
      //   }
      //   */

      //   if (!get) {

      //     o[p] = value;
      //     return;

      //   }
      //   /*
      //     defineTo(o, p);
      //   if(p==='__shady_native_querySelectorAll'){

      //     console.log(o[p])
      //   console.log(921,o , p, opts)
      //   }
      //   return;
      //   */

      // }

      const { get, value } = opts;
      if (!get) {
        o[p] = value;
        return;
      }


      if (p === '__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 (!shadyNativeMap[p]) shadyNativeMap[p] = new WeakMap();
        // const { get, value } = opts;
        // if (get) get[getSymbol] = true;
        // shadyNativeMap[p].set(o, get || value);

        if (!(p in o.constructor.prototype) && !(p in o)) {

          defineProperty.call(this, o.constructor.prototype, p, opts);

          if (p in o) {
            return;
          }

        }
        return;


        //defineTo(constructor, p);

      }
      // Event -> __shady_native_eventPhase
      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;


    }


    /*
    if (nativeConstructorCheck && o.constructor !== Object) {

      console.log(o, p, opts)
    }
    */
    return defineProperty.apply(this, arguments);
  }
  const asserter = (f) => Promise.resolve().then(() => console.assert(f(), f + ""));
  cids.animate = setInterval(() => {
    if (!cids.finish && ('Polymer' in window)) cids.finish = setTimeout(() => {
      cleanCId('animate');
      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);
    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
  }, 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();

  }

})({
  requestAnimationFrame, cancelAnimationFrame, animate: Element.prototype.animate, getComputedStyle: window.getComputedStyle, defineProperty: Object.defineProperty,
  window, setInterval, clearInterval, Promise, CDATASection, ProcessingInstruction, FocusEvent, setTimeout, clearTimeout
});