Skip to content

Commit 9413c18

Browse files
committed
YouTube JS Engine Tamer | Update
* Added `DO_createStampDomArrayFnF1_`
1 parent 1ecae01 commit 9413c18

File tree

1 file changed

+208
-58
lines changed

1 file changed

+208
-58
lines changed

473972.user.js

Lines changed: 208 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ==UserScript==
22
// @name YouTube JS Engine Tamer
33
// @namespace UserScripts
4-
// @version 0.18.2
4+
// @version 0.18.3
55
// @match https://www.youtube.com/*
66
// @match https://www.youtube-nocookie.com/embed/*
77
// @match https://studio.youtube.com/live_chat*
@@ -104,6 +104,7 @@
104104
const DO_createStampDomArrayFnE1_ = true; // added in 2025.02.11 - to improve stampDom responsiveness
105105
const DO_createStampDomArrayFnE1_noConstraintE = true;
106106
const DO_createStampDomArrayFnE1_nativeAppendD = true;
107+
const DO_createStampDomArrayFnF1_ = true;
107108

108109

109110
const FIX_POPUP_UNIQUE_ID = true; // currently only for channel about popup;
@@ -351,6 +352,7 @@
351352
const nativeRemoveE = HTMLElement_.prototype.remove;
352353
const DocumentFragment_ = DocumentFragment;
353354
const nativeAppendD = DocumentFragment_.prototype.append;
355+
const Node_ = Node;
354356

355357
/**
356358
@param {number} x
@@ -3601,14 +3603,104 @@
36013603
const container = this.getStampContainer_(b_);
36023604

36033605
const domShell = container.__domApi;
3604-
if (!domShell || !domShell.appendChild || domShell.firstElementChild) throw new Error();
3606+
if (!domShell || !domShell.appendChild) throw new Error();
3607+
const hasElement = !!domShell.firstElementChild;
36053608
const noscript = document.createElement('noscript');
36063609
// document.body.appendChild(noscript);
36073610
const nofn = () => true;
36083611
const n = a_.length;
36093612
const fns = new Array(n);
3613+
let cxt = 0;
36103614

36113615
let mutex = Promise.resolve(0);
3616+
let qxd = hasElement ? new WeakSet() : null;
3617+
const nextTickFnE1 = () => {
3618+
a_.some((u, i) => {
3619+
3620+
const x = csb(c, u);
3621+
if (!x) {
3622+
fns[i] = nofn;
3623+
cxt++;
3624+
} else {
3625+
3626+
const cp = this.createComponent_(c[x], u[x], d);
3627+
cp[stampIdxSb] = i;
3628+
doDeferRenderStamperBinding_(cp);
3629+
mutex = mutex.then(() => {
3630+
noscript.appendChild(cp);
3631+
});
3632+
3633+
// Promise.resolve(cp).then(doDeferRenderStamperBinding_);
3634+
let q = cp;
3635+
fns[i] = () => {
3636+
if (q && q.parentNode === noscript) {
3637+
const cp = q;
3638+
q = null;
3639+
}
3640+
if (!q) return true;
3641+
};
3642+
3643+
}
3644+
3645+
});
3646+
noscript.setAttribute('ylul8gr', `${Date.now()}`);
3647+
};
3648+
3649+
const nextTickFnF1 = ()=>{
3650+
3651+
a_.some((u, i) => {
3652+
3653+
const x = csb(c, u);
3654+
if (!x) {
3655+
fns[i] = nofn;
3656+
cxt++;
3657+
} else {
3658+
3659+
const is = this.getComponentName_(c[x], u[x]);
3660+
let np = domShell.firstElementChild;
3661+
let chosenNode = null;
3662+
while (np) {
3663+
if (np instanceof Node_ && np.is === is && !qxd.has(np)) {
3664+
qxd.add(np);
3665+
chosenNode = np;
3666+
break;
3667+
}
3668+
np = np.nextElementSibling;
3669+
}
3670+
3671+
const cp = chosenNode || this.createComponent_(c[x], u[x], d);
3672+
let oldIdx = cp[stampIdxSb];
3673+
if(chosenNode !== null && cp === chosenNode){
3674+
3675+
cp[stampIdxSb] = i;
3676+
doDeferRenderStamperBinding_(cp);
3677+
fns[i] = nofn;
3678+
}else{
3679+
3680+
cp[stampIdxSb] = i;
3681+
doDeferRenderStamperBinding_(cp);
3682+
mutex = mutex.then(() => {
3683+
noscript.appendChild(cp); // ui formation
3684+
});
3685+
3686+
// Promise.resolve(cp).then(doDeferRenderStamperBinding_);
3687+
let q = cp;
3688+
fns[i] = () => {
3689+
if (q && q.parentNode === noscript) {
3690+
const cp = q;
3691+
q = null;
3692+
}
3693+
if (!q) return true;
3694+
};
3695+
3696+
}
3697+
3698+
}
3699+
3700+
});
3701+
noscript.setAttribute('ylul8gr', `${Date.now()}`);
3702+
}
3703+
36123704
const pr = new Promise(resolvePR => {
36133705

36143706
let mo = new MutationObserver(() => {
@@ -3623,55 +3715,50 @@
36233715
resolvePR();
36243716
}
36253717
});
3626-
mo.observe(noscript, { subtree: false, childList: true });
3627-
nextBrowserTick_(() => {
3628-
a_.some((u, i) => {
3718+
mo.observe(noscript, { subtree: false, childList: true, attributes:['ylul8gr'] });
36293719

3630-
const x = csb(c, u);
3631-
if (!x) {
3632-
fns[i] = nofn;
3633-
} else {
3634-
3635-
const cp = this.createComponent_(c[x], u[x], d);
3636-
cp[stampIdxSb] = i;
3637-
doDeferRenderStamperBinding_(cp);
3638-
mutex = mutex.then(() => {
3639-
noscript.appendChild(cp);
3640-
});
3720+
if(hasElement){
3721+
nextBrowserTick_(nextTickFnF1);
3722+
}else{
3723+
nextBrowserTick_(nextTickFnE1);
36413724

3642-
// Promise.resolve(cp).then(doDeferRenderStamperBinding_);
3643-
let q = cp;
3644-
fns[i] = () => {
3645-
if (q && q.parentNode === noscript) {
3646-
const cp = q;
3647-
q = null;
3648-
}
3649-
if (!q) return true;
3650-
};
3651-
3652-
}
3725+
}
36533726

3654-
});
3655-
});
36563727

36573728
});
36583729
await pr.then();
36593730
fns.length = 0;
3731+
qxd = null;
36603732

3661-
noscript.remove();
3733+
// nativeRemoveE.call(noscript);
3734+
noscript.remove(); // trigger isAttached change
36623735
const hostElement = this.hostElement;
36633736

3737+
// console.log('em77', hasElement, noscript.childElementCount, domShell.childElementCount, cxt, a_.length);
3738+
// if(a_.length !== noscript.childElementCount+ domShell.childElementCount+ cxt){
3739+
// const pd = `${Math.floor(Math.random() * 314159265359 + 314159265359).toString(36)}`;
3740+
3741+
// console.log('em77b', pd, [...a_], [...noscript.childNodes], [...domShell.childNodes])
3742+
// setTimeout(()=>{
3743+
// console.log('em77c', pd, [...domShell.childNodes])
3744+
// }, 1000)
3745+
// }
3746+
36643747
// const sdc02 = this.__$$StampDomCounter$$__;
36653748

36663749

36673750
/** @type {DocumentFragment} */
36683751
const gFragment = document.createDocumentFragment();
36693752

3670-
mutex = mutex.then(() => {
3753+
mutex = mutex.then(async() => {
3754+
3755+
// await new Promise(r=>setTimeout(r, 1000));
3756+
36713757

3672-
if (domShell.firstElementChild || hostElement.isConnected === false || this.isAttached === false || !hostElement.contains(container) || container.__domApi !== domShell) {
3758+
if (hostElement.isConnected === false || this.isAttached === false || !hostElement.contains(container) || container.__domApi !== domShell) {
36733759
return;
36743760
}
3761+
// const t1 = performance.now();
36753762

36763763
if (DO_createStampDomArrayFnE1_nativeAppendD) {
36773764
let elm;
@@ -3684,39 +3771,74 @@
36843771
gFragment.appendChild(elm);
36853772
}
36863773
}
3687-
}).then(() => {
3774+
// const t2 = performance.now();
36883775

3689-
if (domShell.firstElementChild || hostElement.isConnected === false || this.isAttached === false || !hostElement.contains(container) || container.__domApi !== domShell) {
3776+
// console.log('createStampDomArrayFn_{T2}', t2-t1);
3777+
}).then(async () => {
3778+
3779+
if (hostElement.isConnected === false || this.isAttached === false || !hostElement.contains(container) || container.__domApi !== domShell) {
36903780
return;
36913781
}
36923782

3783+
// const t1 = performance.now();
36933784
// const arr = [...noscript.childNodes];
36943785
// g.append(...arr);
3695-
domShell.appendChild(gFragment);
3786+
3787+
// if(gFragment.childElementCount>10){
3788+
3789+
// for(const p of gFragment.childNodes){
3790+
3791+
// const w = document.createDocumentFragment();
3792+
// w.append(p);
3793+
3794+
// domShell.appendChild(w);
3795+
// await Promise.resolve(0);
3796+
3797+
// }
3798+
3799+
// }else{
3800+
3801+
domShell.appendChild(gFragment);
3802+
// }
3803+
3804+
3805+
36963806
// for (const cp of arr) {
36973807
// const i = cp[stampIdxSb];
36983808
// const u = a_[i];
36993809
// const c = c_;
37003810
// const x = csb(c, u);
37013811
// this.deferRenderStamperBinding_(cp, c[x], u[x]); // necessary?
37023812
// }
3813+
37033814
this.flushRenderStamperComponentBindings_();
37043815
this.markDirty && this.markDirty();
3705-
if (shouldTriggerRendererStamperFinished) {
3706-
const evt = new CustomEvent("yt-rendererstamper-finished", {
3707-
bubbles: !0,
3708-
cancelable: !1,
3709-
composed: !0,
3710-
detail: {
3711-
container
3712-
}
3713-
});
3714-
hostElement.dispatchEvent(evt);
3715-
}
3816+
// const t2 = performance.now();
3817+
3818+
// console.log('createStampDomArrayFn_{T3}', t2-t1); // time consuming
3819+
3820+
if (shouldTriggerRendererStamperFinished) {
3821+
nextBrowserTick_(() => {
3822+
const evt = new CustomEvent("yt-rendererstamper-finished", {
3823+
bubbles: !0,
3824+
cancelable: !1,
3825+
composed: !0,
3826+
detail: {
3827+
container
3828+
}
3829+
});
3830+
hostElement.dispatchEvent(evt);
3831+
})();
3832+
}
3833+
3834+
// this.flushRenderStamperComponentBindings_();
3835+
3836+
37163837

37173838

37183839
});
3719-
}
3840+
};
3841+
37203842

37213843
const stampIdxSb = Symbol();
37223844
const createStampDomArrayFn_ = (fn) => {
@@ -3726,7 +3848,8 @@
37263848
}
37273849
const gn = function (a, b, c, d, shouldTriggerRendererStamperFinished, h) {
37283850
// this.__$$StampDomCounter$$__ = (this.__$$StampDomCounter$$__ || 0) + 1;
3729-
const isNonEmptyArray = (a || 0).length >= 1
3851+
if (a.length === 1 && Object.keys(a[0]).length === 0) a.length = 0;
3852+
const isNonEmptyArray = (a || 0).length >= 1;
37303853
if (!isNonEmptyArray) {
37313854
return fn.call(this, undefined, b, undefined, d);
37323855
} else if (h === undefined && typeof b === 'string' && c && typeof c === 'object' && this.is && val_kevlar_should_maintain_stable_list) {
@@ -3751,17 +3874,44 @@
37513874

37523875
if (container) {
37533876

3754-
((container && container.__domApi) || fn.call(this, undefined, b, undefined, d));
3755-
3756-
const domShell = container && container.__domApi;
3757-
const fulfillment = domShell && domShell.appendChild && domShell.firstElementChild === null;
3758-
3759-
// if(a.length > 10) console.log('createStampDomArrayFnE1_',392, domHolder, b2, p)
3760-
if (fulfillment) {
3761-
createStampDomArrayFnE1_.call(this, a, b, c, d, shouldTriggerRendererStamperFinished, h);
3762-
return;
3877+
let domShell = (container && container.__domApi);
3878+
3879+
if (!domShell) {
3880+
fn.call(this, undefined, b, undefined, d);
3881+
domShell = container && container.__domApi;
37633882
}
3764-
3883+
3884+
3885+
if (domShell && domShell.appendChild) {
3886+
3887+
if (domShell.firstElementChild === null) {
3888+
createStampDomArrayFnE1_.call(this, a, b, c, d, shouldTriggerRendererStamperFinished, h);
3889+
return;
3890+
} else if (DO_createStampDomArrayFnF1_ && domShell.firstElementChild instanceof Node_) {
3891+
// let t1 = performance.now();
3892+
let error = 0;
3893+
const createDocumentFragment = document.createDocumentFragment;
3894+
document.createDocumentFragment = function () {
3895+
throw new Error("DN96IZkGLTY6");
3896+
}
3897+
let r;
3898+
try {
3899+
r = fn.call(this, a, b, c, d, shouldTriggerRendererStamperFinished, h);
3900+
} catch (e) {
3901+
error = (e.message === 'DN96IZkGLTY6') ? 1 : e;
3902+
}
3903+
document.createDocumentFragment = createDocumentFragment;
3904+
if (!error) return r;
3905+
if (error !== 1) throw e;
3906+
// let t2 = performance.now();
3907+
// console.log('createStampDomArrayFnF1_', t2-t1);
3908+
createStampDomArrayFnE1_.call(this, a, b, c, d, shouldTriggerRendererStamperFinished, h);
3909+
return;
3910+
}
3911+
3912+
}
3913+
3914+
37653915
}
37663916

37673917

0 commit comments

Comments
 (0)