You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Chrome扩展设置在聚焦的隐身窗口中打开?

问题

我正在开发一款面向开发者的Chrome扩展,经常需要配合多Chrome实例(包括隐身窗口)使用。扩展有可在设置页管理的配置,期望点击打开设置页时,页面能在当前聚焦的窗口中打开,但目前当聚焦窗口是隐身窗口时,设置页会跳转到任意非隐身实例打开。想知道有没有办法强制在聚焦窗口(哪怕是隐身窗口)打开设置页?技术栈用的是Svelte v1.0.0。

相关代码

manifest.json

{
    "name": "cool-name",
    "version": "1.0.0",
    "description": "good-description",
    "icons": {
        "48": "icons/icon_48.png",
        "128": "icons/icon_128.png"
    },
    "permissions": ["activeTab", "declarativeContent", "storage", "webNavigation", "http://target-webapp-url"],
    "options_ui": {
        "page": "options.html"
    },
    "background": {
        "scripts": ["background.js"],
        "persistent": false
    },
    "page_action": {
        "default_popup": "popup.html"
    },
    "manifest_version": 2
}

Popup.svelte

<div id="header">
    <cx-heading size="medium-large">Heading-String</cx-heading>
    <button id="settingsBtn" on:click={() => chrome.runtime.openOptionsPage()}>
        <svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" fill="black" viewBox="0 0 16 16">
            <path d="some-svg-string"></path>
            <path d="some-svg-string"></path>
        </svg>
    </button>
</div>

已尝试方法

  • 在manifest文件中添加"incognito": "split",但行为无变化。

解决方案

要实现强制在当前聚焦窗口(包括隐身窗口)打开设置页,不能直接依赖chrome.runtime.openOptionsPage()(该API默认会避开隐身窗口),改用手动创建标签页的方式即可,步骤如下:

  1. 更新manifest权限
    添加"tabs"权限,用于操作浏览器标签页。修改后的权限数组:
"permissions": ["activeTab", "declarativeContent", "storage", "webNavigation", "http://target-webapp-url", "tabs"]
  1. 修改Popup.svelte的点击事件逻辑
    替换原有的chrome.runtime.openOptionsPage(),改为获取当前活动窗口ID,然后在该窗口内创建/激活设置页标签:
<div id="header">
    <cx-heading size="medium-large">Heading-String</cx-heading>
    <button id="settingsBtn" on:click={openSettingsInCurrentWindow}>
        <svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" fill="black" viewBox="0 0 16 16">
            <path d="some-svg-string"></path>
            <path d="some-svg-string"></path>
        </svg>
    </button>
</div>

<script>
function openSettingsInCurrentWindow() {
    // 获取当前聚焦的窗口
    chrome.windows.getCurrent({ populate: false }, (currentWindow) => {
        if (chrome.runtime.lastError) {
            console.error(chrome.runtime.lastError);
            return;
        }
        // 构造扩展设置页的绝对URL
        const optionsUrl = chrome.runtime.getURL('options.html');
        // 检查当前窗口是否已打开设置页,避免重复创建
        chrome.tabs.query({ windowId: currentWindow.id, url: optionsUrl }, (tabs) => {
            if (tabs.length > 0) {
                // 已有设置页,直接激活
                chrome.tabs.update(tabs[0].id, { active: true });
            } else {
                // 无设置页,在当前窗口创建新标签
                chrome.tabs.create({ 
                    windowId: currentWindow.id, 
                    url: optionsUrl,
                    active: true 
                });
            }
        });
    });
}
</script>
  1. 保留隐身模式配置
    manifest中继续保留"incognito": "split",该配置让扩展在隐身窗口中拥有独立运行实例,是在隐身窗口内打开设置页的必要前提。之前配置无效是因为没有配合手动创建标签页的逻辑。

原理说明

chrome.windows.getCurrent()可以精准获取到当前用户聚焦的窗口(包括隐身窗口),通过chrome.tabs.create()指定windowId参数,就能强制将设置页创建在目标窗口内。同时加入的标签页查询逻辑,能避免重复打开相同的设置页,提升用户体验。

内容的提问来源于stack exchange,提问作者null-point-exceptional

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 19:57:21