From 386c4e24447447094cf87fb91b22dcad02bbc3d1 Mon Sep 17 00:00:00 2001 From: takker99 <37929109+takker99@users.noreply.github.com> Date: Thu, 17 Feb 2022 20:08:45 +0900 Subject: [PATCH] :sparkles: Support for URL schemes --- omnibox/background.ts | 7 +++++-- omnibox/deps/testing.ts | 1 + omnibox/isURL.test.ts | 11 +++++++++++ omnibox/isURL.ts | 10 ++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 omnibox/deps/testing.ts create mode 100644 omnibox/isURL.test.ts create mode 100644 omnibox/isURL.ts diff --git a/omnibox/background.ts b/omnibox/background.ts index 973aa36..821d972 100644 --- a/omnibox/background.ts +++ b/omnibox/background.ts @@ -15,6 +15,7 @@ import { Asearch } from "./deps/asearch.ts"; import { browser } from "./deps/webextension.ts"; import { ensureTabId } from "./utils.ts"; import { getData } from "./storage.ts"; +import { isURL } from "./isURL.ts"; // // browserActionボタンを押したときcontent_script.jsにメッセージを送る @@ -80,10 +81,12 @@ function search( // ユーザがメニューを選択したとき呼ばれるもの // browser.omnibox.onInputEntered.addListener(async (text) => { - if (text.match(/^http/)) { + if (isURL(text)) { browser.tabs.update({ url: text }); } else { - const response = await fetch("https://goquick.org", { credentials: "include"}); // GoQuick.orgユーザはGoQuick.orgを利用 + const response = await fetch("https://goquick.org", { + credentials: "include", + }); // GoQuick.orgユーザはGoQuick.orgを利用 const data = await response.text(); browser.tabs.update({ url: data.match("GoQuick Login") diff --git a/omnibox/deps/testing.ts b/omnibox/deps/testing.ts new file mode 100644 index 0000000..89ab027 --- /dev/null +++ b/omnibox/deps/testing.ts @@ -0,0 +1 @@ +export * from "https://deno.land/std@0.126.0/testing/asserts.ts"; diff --git a/omnibox/isURL.test.ts b/omnibox/isURL.test.ts new file mode 100644 index 0000000..7be608c --- /dev/null +++ b/omnibox/isURL.test.ts @@ -0,0 +1,11 @@ +/// +import { isURL } from "./isURL.ts"; +import { assert } from "./deps/testing.ts"; + +Deno.test("isURL()", () => { + assert(isURL("https://example.com")); + assert(isURL("http://goquick.org")); + assert(isURL("file:///users/file.txt")); + assert(isURL("things:///show?id=xxx")); + assert(!isURL("helpfeel")); +}); diff --git a/omnibox/isURL.ts b/omnibox/isURL.ts new file mode 100644 index 0000000..45259ce --- /dev/null +++ b/omnibox/isURL.ts @@ -0,0 +1,10 @@ +/** URLかどうか判定する */ +export function isURL(text: string): boolean { + try { + new URL(text); + return true; + } catch (e: unknown) { + if (e instanceof TypeError) return false; + throw e; + } +}