如何在Windows UWP中在同一个WebView中打开弹出窗口(而不是新窗口)?

17

我的UWP程序中有一个WebView,它工作得很好,除了我点击一个通常会在新窗口(弹出窗口)中打开的按钮时。

当我点击通常会在新窗口中打开的按钮时,我只想让它在同一个WebView中打开,但它却在我的默认浏览器中打开。有没有设置可以解决这个问题?

更新:

我已经回答了我的原始问题。我添加了一个NewWindowRequested事件处理程序,打开了

args.uri

在同一个 Webview 中然后说道

e.handled=true

我有一个新问题,可能只出现在jeopardy.com上。我正在尝试制作一个“沃森”,用于谷歌问题,但是当我打开练习测试时,它从未开始。

所以基本上在我的WebView中,我转到https://www.jeopardy.com/be-a-contestant/practice-tests并单击“进行练习测试”。当它打开新窗口时,定时器从未开始。我想他们正在试图阻止人们在他们的问题上使用程序。有没有人知道解决办法?

此外,这对我来说在Edge中很有效。

enter image description here

相对于WebView,它看起来像(右侧)

enter image description here

你认为它可能与javascript函数或窗口大小有关吗?

更新2:

对于任何想要帮助的人,这是一个GitHub存储库:

https://github.com/SethKitchen/JeopardyWinner


桌面版Edge浏览器也无法启动此计时器,因此这可能是Edge引擎的问题?UWP中的WebView也在使用它。 - Andrei Ashikhmin
@Syn-McJ 对我来说在Edge上运行良好。 - Seth Kitchen
我在Webview控件和Edge中看到了相同的图片,所以我敢打赌问题就在那里,但可能并不总是出现。 - Andrei Ashikhmin
@SethKitchen 请问您能否展示更多用于解决原始问题的代码(强制在同一WebView窗口中打开新链接)? - Gail Foad
2个回答

1
我看了一下你的问题。实际上,你需要在第一页创建一些数据并初始化第二页。 如果你直接使用标准浏览器访问第二页(https://www.jeopardy.com/be-a-contestant/practice-test-client),你会得到与UWP应用程序相同的行为。
如果你启动浏览器的调试工具(F12),你会看到主要的脚本是:

所以,我已经通过在第二个页面使用调试器控制台注入一些内容来启动计时器(我已经在Firefox中测试过)。以下是执行步骤(将整个内容复制/粘贴到控制台中并执行):

var s = document.createElement("script");s.type = "text/javascript";s.src = "https://www.jeopardy.com/Assets/jeopardy/easyxdm/exdm_helper.js";$("head").append(s); 

s.src = "https://www.jeopardy.com/Assets/jeopardy/js/main.js";$("head").append(s);

s.src = "https://www.jeopardy.com/Assets/jeopardy/js/practice_test_launcher.js";$("head").append(s);

backendUserCheck(); test_launcher.init();test_launcher.testWindow = window;

$('body').append('<div data - testfile = "/Assets/jeopardy/json/test_practice_2011.json" data - testid = "1" class="button launch_practice_test" data-testtype="adult">Take the practice test</div>');

var button = $('.launch_practice_test');test_launcher.testQuestionsFile = button.attr('data-testfile');test_launcher.testType = button.attr('data-testtype');test_launcher.testTitle = button.attr('data-testtitle');test_launcher.testID = button.attr('data-testid');

testModule.parentRef=test_launcher;

testModule.deleteLocalStorage();
testModule.checkLocalStorage();
testModule.getLocalStorage();
testModule.testID = testModule.parentRef.getTestID();
testModule.testType = testModule.parentRef.getTestType();
testModule.testFile = testModule.parentRef.getFile();
testModule.hardwareTest = testModule.parentRef.checkIfDryRun();

testModule.sendTestStart();

trace = function(){};

trace = {
    output  : true,
    queue   : [],

    push    : function(data)
    {
        trace.queue.push(data);
        if(trace.output)
        {
            trace.post();
        }
        return
    },

    post    : function()
    {
        $.each(trace.queue, function(i,arr)
        {
            trace.log(arr);
        });
        trace.queue = [];
        return
    },

    dump    : function()
    {
        trace.post();
        return
    },

    log     : function(data)
    {
        if (!window.console){ console = {log: function() {}} };
        console.log(data);
        return
    }
};

testModule.loadQuiz();
testModule.parentRef=test_launcher;
testModule.startDate = testModule.parentRef.getStartDateInMS();

这段初始化代码是从之前提到的两个主要脚本的onload事件或init方法中提取的。计时器会被执行,但问题随后不会出现。我认为需要来自practice_test_client.js中的alertParentsetTestData代码。我让你调试网页并找到必要的方法执行。
要在您的UWP中使其工作,您需要向您的Web视图注入这些数据。为此,您可以尝试使用InvokeScriptAsync方法,或使用HttpClient下载页面,并在将结果页面加载到webView之前将初始化命令添加到其中。
string content = "";
using (HttpClient client = new HttpClient())
{
    content = await client.GetStringAsync(new Uri("https://www.jeopardy.com/be-a-contestant/practice-test-client"));
}

谢谢,这看起来很有帮助。如果您找到“必要的执行方法”,请编辑此帖子,我会将其标记为解决方案。 - Seth Kitchen
我已更新我的回复,并提供了加载测验和启动完整测试的全部步骤。 - Arnaud Develay
我无法使您的解决方案正常工作。如果您有时间和/或愿意帮助,我已在此处创建了一个存储库:https://github.com/SethKitchen/JeopardyWinner - Seth Kitchen

0

你正在开发一个UWP应用程序,所以在Webview中渲染网站的JavaScript操作与在Web浏览器中渲染时的行为不同。也许你应该开发一个带有自己HTML和脚本的应用程序。 这样做是为了避免安全风险。


谢谢,但我需要使用仅适用于Windows 10通用的OCR功能。 - Seth Kitchen
在这种情况下,不要尝试打开一个网页弹出窗口,而是打开一个本地对话框。 - Santiago Porras

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接