Comments (20)
@eedahl you can use web_view::escape to safely pass strings to JavaScript now, but it hasn't been published to crates.io.
from web-view.
You can use eval
to set document.documentElement.innerHTML
like described here:
webview/webview#79 (comment)
from web-view.
Okay, neat, so I would just add this as a JavaScript? Maybe I'll figure it out just from staring at it some more but I'm not sure how to invoke this "Rust-side" as some event-listener in my Rust code detects a change in the data-file I'm reading.
EDIT: I'll stare at it until I get what's going on.
from web-view.
EDIT: Removed. I need to hack more before I can conclude if this works or not.
from web-view.
You call this eval function from your Rust host:
https://github.com/Boscop/web-view/blob/master/src/lib.rs#L144
from web-view.
Yeah, but I need a WebView
object to do that if I understand this correctly, which I don't get from calling run
or seemingly anything else. I've now managed to call functions that call functions that access the WebView
object, if a by rather circumvent methods. Trying to learn form your timer example I've arrived at sending
move |webview| {
webview.dispatch(|webview, userdata| {
//listener code that calls
//update_html(webview);
})
}
where update_html
is
fn update_html<'a, T>(webview: &mut WebView<'a, T>) {
let html_table = create_html_table();
let html = create_html(html_table);
//r#"<!doctype html><html><body><h1>a horse is a horse is a horse</h1></body></html>"#;
println!("blep");
webview.eval(&format!(
"{}{}{}",
"document.documentElement.innerHTML=\"", html, "\""
));;
}
which works for the mock example (a horse is a horse is a horse) and should work in the more general example given that I find a replacement for Go's JSEscapeString.
from web-view.
Yes, the init callback will give you the WebView
object which you can call eval on (also later if you keep it around).
Not sure if there's a crate that does what JSEscapeString
does but it's easy to do the same manually..
(A quick crates search only turned up a crate that does the opposite.)
Btw, I'd write format!("document.documentElement.innerHTML=\"{}\";", html)
from web-view.
This solution serves the dual function of making ones' skin crawl while doing the trick for now, haha.
html = html.replace(r#"""#, r#"\""#).replace("/", r"\/").replace(r"'", r"\'").replace("\n", r"\n").replace("\r", r"\r");
from web-view.
Yea, you can always optimize it later :)
from web-view.
Thanks @quadrupleslap, I'll publish it to crates.io now :)
from web-view.
It's on crates.io now, v0.2.1
from web-view.
Wow, that's very cool, thanks for bothering with this! I'll leave it an exercise to myself to implement it too at some point, I confess it's not a hard problem but my Rust is bakery fresh.
from web-view.
@quadrupleslap I think there's one last thing or so web_view::escape
doesn't escape.
webview.eval(&format!(
"document.documentElement.innerHTML=\"{}\";",
html
));
works for me with my patented eye-bleed
html = html.replace(r#"""#, r#"\""#).replace("/", r"\/").replace(r"'", r"\'").replace("\n", r"\n").replace("\r", r"\r");
but web_view::escape
gives a scripting error, "Expected ";""
from web-view.
web_view::escape
adds its own quotes, but if that's not the problem, could you please send some erroring HTML so that I can try fixing it?
from web-view.
EDI: Removed. That does indeed seem to be the problem. If I was wrong about that too, here is some html:
https://pastebin.com/wPVR4ER6
from web-view.
@quadrupleslap Wasn't the intention of the escape
function to also use it with json passed between host and frontend (as a serde_json
"serialize post processor" and "deserialize pre processor" by using serde_json::to_value
/from_value
and walking the Value
's values)?
Json strings have to use double quotes.
So escape
has to either use double quotes or not put any quotes around the escaped string, right?
from web-view.
Sorry, it only serializes strings, and only for JavaScript. I'll get on implementing that, though, it sounds a lot more useful. :P
Edit: Should it strive to be both JSON and JS compatible, or just JS?
from web-view.
If the escape
function used double quotes or no quotes, it would be able to be re-used inside the JSON escaping function, too, right?
If so, I think escape
should strive to be compatible with that, so it can be re-used :)
from web-view.
@quadrupleslap Just a heads-up that unescape would probably be up for a PR with escape functionality
from web-view.
Using this
webview.eval(format!("document.documentElement.innerHTML=String.raw`{}`", YOUR_HTML).unwrap();
looks like it works fine, but I did notice that only the JS in my original HTML was being parsed for some reason. I'm not sure if that's a side effect of this or webkit.
from web-view.
Related Issues (20)
- Way to get current URL?
- Debug mode dumps HTML on every console message HOT 1
- Is there a ready event?
- Is this browser ready?
- Is there a way to access the true file system path when using a "Browse" button on webview?
- [Solved] Compilation fails with Edge feature enabled HOT 1
- Hovering over an image for alt text causes `NSException` on macOS 12.1 w/ M1
- Is multitouch supported? HOT 1
- Android? iOS?
- Make a fork and cooperate to maintain this crate! HOT 1
- box-shadow wrongly displayed
- iframe support?
- Corrupted files
- Is there a way to open two webview windows without crashing? HOT 1
- external.invoke returns undefined instead of promise on Windows 10 HOT 1
- Rust Webview JavaScript interface
- how to create handler? HOT 1
- Impossible to embed a local .js file to the .html code - it is simply not loaded HOT 1
- Blank page when running on X11 (but Wayland is ok)
- macos webview_cocoa.c unknown error
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from web-view.