mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-18 07:21:24 +00:00
Add a top-level web_sys::window
function
Returns `Option<Window>` and can be used as a convenience to get a handle to the global `window` object.
This commit is contained in:
@ -17,4 +17,15 @@ extern crate wasm_bindgen;
|
|||||||
extern crate js_sys;
|
extern crate js_sys;
|
||||||
use js_sys::Object;
|
use js_sys::Object;
|
||||||
|
|
||||||
|
#[cfg(feature = "Window")]
|
||||||
|
pub fn window() -> Option<Window> {
|
||||||
|
use wasm_bindgen::{JsValue, JsCast};
|
||||||
|
|
||||||
|
js_sys::Function::new_no_args("return this")
|
||||||
|
.call0(&JsValue::undefined())
|
||||||
|
.ok()?
|
||||||
|
.dyn_into::<Window>()
|
||||||
|
.ok()
|
||||||
|
}
|
||||||
|
|
||||||
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
|
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
|
||||||
|
@ -8,7 +8,7 @@ use wasm_bindgen::JsCast;
|
|||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn draw() {
|
pub fn draw() {
|
||||||
let document = web_sys::Window::document().unwrap();
|
let document = web_sys::window().unwrap().document().unwrap();
|
||||||
let canvas = document.get_element_by_id("canvas").unwrap();
|
let canvas = document.get_element_by_id("canvas").unwrap();
|
||||||
let canvas: web_sys::HtmlCanvasElement = canvas
|
let canvas: web_sys::HtmlCanvasElement = canvas
|
||||||
.dyn_into::<web_sys::HtmlCanvasElement>()
|
.dyn_into::<web_sys::HtmlCanvasElement>()
|
||||||
|
@ -8,3 +8,6 @@ cargo +nightly build --target wasm32-unknown-unknown
|
|||||||
cargo +nightly run --manifest-path ../../crates/cli/Cargo.toml \
|
cargo +nightly run --manifest-path ../../crates/cli/Cargo.toml \
|
||||||
--bin wasm-bindgen -- \
|
--bin wasm-bindgen -- \
|
||||||
../../target/wasm32-unknown-unknown/debug/fetch.wasm --out-dir .
|
../../target/wasm32-unknown-unknown/debug/fetch.wasm --out-dir .
|
||||||
|
|
||||||
|
npm install
|
||||||
|
npm run serve
|
||||||
|
@ -12,7 +12,7 @@ use wasm_bindgen::prelude::*;
|
|||||||
use wasm_bindgen::JsCast;
|
use wasm_bindgen::JsCast;
|
||||||
use wasm_bindgen_futures::future_to_promise;
|
use wasm_bindgen_futures::future_to_promise;
|
||||||
use wasm_bindgen_futures::JsFuture;
|
use wasm_bindgen_futures::JsFuture;
|
||||||
use web_sys::{Request, RequestInit, RequestMode, Response, Window};
|
use web_sys::{Request, RequestInit, RequestMode, Response};
|
||||||
|
|
||||||
/// A struct to hold some data from the github Branch API.
|
/// A struct to hold some data from the github Branch API.
|
||||||
///
|
///
|
||||||
@ -57,7 +57,8 @@ pub fn run() -> Promise {
|
|||||||
.set("Accept", "application/vnd.github.v3+json")
|
.set("Accept", "application/vnd.github.v3+json")
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let request_promise = Window::fetch_with_request(&request);
|
let window = web_sys::window().unwrap();
|
||||||
|
let request_promise = window.fetch_with_request(&request);
|
||||||
|
|
||||||
let future = JsFuture::from(request_promise)
|
let future = JsFuture::from(request_promise)
|
||||||
.and_then(|resp_value| {
|
.and_then(|resp_value| {
|
||||||
|
@ -9,7 +9,7 @@ use wasm_bindgen::JsCast;
|
|||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
let document = web_sys::Window::document().unwrap();
|
let document = web_sys::window().unwrap().document().unwrap();
|
||||||
let canvas = document
|
let canvas = document
|
||||||
.create_element("canvas")
|
.create_element("canvas")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
Reference in New Issue
Block a user