Skip to content

Commit

Permalink
fix: Injecting into subframes on platforms other than linux
Browse files Browse the repository at this point in the history
  • Loading branch information
Norbiros committed Sep 23, 2024
1 parent 15957a9 commit c65ca84
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
11 changes: 3 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ pub use error::*;
pub use http;
pub use proxy::{ProxyConfig, ProxyEndpoint};
pub use web_context::WebContext;
use webkit2gtk::UserContentInjectedFrames;

/// A rectangular region.
#[derive(Clone, Copy, Debug)]
Expand Down Expand Up @@ -506,7 +505,7 @@ pub struct WebViewAttributes {
pub bounds: Option<Rect>,

/// Whether JavaScript code should be injected into only main or all subframes
pub inject_into_subframes: UserContentInjectedFrames,
pub inject_into_subframes: bool,
}

impl Default for WebViewAttributes {
Expand Down Expand Up @@ -545,7 +544,7 @@ impl Default for WebViewAttributes {
position: dpi::LogicalPosition::new(0, 0).into(),
size: dpi::LogicalSize::new(200, 200).into(),
}),
inject_into_subframes: UserContentInjectedFrames::TopFrame,
inject_into_subframes: false,
}
}
}
Expand Down Expand Up @@ -898,11 +897,7 @@ impl<'a> WebViewBuilder<'a> {

/// Whether JavaScript code should be injected to all subframes
pub fn with_inject_into_subframes(mut self, inject_into_subframes: bool) -> Self {
self.attrs.inject_into_subframes = if inject_into_subframes {
UserContentInjectedFrames::AllFrames
} else {
UserContentInjectedFrames::TopFrame
};
self.attrs.inject_into_subframes = inject_into_subframes;
self
}

Expand Down
7 changes: 6 additions & 1 deletion src/webkitgtk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,8 +598,13 @@ impl InnerWebView {
Ok(())
}

fn init(&self, js: &str, injected_frames: UserContentInjectedFrames) -> Result<()> {
fn init(&self, js: &str, inject_into_subframes: bool) -> Result<()> {
if let Some(manager) = self.webview.user_content_manager() {
let injected_frames = if inject_into_subframes {
UserContentInjectedFrames::AllFrames
} else {
UserContentInjectedFrames::TopFrame
};
let script = UserScript::new(
js,
injected_frames,
Expand Down
13 changes: 6 additions & 7 deletions src/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ use http::{
version::Version,
Request, Response as HttpResponse,
};
use webkit2gtk::UserContentInjectedFrames;

const IPC_MESSAGE_HANDLER_NAME: &str = "ipc";
#[cfg(target_os = "macos")]
Expand Down Expand Up @@ -1110,16 +1109,16 @@ r#"Object.defineProperty(window, 'ipc', {
Ok(())
}

fn init(&self, js: &str, injected_frames: UserContentInjectedFrames) {
let for_main_frame_only = match injected_frames {
UserContentInjectedFrames::AllFrames => 0,
_ => 1,
};

fn init(&self, js: &str, inject_into_subframes: bool) {
// Safety: objc runtime calls are unsafe
// Equivalent Obj-C:
// [manager addUserScript:[[WKUserScript alloc] initWithSource:[NSString stringWithUTF8String:js.c_str()] injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES]]
unsafe {
let for_main_frame_only = match inject_into_subframes {
true => 0,
false => 1,
};

let userscript: id = msg_send![class!(WKUserScript), alloc];
let script: id = msg_send![userscript, initWithSource:NSString::new(js) injectionTime:0 forMainFrameOnly:for_main_frame_only];
let _: () = msg_send![self.manager, addUserScript: script];
Expand Down

0 comments on commit c65ca84

Please sign in to comment.