Javascript 如何将变异观察者添加到WebView2控件

Javascript 如何将变异观察者添加到WebView2控件,javascript,c#,winforms,webview2,Javascript,C#,Winforms,Webview2,我正在构建一个使用WebView2控件的WinForms应用程序 有没有一种方法可以在ajax调用后注入一些JavaScript(可能带有变异观察者)来获取div的内容 我认为这可以通过在WebVew中使用ScriptNotify来实现 我的问题是我可以注入一个脚本,但是在JavaScript完成之前,控件会返回到WinForms C# JavaScript // JavaScript source code var results = []; let target = document.que

我正在构建一个使用WebView2控件的WinForms应用程序 有没有一种方法可以在ajax调用后注入一些JavaScript(可能带有变异观察者)来获取div的内容

我认为这可以通过在WebVew中使用ScriptNotify来实现

我的问题是我可以注入一个脚本,但是在JavaScript完成之前,控件会返回到WinForms

C#

JavaScript

// JavaScript source code
var results = [];
let target = document.querySelector('.getblock')
let config = { childList: true }
let observer = new MutationObserver(function (mutationList, observer) {
    console.log("CONTENT CHANGED!")
    console.log(mutationList)
    console.log(observer)
    for (const mutation of mutationList) {
        if (mutation.type === 'childList') {
            console.log('A child node has been added or removed.');
            results.push(mutation);
        }
        else if (mutation.type === 'attributes') {
            console.log('The ' + mutation.attributeName + ' attribute was modified.');
        }
    }
    //json = JSON.stringify(results);
    //returnFunc(json);

    GetLinks();
    
})

observer.observe(target, config)

var el = document.querySelectorAll('.getblock span');
//var el = document.querySelectorAll('button');
var i;
alert("el is " + el.length + " Long");
for (i = 0; i < el.length; i++) {
    // alert("Click "+ i);
    el[i].click();
}

function GetLinks() {
    alert("Hello from GetLinks ")
    var el = document.querySelectorAll('.getblock  a');
    alert("Hello from GetLinks " + el.length);
    var i;
    var json;
    for (i = 0; i < el.length; i++) {
        results.push(el[i].href);
        json = JSON.stringify(results);
    }
    returnFunc(json);
}

function returnFunc(json) {
    var ele = document.getElementById('res');
    if (ele !== null) {
        ele.innerHTML = json
    } else {
        var div = document.createElement('div');
        div.id = 'res';
        div.innerHTML = json
        document.body.appendChild(div);
    }
}
//JavaScript源代码
var结果=[];
让target=document.querySelector('.getblock')
let config={childList:true}
let observer=新的MutationObserver(函数(mutationList,observer){
log(“内容已更改!”)
console.log(mutationList)
console.log(观察者)
for(突变列表的常数突变){
if(mutation.type==='childList'){
log('已添加或删除子节点');
结果:push(突变);
}
else if(mutation.type==='attributes'){
log('修改了'+mutation.attributeName+'属性');
}
}
//json=json.stringify(结果);
//returnFunc(json);
GetLinks();
})
observer.observe(目标,配置)
var el=document.queryselectoral('.getblock span');
//var el=document.queryselectoral('button');
var i;
警报(“el为”+el.length+“Long”);
对于(i=0;i
对ExecuteScriptAsync调用调用wait将等待同步全局脚本执行或引发未处理的异常。因此,ExecuteScriptAsync不会等待来自setTimeout、promises或MutationObserver的回调等任何异步JavaScript代码。或者,如果存在任何未处理的异常,它将停止执行注入的脚本

如果需要了解异步JavaScript代码的完成情况,可以使用和
chrome.webview.postMessage
在脚本完成后将消息从脚本发送到本机代码


如果您希望在发生未处理异常时获得详细的错误信息,可以将插入的代码包装在try/catch块中,并检查任何未处理的异常。

您是说用chrome.webview.postMessage发送/插入JavaScript代码,完成后将触发CoreWebView2.WebMessageReceived?除了基础课程外,还有关于如何做的教程或示例吗?是的,没错。在本机代码中,订阅WebMessageReceived事件,然后注入脚本。脚本在任何时候都可以异步或以其他方式调用
chrome.webview.postMessage({completed:true}),您将在WebMessageReceived中以JSON的形式接收该对象。WebVIEW2SAMP应用程序有示例代码,我一直在查看示例代码,它看起来像是本地代码不是C++,也许是C++?使用它作为源代码,你能告诉我它是如何在C#中完成的吗?或者这需要一个新线程吗?没有其他示例或示例。如果你愿意,你可以打开一个新的线程。
// JavaScript source code
var results = [];
let target = document.querySelector('.getblock')
let config = { childList: true }
let observer = new MutationObserver(function (mutationList, observer) {
    console.log("CONTENT CHANGED!")
    console.log(mutationList)
    console.log(observer)
    for (const mutation of mutationList) {
        if (mutation.type === 'childList') {
            console.log('A child node has been added or removed.');
            results.push(mutation);
        }
        else if (mutation.type === 'attributes') {
            console.log('The ' + mutation.attributeName + ' attribute was modified.');
        }
    }
    //json = JSON.stringify(results);
    //returnFunc(json);

    GetLinks();
    
})

observer.observe(target, config)

var el = document.querySelectorAll('.getblock span');
//var el = document.querySelectorAll('button');
var i;
alert("el is " + el.length + " Long");
for (i = 0; i < el.length; i++) {
    // alert("Click "+ i);
    el[i].click();
}

function GetLinks() {
    alert("Hello from GetLinks ")
    var el = document.querySelectorAll('.getblock  a');
    alert("Hello from GetLinks " + el.length);
    var i;
    var json;
    for (i = 0; i < el.length; i++) {
        results.push(el[i].href);
        json = JSON.stringify(results);
    }
    returnFunc(json);
}

function returnFunc(json) {
    var ele = document.getElementById('res');
    if (ele !== null) {
        ele.innerHTML = json
    } else {
        var div = document.createElement('div');
        div.id = 'res';
        div.innerHTML = json
        document.body.appendChild(div);
    }
}