-
-
Notifications
You must be signed in to change notification settings - Fork 313
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
How to intercept an HTTP API request from webview? #1087
Comments
That one from Tauri is built from custom protocol I believe. Wry doesn't provide any callback or method to intercept HTTP request except custom protocol unfortunately. |
How can I use the Tauri one? I tried this but it didn't work. I think this only works when the HTTP protocol is tauri::Builder::default()
.setup(|app| {
WindowBuilder::new(app, "core", WindowUrl::External(final_url))
.on_web_resource_request(|request, response| {
// if request.uri().contains("beta/users") {
println!("Capturing the request {}", request.uri());
}
)
.user_agent(user_agent)
.build()?;
Ok(())
})
.run(tauri::generate_context!())
.expect("error while running tauri application"); |
I mean the tauri one is still built by wry's function. And wry can only intercept those URL belongs to custom protocol. |
need here too, i want to intercept every requests and decides what url will be blocked when loading remote websites. i had achieved successfully a long time ago by modifying the wry, but a "official solution" will be better. diff --git a/src/webview/mod.rs b/src/webview/mod.rs
index 03b457c..fc03cb7 100644
--- a/src/webview/mod.rs
+++ b/src/webview/mod.rs
@@ -162,6 +162,9 @@ pub struct WebViewAttributes {
/// allow to navigate and false is not.
pub navigation_handler: Option<Box<dyn Fn(String) -> bool>>,
+ // Set a request filter to decide if incoming url is allowed to request.
+ pub system_request_filter: Option<Box<dyn Fn(String) -> bool>>,
+
/// Set a download started handler to manage incoming downloads.
///
/// The closure takes two parameters - the first is a `String` representing the url being downloaded from and and the
@@ -231,6 +234,7 @@ impl Default for WebViewAttributes {
ipc_handler: None,
file_drop_handler: None,
navigation_handler: None,
+ system_request_filter: None,
download_started_handler: None,
download_completed_handler: None,
new_window_req_handler: None,
@@ -477,6 +481,12 @@ impl<'a> WebViewBuilder<'a> {
self
}
+ // Set a request filter to decide if incoming url is allowed to request.
+ pub fn with_system_request_filter(mut self, callback: impl Fn(String) -> bool + 'static) -> Self {
+ self.webview.system_request_filter = Some(Box::new(callback));
+ self
+ }
+
/// Set a download started handler to manage incoming downloads.
///
/// The closure takes two parameters - the first is a `String` representing the url being downloaded from and and the
diff --git a/src/webview/webview2/mod.rs b/src/webview/webview2/mod.rs
index 1e7af7a..bf04b47 100644
--- a/src/webview/webview2/mod.rs
+++ b/src/webview/webview2/mod.rs
@@ -40,7 +40,7 @@ use windows::{
use webview2_com::{Microsoft::Web::WebView2::Win32::*, *};
use crate::application::{platform::windows::WindowExtWindows, window::Window};
-use http::Request;
+use http::{Request, StatusCode};
impl From<webview2_com::Error> for Error {
fn from(err: webview2_com::Error) -> Self {
@@ -452,6 +452,17 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_
let mut custom_protocol_names = HashSet::new();
if !attributes.custom_protocols.is_empty() {
+ // intercept all requests
+ if attributes.system_request_filter.is_some() {
+ unsafe {
+ webview.AddWebResourceRequestedFilter(
+ PCWSTR::from_raw(encode_wide("*".to_string()).as_ptr()),
+ COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL,
+ )
+ }
+ .map_err(webview2_com::Error::WindowsError)?;
+ }
+
for (name, _) in &attributes.custom_protocols {
// WebView2 doesn't support non-standard protocols yet, so we have to use this workaround
// See https://github.com/MicrosoftEdge/WebView2Feedback/issues/73
@@ -465,6 +476,8 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_
.map_err(webview2_com::Error::WindowsError)?;
}
+ let system_request_filter = attributes.system_request_filter;
+
let custom_protocols = attributes.custom_protocols;
let env = env.clone();
unsafe {
@@ -595,6 +608,28 @@ window.addEventListener('mousemove', (e) => window.chrome.webview.postMessage('_
}
Err(_) => Err(E_FAIL.into()),
};
+ } else if let Some(system_request_filter) = &system_request_filter {
+ let allow = system_request_filter(uri);
+
+ if !allow {
+ let stream = CreateStreamOnHGlobal(0, true)?;
+ stream.SetSize(0 as u64)?;
+
+ let body = Some(stream);
+
+ let status_code = StatusCode::NOT_FOUND;
+
+ let response = env.CreateWebResourceResponse(
+ body.as_ref(),
+ 404 as i32,
+ PCWSTR::from_raw(
+ encode_wide(status_code.canonical_reason().unwrap_or("OK")).as_ptr(),
+ ),
+ PCWSTR::from_raw(encode_wide(String::default()).as_ptr()),
+ )?;
+
+ args.SetResponse(&response)?;
+ }
}
} |
I want to intercept the HTTP request that comes from WebView in Wry. Here is my code
I was able to do all the things here with Teams, open the app and login, but I want to see how can I intercept the HTTP requests that are made by Teams while it is inside my Wry's WebView.
I could not find any method in WebView of Wry that can give me that.
There was one in Tauri
on_web_resource_request
(ref) but it returns aBuilder<Wry>
type which I could not inject into the Wry window here.Can someone help me with this? Any help would be appreciated
The text was updated successfully, but these errors were encountered: