Git Product home page Git Product logo

Comments (20)

quadrupleslap avatar quadrupleslap commented on July 29, 2024 2

@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.

Boscop avatar Boscop commented on July 29, 2024

You can use eval to set document.documentElement.innerHTML like described here:
webview/webview#79 (comment)

from web-view.

eedahl avatar eedahl commented on July 29, 2024

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.

eedahl avatar eedahl commented on July 29, 2024

EDIT: Removed. I need to hack more before I can conclude if this works or not.

from web-view.

Boscop avatar Boscop commented on July 29, 2024

You call this eval function from your Rust host:
https://github.com/Boscop/web-view/blob/master/src/lib.rs#L144

from web-view.

eedahl avatar eedahl commented on July 29, 2024

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.

Boscop avatar Boscop commented on July 29, 2024

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.

eedahl avatar eedahl commented on July 29, 2024

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.

Boscop avatar Boscop commented on July 29, 2024

Yea, you can always optimize it later :)

from web-view.

Boscop avatar Boscop commented on July 29, 2024

Thanks @quadrupleslap, I'll publish it to crates.io now :)

from web-view.

Boscop avatar Boscop commented on July 29, 2024

It's on crates.io now, v0.2.1

from web-view.

eedahl avatar eedahl commented on July 29, 2024

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.

eedahl avatar eedahl commented on July 29, 2024

@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.

quadrupleslap avatar quadrupleslap commented on July 29, 2024

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.

eedahl avatar eedahl commented on July 29, 2024

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.

Boscop avatar Boscop commented on July 29, 2024

@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.

quadrupleslap avatar quadrupleslap commented on July 29, 2024

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.

Boscop avatar Boscop commented on July 29, 2024

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.

eedahl avatar eedahl commented on July 29, 2024

@quadrupleslap Just a heads-up that unescape would probably be up for a PR with escape functionality

from web-view.

phase avatar phase commented on July 29, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.