tauri icon indicating copy to clipboard operation
tauri copied to clipboard

[bug] `tauri::Plugin`'s `js_init_script` does not run in iframes

Open Vexcited opened this issue 7 months ago • 3 comments

Describe the bug

In the documentation, it says it runs on all top-level document and child frame page navigations but I think I misunderstood the following because when adding a way to tweak window and navigator properties to my internal plugin, it seemed like it only applied to the top window and none of the iframes' window.

Reproduction

SETUP

  1. Navigate to a page that has a data: URL.
  2. Scripts in this page creates an iframe to an external https: domain which contains an HTML document that is loaded.

PLUGIN

As simple as...

pub const INIT_SCRIPT: &str = r#"
  console.log(-1, "Hello, from", window.location.origin);

  const SCREEN_WIDTH = 393;
  const SCREEN_HEIGHT = 852;

  const tweak_screen = (prop, val) => window.screen.__defineGetter__(prop, () => val);
  tweak_screen("height", SCREEN_HEIGHT);
  tweak_screen("width", SCREEN_WIDTH);
  tweak_screen("availHeight", SCREEN_HEIGHT);
  tweak_screen("availWidth", SCREEN_WIDTH);
  tweak_screen("pixelDepth", 24);
  tweak_screen("colorDepth", 24);

  window.__defineGetter__("innerHeight", () => SCREEN_HEIGHT);
  window.__defineGetter__("innerWidth", () => SCREEN_WIDTH);
  window.screen.orientation.__defineGetter__("type", () => "portrait-primary");
  navigator.__defineGetter__("platform", () => "iPhone");
"#;

// ... in the plugin's `init` function ...

Builder::new("internal-api")
  .js_init_script(INIT_SCRIPT.to_string())
  .build()

DEMO

We can simply observe this by looking at the console and see that only a null domain is logged while another log is expected from iframe.

Also, we can check if properties are modified in the iframe.

Image

As we can see, they're only modified in the top window but not in the iframe's window.

Expected behavior

Initialization script should also run in the iframe, as mentioned in the documentation.

Full tauri info output

[✔] Environment
    - OS: Mac OS 15.5.0 arm64 (X64)
    ✔ Xcode Command Line Tools: installed
    ✔ rustc: 1.87.0 (17067e9ac 2025-05-09)
    ✔ cargo: 1.87.0 (99624be96 2025-05-06)
    ✔ rustup: 1.28.2 (e4f3ad6f8 2025-04-28)
    ✔ Rust toolchain: stable-aarch64-apple-darwin (default)
    - node: 24.1.0
    - pnpm: 10.11.0
    - yarn: 1.22.22
    - npm: 11.4.1
    - bun: 1.2.15

[-] Packages
    - tauri 🦀: 2.5.1
    - tauri-build 🦀: 2.2.0
    - wry 🦀: 0.51.2
    - tao 🦀: 0.33.0
    - @tauri-apps/api : 2.5.0
    - @tauri-apps/cli : 2.5.0

[-] Plugins
    - tauri-plugin-opener 🦀: 2.2.6, (outdated, latest: 2.2.7)
    - @tauri-apps/plugin-opener : 2.2.6 (outdated, latest: 2.2.7)
    - tauri-plugin-http 🦀: 2.4.3, (outdated, latest: 2.4.4)
    - @tauri-apps/plugin-http : 2.4.3 (outdated, latest: 2.4.4)
    - tauri-plugin-dialog 🦀: 2.2.1, (outdated, latest: 2.2.2)
    - @tauri-apps/plugin-dialog : 2.2.1 (outdated, latest: 2.2.2)
    - tauri-plugin-os 🦀: 2.2.1
    - @tauri-apps/plugin-os : 2.2.1

[-] App
    - build-type: bundle
    - CSP: unset
    - frontendDist: ../dist
    - devUrl: http://localhost:1420/
    - framework: SolidJS
    - bundler: Vite

[-] iOS
    - Developer Teams: Mikkel ALMONTE--RINGAUD (ID: WLAPHT4WXV)

Stack trace


Additional context

No response

Vexcited avatar Jun 05 '25 23:06 Vexcited

Yeah, currently they're injected to subframes on Windows (webview2 doesn't have the option to only inject to the main frame yet) but not on other platforms, we should document this and maybe provide a new API that allows you to always inject to all subframes like we did for https://github.com/tauri-apps/tauri/blob/f1891540bfdd4694023d82fa5c9fb986b30e7d37/crates/tauri/src/webview/mod.rs#L742

Legend-Master avatar Jun 06 '25 07:06 Legend-Master

So, as of right now, my only solution is to move my window from my tauri.conf.json to Rust so I can use initialization_script_for_all_frames ?

Is there any ETA on when this new API could be implemented ?

Vexcited avatar Jun 06 '25 11:06 Vexcited

So, as of right now, my only solution is to move my window from my tauri.conf.json to Rust so I can use initialization_script_for_all_frames ?

Yeah

Is there any ETA on when this new API could be implemented ?

I could take a look at it next week, no promise though

Legend-Master avatar Jun 06 '25 11:06 Legend-Master