LoginSignup
0
0

タブ

Posted at
// 現在のタブの情報を取得
var currentTab = browser.tabs.getCurrent();

currentTab.then(function(tabInfo) {
    // タブのタイトルを取得
    var tabTitle = tabInfo.title;
    
    // タブのURLを取得
    var tabUrl = tabInfo.url;

    console.log("タブのタイトル:", tabTitle);
    console.log("タブのURL:", tabUrl);
}).catch(function(error) {
    console.error("エラー:", error);
});
// すべてのタブの情報を取得
browser.tabs.query({}).then(function(tabs) {
    tabs.forEach(function(tab) {
        // タブのタイトルを取得
        var tabTitle = tab.title;

        // タブのURLを取得
        var tabUrl = tab.url;

        console.log("タブのタイトル:", tabTitle);
        console.log("タブのURL:", tabUrl);
    });
}).catch(function(error) {
    console.error("エラー:", error);
});

// 新しいタブを開く前に同じURLのタブが存在するか確認する
function openNewTabIfNotExists(url) {
    browser.tabs.query({ url: url }).then(function(tabs) {
        if (tabs.length > 0) {
            // 同じURLのタブが存在する場合、そのタブをアクティブにする
            browser.tabs.update(tabs[0].id, { active: true });
        } else {
            // 同じURLのタブが存在しない場合、新しいタブを開く
            browser.tabs.create({ url: url });
        }
    }).catch(function(error) {
        console.error("エラー:", error);
    });
}

// 使用例
var targetUrl = "https://example.com";
openNewTabIfNotExists(targetUrl);
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Open Window Example</title>
</head>
<body>

<!-- ボタンをクリックすると新しいウィンドウが開かれる -->
<button onclick="openNewWindow('https://example.com', 'exampleWindow')">Open Example.com in a new window</button>

<script>
function openNewWindow(url, windowName) {
    // 同じ名前のウィンドウが存在するか確認する
    var existingWindow = window.open('', windowName);

    if (existingWindow) {
        // 同じ名前のウィンドウが存在する場合は既存のウィンドウをアクティブにする
        existingWindow.focus();
    } else {
        // 同じ名前のウィンドウが存在しない場合は新しいウィンドウを開く
        window.open(url, windowName);
    }
}
</script>

</body>
</html>

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0