Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: add omnibox #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ categories = ["api-bindings", "wasm"]
crate-type = ["cdylib", "rlib"]

[dependencies]
derive_more = "0.99"
gloo-console = "0.2"
gloo-utils = "0.1.5"
js-sys = "0.3.60"
serde = { version = "1.0.147", features = ["derive"] }
serde = {version = "1.0.147", features = ["derive"]}
serde_derive = "1.0.147"
serde_json = "1.0.87"
thiserror = "1.0.37"
Expand All @@ -32,3 +34,6 @@ wasm-bindgen-test = "0.3.33"
[features]
default = []
firefox = []

[workspace]
members = ["examples/omnibox/new-tab-search"]
37 changes: 37 additions & 0 deletions examples/omnibox/new-tab-search/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
authors = ["Flier Lu <[email protected]>"]
edition = "2018"
name = "new-tab-search"
version = "0.1.0"

[lib]
crate-type = ["cdylib", "rlib"]

[features]
default = ["console_error_panic_hook"]

[dependencies]
gloo-console = "0.2"
gloo-utils = "0.1"
js-sys = "0.3"
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = {version = "0.1.6", optional = true}

# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
# compared to the default allocator's ~10K. It is slower than the default
# allocator, however.
wee_alloc = {version = "0.4", optional = true}

web-extensions = {version = "0.3", path = "../../.."}

[profile.release]
# Tell `rustc` to optimize for small code size.
codegen-units = 1
lto = true
opt-level = "s"
13 changes: 13 additions & 0 deletions examples/omnibox/new-tab-search/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import init, { start } from './pkg/new_tab_search.js';

console.log("WASM module loaded")

async function run() {
await init();

console.log("WASM module initialized")

start();
}

run();
46 changes: 46 additions & 0 deletions examples/omnibox/new-tab-search/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "Omnibox - New Tab Search",
"description": "Type 'nt' plus a search term into the Omnibox to open search in new tab.",
"version": "1.0",
"manifest_version": 3,
"background": {
"service_worker": "background.js",
"type": "module"
},
"permissions": [
"activeTab",
"tabs",
"scripting"
],
"host_permissions": [
"https://*/*"
],
"content_security_policy": {
"extension_pages": "script-src 'self' 'wasm-unsafe-eval'; object-src 'self'"
},
"web_accessible_resources": [
{
"resources": [
"pkg/new_tab_search_bg.wasm"
],
"matches": [
"https://*/*"
]
}
],
"omnibox": {
"keyword": "nt"
},
"action": {
"default_icon": {
"16": "newtab_search16.png",
"32": "newtab_search32.png"
}
},
"icons": {
"16": "newtab_search16.png",
"32": "newtab_search32.png",
"48": "newtab_search48.png",
"128": "newtab_search128.png"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/omnibox/new-tab-search/newtab_search16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/omnibox/new-tab-search/newtab_search32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
128 changes: 128 additions & 0 deletions examples/omnibox/new-tab-search/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
use gloo_console as console;
use js_sys as js;
use wasm_bindgen::prelude::*;

use web_extensions::{self as ext, omnibox::OnInputEnteredDisposition::*};

mod utils;

// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

#[wasm_bindgen]
pub fn start() {
utils::set_panic_hook();

console::info!("Starting background script");

ext::omnibox::set_default_suggestion(&ext::omnibox::DefaultSuggestResult {
description: "Type anything to search",
})
.unwrap();

ext::omnibox::on_input_started()
.add_listener(|| {
console::debug!("Input started");
})
.forget();

ext::omnibox::on_input_cancelled()
.add_listener(|| {
console::debug!("Input cancelled");
})
.forget();

ext::omnibox::on_input_changed()
.add_listener(|text, suggest| {
console::debug!("Input changed", text);
})
.forget();

ext::omnibox::on_input_entered()
.add_listener(|text, disposition| {
console::debug!("Input entered", text, disposition.to_string());

let url = format!(
"https://www.google.com/search?q={}",
js::encode_uri_component(text).to_string(),
);

wasm_bindgen_futures::spawn_local(async move {
let mut tab_id = None;

if disposition == CurrentTab {
let query = ext::tabs::QueryDetails {
active: Some(true),
last_focused_window: Some(true),
..Default::default()
};

match ext::tabs::query(&query).await {
Ok(tabs) => {
if let [tab, ..] = &tabs[..] {
console::debug!(
"current tab",
tab.id.map_or(-1, Into::<i32>::into)
);

tab_id = tab.id;
}
}
Err(err) => {
console::error!("query tabs failed", err.to_string());
}
}
}

if disposition == CurrentTab {
console::info!(
"open on the current tab",
&url,
tab_id.map_or(-1, Into::<i32>::into)
);

match ext::tabs::update(
tab_id,
ext::tabs::UpdateProperties {
url: Some(&url),
..Default::default()
},
)
.await
{
Ok(tab) => {
console::info!(
"opened on the current tab",
&url,
tab.id.map_or(-1, Into::<i32>::into)
)
}
Err(err) => console::error!("update tabs failed", err.to_string()),
}
} else {
console::info!("open on a new tab", &url);

match ext::tabs::create(ext::tabs::CreateProperties {
active: disposition == NewForegroundTab,
url: &url,
})
.await
{
Ok(tab) => {
console::info!(
"open on a new tab",
tab.id.map_or(-1, Into::<i32>::into)
)
}
Err(err) => {
console::error!("create tab failed", err.to_string());
}
};
}
})
})
.forget();
}
10 changes: 10 additions & 0 deletions examples/omnibox/new-tab-search/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub use crate::error::*;
pub mod bookmarks;
pub mod downloads;
pub mod history;
pub mod omnibox;
pub mod tabs;

#[cfg(feature = "firefox")]
Expand Down
Loading