Created
August 7, 2023 00:39
-
-
Save ncaq/38d995991b3f06acf444eb313c69ba6e to your computer and use it in GitHub Desktop.
指定されたURLのタブに移動するか新規に開くスクリプト、WebExtension版とSurfingkeys版の両法を記載。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 指定されたURLのタブが既に開かれていればそのタブをアクティブにします。 | |
* 開かれていなければ新規に開きます。 | |
*/ | |
function tabActivateOrCreate(url) { | |
RUNTIME("getTabs", { queryInfo: { url, currentWindow: true } }, ({ tabs }) => { | |
if (!Array.isArray(tabs)) { | |
throw new Error(`tabs is not Array: ${JSON.stringify(tabs)}`); | |
} | |
const tabId = tabs?.[0]?.id; | |
// タブが存在すれば、そのタブをアクティブにします。 | |
if (tabId != null) { | |
RUNTIME("focusTab", { tabId }); | |
} else { | |
// タブが存在しなければ、新しくタブを開きます。 | |
tabOpenLink(url); | |
} | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
w/** | |
* 指定されたURLのタブが既に開かれていればそのタブをアクティブにします。 | |
* 開かれていなければ新規に開きます。 | |
*/ | |
async function tabActivateOrCreate(url) { | |
const tabs = await browser.tabs.query({ url }); | |
if (!Array.isArray(tabs)) { | |
throw new Error(`tab is not Array: ${tabs}`); | |
} | |
const id = tabs?.[0]?.id; | |
// タブが存在すれば、そのタブをアクティブにします。 | |
if (id != null) { | |
browser.tabs.update(id, { active: true }); | |
} else { | |
// タブが存在しなければ、新しくタブを開きます。 | |
browser.tabs.create({ url }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment