cyfung1031 / userscript-supports

This is for the userscripts created on GreasyFork.org.
https://cyfung1031.github.io/userscript-supports/
MIT License
48 stars 3 forks source link

Deferred Message makes the chat message disappeared #12

Closed cyfung1031 closed 1 year ago

cyfung1031 commented 1 year ago

Why?

https://greasyfork.org/scripts/469878-youtube-super-fast-chat/discussions/197267

img

Screen Shot 2023-08-17 at 4 29 09

'use strict';

(() => {

    function z(inputValue) {
        inputValue = yl(inputValue);
        if (inputValue === "false") {
            return false;
        }
        return !!(inputValue);
    }

    function lq(a) {
        kq(a, "WARNING")
    }

    const Ls = function (element = document) {
        if (element instanceof Js) {
            return element;
        }
        if (element instanceof Is) {
            return element;
        }

        let domApi = element.__domApi;

        if (!domApi) {
            domApi = element instanceof Event ? new Is(element) : new Js(element);
            element.__domApi = domApi;
        }

        return domApi;
    };

    const uE = new WeakMap;
    const vE = new WeakMap;
    function ZHa(objectA, objectB) {
        for (const property in objectA) {
            if (objectA.hasOwnProperty(property) && objectB[property]) {
                return property;
            }
        }
        return null;
    }
    function aIa(reuseComponentsFlag) {
        if (typeof reuseComponentsFlag === "undefined") {
            if (z("kevlar_tuner_should_test_reuse_components")) {
                return z("kevlar_tuner_should_reuse_components");
            }
            return ll("REUSE_COMPONENTS", false);
        }
        return reuseComponentsFlag || false;
    }
    function bIa(typeOfElement, childrenElement, reuseComponentsFlag) {
        reuseComponentsFlag && (childrenElement.is ? (vE.has(childrenElement) && (typeOfElement = vE.get(childrenElement),
        reuseComponentsFlag = typeOfElement.listener,
        delete childrenElement[typeOfElement.property],
        "element" === reuseComponentsFlag.type ? childrenElement.removeEventListener(reuseComponentsFlag.eventName, reuseComponentsFlag.handler) : "polymer" === reuseComponentsFlag.type && childrenElement.unlisten(childrenElement, reuseComponentsFlag.eventName, reuseComponentsFlag.methodName),
        vE.delete(childrenElement)),
        GC(childrenElement)) : childrenElement.__proto__ == HTMLElement.prototype && (childrenElement = new tm("Rendererstamper removed a non custom element","Stamper: " + typeOfElement + " element: " + (childrenElement && childrenElement.nodeName)),
        lq(childrenElement)))
    }
    function cIa(typeOfElement, domContainer, childrenElement, reuseComponentsFlag) {
        const node = domContainer.node ? domContainer.node : domContainer;
        if (Ls(childrenElement).parentNode == node) {
            domContainer.removeChild(childrenElement);
            bIa(typeOfElement, childrenElement, reuseComponentsFlag);
        }
        let timeoutId = uE.get(childrenElement);
        if (timeoutId) {
            uE.delete(childrenElement);
            xi.cancelJob(timeoutId);
        }
        childrenElement.hidden = false;
    }

    function dIa(typeOfElement, domContainer, index, reuseComponentsFlag) {
        if (domContainer && domContainer.children) {
            let childrenElement = domContainer.children[index];
            if (childrenElement) {
                if (ll("DEFERRED_DETACH")) {
                    while (childrenElement && !uE.has(childrenElement)) {
                        const e = typeOfElement;
                        const h = domContainer;
                        const l = childrenElement;
                        const m = reuseComponentsFlag;
                        l.hidden = true;
                        const timeoutId = Bm(0, cIa.bind(null, e, h, l, m), 0);
                        uE.set(l, timeoutId);
                        childrenElement = childrenElement.nextElementSibling;
                    }
                } else {
                    let element;
                    while (element = domContainer.lastElementChild) {
                        domContainer.removeChild(element);
                        bIa(typeOfElement, element, reuseComponentsFlag);
                        if (element === childrenElement) break;
                    }
                }
            }
        }
    }

    window.p301 = {
        Ls,
        aIa
    };

    window.q301 = {
        stampDomArray_: function (inputArray, containerId, pivotEntry, reuseComponentsFlag, fireEvent, maintainStableListFlag) {
            let container = this.getStampContainer_(containerId);

            if (container) {
                const domContainer = Ls(container);
                reuseComponentsFlag = aIa(reuseComponentsFlag);

                if (inputArray) {
                    let currentIndex = 0;
                    let arrayIndex = 0;
                    let currentNode;

                    let isMaintainStableListFlag = maintainStableListFlag === undefined ? z("kevlar_tuner_should_test_maintain_stable_list") ? z("kevlar_should_maintain_stable_list") : !!ll("STAMPER_STABLE_LIST", false) : maintainStableListFlag || false;

                    if (isMaintainStableListFlag) {
                        currentNode = domContainer.firstElementChild;

                        while (currentNode && !uE.has(currentNode) && inputArray.length > arrayIndex) {
                            const currentEntry = inputArray[arrayIndex];
                            const extractedData = ZHa(pivotEntry, currentEntry);

                            if (extractedData) {
                                const componentName = this.getComponentName_(pivotEntry[extractedData], currentEntry[extractedData]);
                                if (componentName !== currentNode.is) {
                                    const newNode = this.createComponent_(pivotEntry[extractedData], currentEntry[extractedData], reuseComponentsFlag);
                                    const nextSiblingNode = Ls(currentNode).nextElementSibling;

                                    if (nextSiblingNode) {
                                        domContainer.insertBefore(newNode, nextSiblingNode);
                                    } else {
                                        domContainer.appendChild(newNode);
                                    }
                                    cIa(this.is, domContainer, currentNode, reuseComponentsFlag);
                                    currentNode = newNode;
                                } else {
                                    this.telemetry_.reuse++;
                                }
                                this.deferRenderStamperBinding_(currentNode, pivotEntry[extractedData], currentEntry[extractedData]);
                                currentIndex++;
                                arrayIndex++;
                                currentNode = Ls(currentNode).nextElementSibling;
                            } else {
                                arrayIndex++;
                            }
                        }
                    }
                    dIa(this.is, domContainer, currentIndex, reuseComponentsFlag);
                    const subsequentNode = currentNode;

                    if (inputArray.length > arrayIndex) {
                        const fragment = document.createDocumentFragment();

                        for (; arrayIndex < inputArray.length; arrayIndex++) {
                            const currentValue = inputArray[arrayIndex];
                            const extractedValue = ZHa(pivotEntry, currentValue);
                            if (extractedValue) {
                                currentNode = this.createComponent_(pivotEntry[extractedValue], currentValue[extractedValue], reuseComponentsFlag);
                                this.deferRenderStamperBinding_(currentNode, pivotEntry[extractedValue], currentValue[extractedValue]);
                                fragment.appendChild(currentNode);
                            }
                        }

                        if (subsequentNode && Ls(subsequentNode).parentNode && currentNode && !uE.has(currentNode)) {
                            domContainer.insertBefore(fragment, subsequentNode);
                        } else {
                            domContainer.appendChild(fragment);
                        }
                    }
                    this.flushRenderStamperComponentBindings_();

                    if (this.markDirty) {
                        this.markDirty();
                    }
                    if (fireEvent) {
                        Au(this.hostElement, "yt-rendererstamper-finished", { container: container });
                    }
                } else {
                    dIa(this.is, domContainer, 0, reuseComponentsFlag);
                }
            } else {
                kq(new tm("Container object not found", containerId));
            }
        }
    };
})();
cyfung1031 commented 1 year ago

Confirmed that it is due to the deferred message occurrence conflicted with BetterStreamChat.

Because of that, I have submitted the PR and the chrome extension have been updated to v1.3.6 in 2023.08.19. To prevent problems in the future due to this deference, I have added a checking to ensure the message will not be modified due to deferred message.