mirror of
https://github.com/Stremio/stremio-shell-ng.git
synced 2026-01-11 22:40:32 +00:00
Merge pull request #21 from Stremio/fix/webview-handle-window-request
fix(webview): handle window request
This commit is contained in:
commit
80df7bcd17
3 changed files with 43 additions and 17 deletions
15
src/stremio_app/stremio_wevbiew/constants.rs
Normal file
15
src/stremio_app/stremio_wevbiew/constants.rs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
pub const WARNING_URL: &str = "https://www.stremio.com/warning#";
|
||||
pub const WHITELISTED_HOSTS: &[&str] = &[
|
||||
"stremio.com",
|
||||
"strem.io",
|
||||
"stremio.zendesk.com",
|
||||
"google.com",
|
||||
"youtube.com",
|
||||
"twitch.tv",
|
||||
"twitter.com",
|
||||
"x.com",
|
||||
"netflix.com",
|
||||
"adex.network",
|
||||
"amazon.com",
|
||||
"forms.gle",
|
||||
];
|
||||
|
|
@ -1,2 +1,4 @@
|
|||
mod constants;
|
||||
|
||||
pub mod wevbiew;
|
||||
pub use wevbiew::WebView;
|
||||
|
|
|
|||
|
|
@ -9,11 +9,14 @@ use std::mem;
|
|||
use std::rc::Rc;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread;
|
||||
use url::Url;
|
||||
use urlencoding::decode;
|
||||
use webview2::Controller;
|
||||
use winapi::shared::windef::HWND;
|
||||
use winapi::um::winuser::{GetClientRect, VK_F7, WM_SETFOCUS};
|
||||
|
||||
use super::constants::{WARNING_URL, WHITELISTED_HOSTS};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct WebView {
|
||||
pub endpoint: Rc<OnceCell<String>>,
|
||||
|
|
@ -101,6 +104,29 @@ impl PartialUi for WebView {
|
|||
settings.put_are_host_objects_allowed(false).ok();
|
||||
settings.put_are_default_script_dialogs_enabled(false).ok();
|
||||
|
||||
// Handle window.open and href
|
||||
webview.add_new_window_requested(move |_webview, event| {
|
||||
if let Ok(uri) = event.get_uri() {
|
||||
if let Ok(url) = Url::parse(&uri) {
|
||||
let is_whitelisted = url.host().is_some_and(|host| {
|
||||
WHITELISTED_HOSTS.iter().any(|whitelisted_host| host.to_string().ends_with(whitelisted_host))
|
||||
});
|
||||
|
||||
let final_url = if is_whitelisted {
|
||||
url.to_string()
|
||||
} else {
|
||||
format!("{}{}", WARNING_URL, urlencoding::encode(url.as_ref()))
|
||||
};
|
||||
|
||||
if let Err(e) = open::that(final_url) {
|
||||
eprintln!("Failed to open URL: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
if let Some(endpoint) = endpoint.get() {
|
||||
if webview
|
||||
.navigate(endpoint.as_str()).is_err() {
|
||||
|
|
@ -138,23 +164,6 @@ impl PartialUi for WebView {
|
|||
})
|
||||
}catch(e){}
|
||||
|
||||
window.open = (url) => {
|
||||
if (typeof url === 'string' && URL.canParse(url)) {
|
||||
try {
|
||||
const message = {
|
||||
id: 1,
|
||||
args: ['open-external', url],
|
||||
};
|
||||
|
||||
window.chrome.webview.postMessage(JSON.stringify(message));
|
||||
} catch(e) {
|
||||
console.error('Failed to post message');
|
||||
}
|
||||
} else {
|
||||
return console.error('Not a valid URL string');
|
||||
}
|
||||
};
|
||||
|
||||
try{console.log('Shell JS injected');if(window.self === window.top) {
|
||||
window.qt={webChannelTransport:{send:window.chrome.webview.postMessage}};
|
||||
window.chrome.webview.addEventListener('message',ev=>window.qt.webChannelTransport.onmessage(ev));
|
||||
|
|
|
|||
Loading…
Reference in a new issue