Skip to content

[Problem/Bug]: I want to use add_ebResourceRequested to make changes to the JS file, but it won't succeed. #5327

@LeeTomzed

Description

@LeeTomzed

What happened?

the developer tool shows a network request error. Even using official examples SampleApps/WebView2APISample/ScenarioSharedWorkerWRR.cpp , use the same question.

wil::com_ptr<ICoreWebView2_22> webview222 = data->webview.try_query<ICoreWebView2_22>();
webview222 ->AddWebResourceRequestedFilterWithRequestSourceKinds(L"https://lf-webcast-platform.bytetos.com/obj/webcast-platform-cdn/webcast/douyin_live/*", COREWEBVIEW2_WEB_RESOURCE_CONTEXT_SCRIPT, COREWEBVIEW2_WEB_RESOURCE_REQUEST_SOURCE_KINDS_ALL);
data->webview->add_WebResourceRequested(Callback<ICoreWebView2WebResourceRequestedEventHandler>(
    [data](ICoreWebView2* sender, ICoreWebView2WebResourceRequestedEventArgs* args) -> HRESULT {
        wil::com_ptr<ICoreWebView2WebResourceRequest> request;
        args->get_Request(&request);
        wil::unique_cotaskmem_string uri;
        request->get_Uri(&uri);
        std::wstring url(uri.get());
        std::wstringstream ss(url);
        if (url.find(L".js") == std::string::npos)
        {
            return S_OK;
        }
        std::wstring segment;
        std::vector<std::wstring> arr;
        while (std::getline(ss, segment, L"/")) {
            arr.push_back(segment);
        }
        if (arr.size() != 8 || url.find(L"client") != std::string::npos || url.find(L"runtime") != std::string::npos || url.find(L"polyfill") != std::string::npos)
        {
            return S_OK;
        }
        std::wstring targetUri(uri.get());
        std::string originalJs = DownloadUrlContentA(targetUri);
        if (!originalJs.empty()) {
            std::string modifiedJs = ModifyJavaScript(originalJs);
            auto stream = SHCreateMemStream(
                reinterpret_cast<const BYTE*>(modifiedJs.data()),
                static_cast<UINT>(modifiedJs.size())
            );
            wil::com_ptr<ICoreWebView2WebResourceResponse> response;
            Microsoft::WRL::ComPtr<ICoreWebView2Environment> environment;
            Microsoft::WRL::ComPtr<ICoreWebView2_2> webview2;
            data->webview->QueryInterface(IID_PPV_ARGS(&webview2));
            webview2->get_Environment(&environment);
            environment->CreateWebResourceResponse(
                stream,
                200,
                L"OK",
                L"Content-Type: application/javascript; charset=utf-8",
                &response
            );
            args->put_Response(response.get());
            return S_OK;
        }
        return S_OK;
    }
).Get(), &data->webResourceToken);
Image

Importance

Blocking. My app's basic functions are not working due to this issue.

Runtime Channel

Stable release (WebView2 Runtime)

Runtime Version

138.0.3351.109

SDK Version

1.0.3351.48

Framework

Win32

Operating System

Windows 11

OS Version

1000.26100.128.0

Repro steps

  1. git clone https://github.com/MicrosoftEdge/WebView2Samples.git
  2. open WebView2GettingStarted.sln
  3. update Microsoft.Web.WebView2 1.0.3351.48 by NuGet
  4. change \GettingStartedGuides\Win32_GettingStarted\HelloWebView.cpp source code
  5. run

Repros in Edge Browser

No, issue does not reproduce in the corresponding Edge version

Regression

Don't know

Last working version (if regression)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions