i have a html page containing this :
<div id="ajaxloader" style="display: none;">
<div>
hello world
</div>
</div>
i can read its display value with document.getElementById('ajaxloader').style.display
i want to get notified whenever its display changed to block or none.
currently i am using a dumb solution using timer :
Timer{
running : true
repeat : true
interval: 500
onTriggered: {
var js = "document.getElementById('ajaxloader').style.display"
webView.runJavaScript(js,x=>{
if(x=="block"){
// we catch the change but its really bad solution
}
})
}
}
i am looking for a way to catch this kind of changes in DOM , there is something called Mutation Observer but i am not sure how to implement it into QML's WebEngineView.
all i need is a way to catch changes happend in the WebEngineView, or an event to catch CRUD's going on in the engine or anyway better than this timer!
UPDATE :
for example we have a webengine which goes to google.com and after load finished it changes search text to 'hello world' we want to catch that change without using timer , in real websites this changes actually happens with CRUD functions (ajax requests) or other ways:
WebChannel{
id:_channel
}
WebEngineView{
id:webEngine
height: parent.height
width: parent.width
webChannel: _channel
url : "www.google.com"
onNewViewRequested: {
request.openIn(webEngine)
}
objectName: "webView"
profile.httpCacheType: WebEngineProfile.NoCache
onLoadingChanged: {
if(loadRequest.status == WebEngineView.LoadSucceededStatus){
var js = "document.querySelector('input[role=combobox]').value = 'hello world'"
webEngine.runJavaScript(js,y=>{})
}
}
}
dont forget to initialize engine in c++ it wont work without this : QtWebEngine::initialize(); and other importing stuff plus you need to add this to pro file
QT += webengine webengine-private webenginecore webenginecore-private
now if i use timer method which i want to put it aside it should be like this:
Timer{
running : true
repeat : true
interval : 500
onTriggered:{
var js = "document.querySelector('input[role=combobox]').value"
webEngine.runJavaScript(js,y=>{console.log(y)});
// now i have to check y to see if its equals to hello world or what ever which is really bad idea to use a timer here
}
}
for example you can observe changes in input of google like this :
var targetNode = document.querySelector('input[role=combobox]')
targetNode.oninput = function(e){this.setAttribute('value',targetNode.value)}
var config = { attributes: true, childList: true, subtree: true };
var callback = function(mutationsList, observer) {
for(var mutation of mutationsList) {
if (mutation.type == 'childList') {
console.log('A child node has been added or removed.');
}
else if (mutation.type == 'attributes') {
console.log('The ' + mutation.attributeName + ' attribute was modified.');
}
}
};
// Create an observer instance linked to the callback function
var observer = new MutationObserver(callback);
// Start observing the target node for configured mutations
observer.observe(targetNode, config);