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; + } +}