首页 > 解决方案 > 为 Firefox 扩展设置弹出窗口标题

问题描述

我正在构建一个可以在多个浏览器中运行的浏览器扩展。单击浏览器操作时,会创建一个显示扩展页面的弹出窗口。Chrome 使用弹出页面的标题标签作为弹出窗口的标题,但 Firefox 没有。

相反,我得到以下信息:

在此处输入图像描述

为什么 Firefox 不使用页面标题?为了修复它,我在 windows.create 回调中设置了窗口标题,但这也不起作用。

这是打开弹出窗口的 background.js 脚本。

'use strict';

let browser = (function () {
    return window.msBrowser ||
        window.browser ||
        window.chrome;
})();

var popupWindowId = false;

browser.browserAction.onClicked.addListener(function() {
    // Open the popup if not already open. If open, focus on it.
    if(popupWindowId === false) {
        popupWindowId = true;
        browser.windows.create({
            'url': 'index.html',
            'type': 'popup',
            'height': 525,
            'width': 350
        }, function(win) {
            win.title = 'My Title';
            popupWindowId = win.id;
            firefoxWorkaroundForBlankPanel();
        });

        return;
    } else if(typeof popupWindowId === 'number'){
        // The window is open, and the user clicked the button.
        // Focus the window.
        browser.windows.update(popupWindowId, { focused: true });
    }
});

browser.windows.onRemoved.addListener(function (winId){
    if(popupWindowId === winId){
        popupWindowId = false;
    }
});

// workaround for bug https://bugzilla.mozilla.org/show_bug.cgi?id=1425829
// bug causes popup to appear blank until resized
async function firefoxWorkaroundForBlankPanel () {
    const {id, width, height} = await browser.windows.getCurrent();
    browser.windows.update(id, {
        height: height + 1
    });
}

标签: javascriptfirefoxfirefox-addon-webextensions

解决方案


不幸的是,您无法从弹出窗口标题中删除扩展 URL。根据这个Mozilla 错误报告讨论,这是故意的,以防止恶意扩展的欺骗/网络钓鱼尝试(例如,通过欺骗用户扩展弹出窗口是浏览器 UI 的一部分)。但至少他们可能会将其更改为在窗口标题中显示“扩展 [扩展名称]”,而不是丑陋的扩展 URL。


推荐阅读