Skip to content

Instantly share code, notes, and snippets.

@ncaq
Created August 7, 2023 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ncaq/38d995991b3f06acf444eb313c69ba6e to your computer and use it in GitHub Desktop.
Save ncaq/38d995991b3f06acf444eb313c69ba6e to your computer and use it in GitHub Desktop.
指定されたURLのタブに移動するか新規に開くスクリプト、WebExtension版とSurfingkeys版の両法を記載。
/**
* 指定された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);
}
});
}
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