From 5d13bb734d45a8f3ff95723509f500b31c3ec57e Mon Sep 17 00:00:00 2001 From: James Kay Date: Tue, 16 Apr 2024 18:29:04 +0100 Subject: [PATCH 1/7] Add sample application page --- sample-app/index.html | 47 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 sample-app/index.html diff --git a/sample-app/index.html b/sample-app/index.html new file mode 100644 index 0000000..7121cec --- /dev/null +++ b/sample-app/index.html @@ -0,0 +1,47 @@ + + + + + + + + From 7958267315fcaf30b69b4dbb71dc0688e0673e4c Mon Sep 17 00:00:00 2001 From: James Kay Date: Tue, 16 Apr 2024 18:29:04 +0100 Subject: [PATCH 2/7] Update for manifest v3 --- build.sh | 48 ++------------------------------------------ js/content_script.js | 22 ++++++++++++++++++++ js/service_worker.js | 5 +++++ manifest.json | 20 ++++++++++++++++++ manifest_v2.json | 18 ----------------- manifest_v3.json | 19 ------------------ src/lib.rs | 7 ++++++- 7 files changed, 55 insertions(+), 84 deletions(-) create mode 100644 js/content_script.js create mode 100644 js/service_worker.js create mode 100644 manifest.json delete mode 100644 manifest_v2.json delete mode 100644 manifest_v3.json diff --git a/build.sh b/build.sh index 34069c1..7f8ab32 100755 --- a/build.sh +++ b/build.sh @@ -1,52 +1,8 @@ #!/bin/sh -command -v cargo >/dev/null 2>&1 || { echo >&2 "Installing cargo..."; curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh; } -command -v wasm-pack >/dev/null 2>&1 || { echo >&2 "Installing wasm-pack..."; cargo install wasm-pack; } - rm -rf pkg -# Find value of argument --manifest-version -for arg in "$@"; do - case "$arg" in - --manifest-version=*) - manifest_version="${arg#*=}" - ;; - esac -done - -# Find whether release was passed -for arg in "$@"; do - case "$arg" in - --release) - release=true - ;; - esac -done - # Build for release or debug -if [ "$release" = true ]; then - wasm-pack build --target=no-modules --release || exit 1 -else - wasm-pack build --target=no-modules --dev || exit 1 -fi - -# Copy manifest.json to pkg -if [ "$manifest_version" = "v3" ] || [ "$manifest_version" = "3" ]; then - cp manifest_v3.json pkg/manifest.json -else if [ "$manifest_version" = "v2" ] || [ "$manifest_version" = "2" ]; then - cp manifest_v2.json pkg/manifest.json -else - echo "Packaging with manifest version v2. Pass --manifest-version=v3 to package with manifest version 3." - cp manifest_v2.json pkg/manifest.json - fi -fi - -printf " -const runtime = chrome.runtime || browser.runtime; - -async function run() { - await wasm_bindgen(runtime.getURL('linera_web_bg.wasm')); -} +wasm-pack build --target=web "$@" -run(); -" >> pkg/run_wasm.js +ln js/* manifest.json pkg/ diff --git a/js/content_script.js b/js/content_script.js new file mode 100644 index 0000000..be8876a --- /dev/null +++ b/js/content_script.js @@ -0,0 +1,22 @@ +function handleMessage(event) { + console.log("received message"); + if (event.source != window) return; + console.log("received message", event.data); + return true; +} + +function respond(event, response) { + window.dispatchEvent(new CustomEvent("linera-wallet-response", { + detail: { + id: event.detail.id, + message: response, + } + })) +} + +window.addEventListener("linera-wallet-request", e => { + console.log("got request", e.detail); + respond(e, "Hi!"); +}); + +window.dispatchEvent(new Event("linera-wallet-load")); diff --git a/js/service_worker.js b/js/service_worker.js new file mode 100644 index 0000000..5dfe76e --- /dev/null +++ b/js/service_worker.js @@ -0,0 +1,5 @@ +import wasm_bindgen from "./linera_web.js"; + +(async () => await wasm_bindgen( + (chrome.runtime || browser.runtime).getURL('linera_web_bg.wasm') +))(); diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..4244b0b --- /dev/null +++ b/manifest.json @@ -0,0 +1,20 @@ +{ + "name" : "linera-web", + "version" : "1.0", + "description" : "linera-web", + "permissions": [], + "content_scripts": [ + { + "matches": ["file:///*"], + "js": ["content_script.js"] + } + ], + "background": { + "service_worker": "service_worker.js", + "type": "module" + }, + "content_security_policy": { + "extension_pages": "script-src 'self' 'wasm-unsafe-eval';" + }, + "manifest_version": 3 +} diff --git a/manifest_v2.json b/manifest_v2.json deleted file mode 100644 index 2be423d..0000000 --- a/manifest_v2.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "name" : "linera-web", - "version" : "1.0", - "description" : "linera-web", - "permissions": [], - "content_scripts": [ - { - "matches": ["*://*.example.com/*"], - "js": [ - "linera_web.js", "run_wasm.js" - ] - } - ], - "web_accessible_resources": [ - "linera_web_bg.wasm" - ], - "manifest_version": 2 -} diff --git a/manifest_v3.json b/manifest_v3.json deleted file mode 100644 index 773b529..0000000 --- a/manifest_v3.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "name" : "linera-web", - "version" : "1.0", - "description" : "linera-web", - "permissions": [], - "content_scripts": [ - { - "matches": ["*://*.example.com/*"], - "js": [ - "linera_web.js", "run_wasm.js" - ] - } - ], - "web_accessible_resources": [{ - "resources": ["linera_web_bg.wasm"], - "matches": ["*://*.example.com/*"] - }], - "manifest_version": 3 -} diff --git a/src/lib.rs b/src/lib.rs index 1eab367..798cb5d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,9 +3,14 @@ use web_sys::*; #[macro_use] mod util; +#[wasm_bindgen] +pub fn query(n: u32) -> u32 { + n + 1 +} + #[wasm_bindgen(start)] pub async fn main() { std::panic::set_hook(Box::new(console_error_panic_hook::hook)); log!("Hello World!"); -} \ No newline at end of file +} From 7f6c992d7c445c5991fe919079214d5687280555 Mon Sep 17 00:00:00 2001 From: James Kay Date: Tue, 16 Apr 2024 18:29:04 +0100 Subject: [PATCH 3/7] Clean up sample app --- sample-app/index.html | 32 +------------------------------- sample-app/linera.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 31 deletions(-) create mode 100644 sample-app/linera.js diff --git a/sample-app/index.html b/sample-app/index.html index 7121cec..e179aae 100644 --- a/sample-app/index.html +++ b/sample-app/index.html @@ -1,38 +1,8 @@ +