-
Notifications
You must be signed in to change notification settings - Fork 15
/
lib.rs
59 lines (50 loc) · 1.66 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
use crc::{crc32, Hasher32};
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
}
#[wasm_bindgen]
// pub fn make_fingerprint() -> Result<JsValue, JsValue> {
pub fn make_fingerprint() -> Result<String, JsValue> {
// f: &js_sys::Function
let window = web_sys::window().expect("should have a window in this context");
let document = web_sys::window().unwrap().document().unwrap();
let performance = window
.performance()
.expect("performance should be available");
let _srt = performance.now();
let canvas = document.create_element("canvas")?;
canvas.set_attribute("display", "none");
let canvas: web_sys::HtmlCanvasElement = canvas
.dyn_into::<web_sys::HtmlCanvasElement>()
.map_err(|_| ())
.unwrap();
let context = canvas
.get_context("2d")
.unwrap()
.unwrap()
.dyn_into::<web_sys::CanvasRenderingContext2d>()
.unwrap();
context.begin_path();
context.fill_style();
context.fill_text("❤️🤪🎉👋", 50.0, 70.0).unwrap();
context.stroke();
let daturl = canvas.to_data_url().unwrap();
let mut digest = crc32::Digest::new_with_initial(crc32::IEEE, 0u32);
digest.write(&daturl.as_bytes());
let _end = performance.now();
let output = format!(
"{{\"ms\": {:?},\"print\": \"{:X}\"}}",
_end - _srt,
digest.sum32()
);
// log(&output);
// let x = JsValue::from_str(&output);
Ok(output)
}