我可以从“预防违规”中保护自定义事件。

发布于 2025-02-03 06:21:17 字数 2508 浏览 4 评论 0原文

我们有一个模具Web组件,可渲染用户对话框。它由“外部组件”和“ InnerComponent”组成。 外部一个人关心处理浏览器(道具,cookie中的装载物等),而内部则呈现实际的HTML,并让用户对其进行操作。

(实际上,内部使用了更多组件,从一个UI组件(例如复选框,按钮等)的不同项目中使用。但是我认为这在这里不相关。)

当复选框,按钮等单击<内部组件>在组件中的一个单击处理程序被称为执行某些UI逻辑(例如设置“检查”属性),然后发出自定义事件,例如:

@Event() checkboxToggleModalEvent: EventEmitter<OptionType>;
...

<checkbox-comp fid="...">
  <input type="checkbox" checked={optionCheckbox.userSelection} onClick={this.handleCheckbox} />
  ...
</checkbox-comp>
...

private handleCheckbox(event: Event) {
    const checkboxElement: HTMLInputElement = event.target as HTMLInputElement;
    ...
    const selection: OptionType = { name: indexId, userSelection };

    this.checkboxToggleModalEvent.emit(selection);
}

现在,在&lt; outer--中组件&gt;此事件会被聆听,并且处理程序关心的是“技术”逻辑:

    @Listen("checkboxToggleModalEvent")
    checkboxToggleModalEventHandler(event) {
        LogService.log.debug(event);

        ... some technical logic
    }

在大多数情况下,这很好。现在,我们在一个站点上进行了集成,其中这些事件显然无法正确发射或以某种方式在中间丢失。 UI逻辑是正常执行的,但是OuterComponent中的处理程序永远不会被调用。

我能够从引起问题的集成库中找到代码(很抱歉粘贴整个功能!):

// From the imported library on customer website:
function(t, exports) {
    try {
        var e = new window.CustomEvent("test");
        if (e.preventDefault(),
        !0 !== e.defaultPrevented)
            throw new Error("Could not prevent default")
    } catch (t) {
        var n = function(t, e) {
            var n, r;
            return e = e || {
                bubbles: !1,
                cancelable: !1,
                detail: void 0
            },
            n = document.createEvent("CustomEvent"),
            n.initCustomEvent(t, e.bubbles, e.cancelable, e.detail),
            r = n.preventDefault,
            n.preventDefault = function() {
                r.call(this);
                try {
                    Object.defineProperty(this, "defaultPrevented", {
                        get: function() {
                            return !0
                        }
                    })
                } catch (t) {
                    this.defaultPrevented = !0
                }
            }
            ,
            n
        };
        n.prototype = window.Event.prototype,
        window.CustomEvent = n
    }
}

如果我删除此函数,则一切都按预期工作。

现在,我想知道我们是否可以以某种方式“保护”我们的事件免受这样的拦截,因为任何情况下的组件都应该真正起作用(这就是为什么我们选择这项技术)。

但是,我也非常感谢任何可能导致问题的提示。

多谢!!

We have a Stencil web component that renders a user dialog. It consists of an "outerComponent" and an "innerComponent".
The outer one cares about dealing with the browser (props, load stuff from cookies etc.) and the inner one renders the actual HTML and lets the user operate it.

(Actually there are more components used inside, from a different project for the UI components such as checkbox, button etc. But I don't think that's relevant here.)

When a checkbox, button etc. is clicked in <inner-component> an onclick-handler within the component is called that executes some UI logic (e.g. set the "checked" property) and then emits a custom event, e.g.:

@Event() checkboxToggleModalEvent: EventEmitter<OptionType>;
...

<checkbox-comp fid="...">
  <input type="checkbox" checked={optionCheckbox.userSelection} onClick={this.handleCheckbox} />
  ...
</checkbox-comp>
...

private handleCheckbox(event: Event) {
    const checkboxElement: HTMLInputElement = event.target as HTMLInputElement;
    ...
    const selection: OptionType = { name: indexId, userSelection };

    this.checkboxToggleModalEvent.emit(selection);
}

Now, in <outer-component> this event is listened for and the handler cares for the "technical" logic:

    @Listen("checkboxToggleModalEvent")
    checkboxToggleModalEventHandler(event) {
        LogService.log.debug(event);

        ... some technical logic
    }

This works fine in most cases. Now we have an integration on one site, where the events apparently do not get emitted correctly or somehow lost in the middle.
The UI logic is executed normally but the handler in outerComponent never gets called.

I was able to find the piece of code from an integrated library that causes the problem (sorry for pasting the whole function!):

// From the imported library on customer website:
function(t, exports) {
    try {
        var e = new window.CustomEvent("test");
        if (e.preventDefault(),
        !0 !== e.defaultPrevented)
            throw new Error("Could not prevent default")
    } catch (t) {
        var n = function(t, e) {
            var n, r;
            return e = e || {
                bubbles: !1,
                cancelable: !1,
                detail: void 0
            },
            n = document.createEvent("CustomEvent"),
            n.initCustomEvent(t, e.bubbles, e.cancelable, e.detail),
            r = n.preventDefault,
            n.preventDefault = function() {
                r.call(this);
                try {
                    Object.defineProperty(this, "defaultPrevented", {
                        get: function() {
                            return !0
                        }
                    })
                } catch (t) {
                    this.defaultPrevented = !0
                }
            }
            ,
            n
        };
        n.prototype = window.Event.prototype,
        window.CustomEvent = n
    }
}

If I remove this, everything works as expected.

Now, I'm wondering if we can somehow "protect" our events from being intercepted like this as the component should really work in any case (that's why we chose this technology).

But I also would be very grateful for any hints to what might actually cause the problem.

Thanks a lot!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷你仙 2025-02-10 06:21:17
  n.prototype = window.Event.prototype,
  window.CustomEvent = n

看起来他们 超载 CustomeVent并注入了自己的代码。

这是使用第三方软件的缺点。

在这种情况下,解决此问题的唯一方法是 及早进入 ,而过载customevent自己。

但是,您面临着制作 代码工作的挑战;因为他们这样做是有原因的。

什么是第三方软件?公开羞辱他们。

对于那些想尝试过载的人,请尽早执行:

window.customeelements.define =()=&gt; {}

  n.prototype = window.Event.prototype,
  window.CustomEvent = n

Looks like they overloaded CustomEvent and injected their own code.

This is the drawback of using 3rd party software.

In this case, only way to get around this is to get in early, and overload CustomEvent yourself.

But you then have the challenge of making their code work; because they did this overloading for a reason.

What is the 3rd party software? Publically shame them.

For those who want to try overloading, execute this early:

window.customeElements.define = () => {}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文