main/
main.rs

1/*
2 * Copyright (c) 2016 Boucher, Antoni <bouanto@zoho.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy of
5 * this software and associated documentation files (the "Software"), to deal in
6 * the Software without restriction, including without limitation the rights to
7 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8 * the Software, and to permit persons to whom the Software is furnished to do so,
9 * subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22extern crate gio;
23extern crate glib;
24extern crate gtk;
25extern crate webkit2gtk;
26
27#[cfg(feature = "v2_4")]
28use glib::ToVariant;
29use gtk::{prelude::*, Window, WindowType};
30use webkit2gtk::{SettingsExt, WebContext, WebContextExt, WebView, WebViewExt};
31#[cfg(feature = "v2_6")]
32use webkit2gtk::{UserContentManager, WebViewExtManual};
33
34fn main() {
35  gtk::init().unwrap();
36
37  let window = Window::new(WindowType::Toplevel);
38  let context = WebContext::default().unwrap();
39  #[cfg(feature = "v2_4")]
40  context.set_web_extensions_initialization_user_data(&"webkit".to_variant());
41  context.set_web_extensions_directory("../webkit2gtk-webextension-rs/example/target/debug/");
42  #[cfg(feature = "v2_6")]
43  let webview =
44    WebView::new_with_context_and_user_content_manager(&context, &UserContentManager::new());
45  #[cfg(not(feature = "v2_6"))]
46  let webview = WebView::with_context(&context);
47  webview.load_uri("https://crates.io/");
48  window.add(&webview);
49
50  let settings = WebViewExt::settings(&webview).unwrap();
51  settings.set_enable_developer_extras(true);
52
53  /*let inspector = webview.get_inspector().unwrap();
54  inspector.show();*/
55
56  window.show_all();
57
58  webview.run_javascript("alert('Hello');", None::<&gio::Cancellable>, |_result| {});
59  #[cfg(feature = "v2_22")]
60  webview.run_javascript("42", None::<&gio::Cancellable>, |result| match result {
61    Ok(result) => {
62      use java_script_core::ValueExt;
63      let value = result.js_value().unwrap();
64      println!("is_boolean: {}", value.is_boolean());
65      println!("is_number: {}", value.is_number());
66      println!("{:?}", value.to_int32());
67      println!("{:?}", value.to_boolean());
68    }
69    Err(error) => println!("{}", error),
70  });
71
72  window.connect_delete_event(|_, _| {
73    gtk::main_quit();
74    glib::Propagation::Proceed
75  });
76
77  gtk::main();
78}