简体   繁体   English

承诺后发生火灾

[英]Fire event after Promise

I have a project that works like a webproxy. 我有一个像webproxy一样工作的项目。 Use it to make changes to other sites. 使用它来更改其他站点。 (This part is important, I can only inject javascript code, but I can not change the code of the site itself.) (这部分很重要,我只能注入javascript代码,但不能更改网站本身的代码。)

I'm trying to modify a website made in React and I can not do it. 我正在尝试修改React中创建的网站,但我做不到。 The site takes content through api and promises. 该网站通过api和promises获取内容。 So I have the problem: 所以我有问题:

If I use: 如果我使用:

window.addEventListener('DOMContentLoaded', function () {

  document.getElementsByClassName ('content-class')[0].innerHTML="New Content";
});

Content does not exist yet and does not work 内容尚不存在,无法正常工作

If I use: 如果我使用:

window.onload = function() {
 document.getElementsByClassName ('content-class')[0].innerHTML="New Content";
}

It works after some time the content appeared. 内容显示一段时间后,它可以工作。

I Tried 我试过了

var content = document.getElementsByClassName('content-class')[0];

content.addEventListener ('load', function() {

    document.getElementsByClassName('content-class')[0].innerHTML="New Content";

});

Unsuccessful too 也不成功

How could I solve this? 我该如何解决? Is there any way to trigger an event after all the promises are finished? 完成所有承诺后,有什么方法可以触发事件?

Or detect that the element was created by the promise and trigger an event. 或者检测该元素是由Promise创建的,并触发一个事件。

As far as I know there is no way to trace promises that occur within the React app 据我所知,没有办法跟踪React应用程序中发生的承诺

However, modern browsers do have the mutation observer api developer.mozilla.org/en-US/docs/Web/API/MutationObserver which an be used to detect changes that the react app makes to the DOM 但是,现代浏览器的确具有变异观察器api developer.mozilla.org/en-US/docs/Web/API/MutationObserver ,可用于检测react应用对DOM所做的更改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM