控制Chrome / Firefox扩展程序中的菜单项状态

时间:2018-01-31 12:00:56

标签: google-chrome-extension firefox-webextensions

我的Chrome和Firefox网络扩展程序需要控制其创建的上下文菜单项的已检查状态。这适用于当前的Firefox(57,在Windows上),但Chrome(64)似乎接管完全控制。在Chrome中,菜单项的选中状态会在用户点击时切换,但从我的附加代码设置选中状态无效。

我将其缩小到以下示例(背景页面,使用" contextMenus"权限):

// prepare a bunch of menu items...
const items = [];
for (let i=0; i<5; i++) {
    items.push(`test-item-${i}`)
}
// ...they will all change state in sync...
let allChecked = true;
// ...create the menu
buildContextMenuItems();

// now clicking any item should check / uncheck all items.
// Works in Firefox, does not work in Chrome.

function buildContextMenuItems() {
    for (let item of items) {
        chrome.contextMenus.create({
            id: item,
            title: item,
            contexts: ['all'],
            onclick: handleItemClick,
            type: 'checkbox',
            checked: allChecked // initially: true
        });
    }
}

function handleItemClick(info, tab) {
    allChecked = !allChecked;
    console.log('wait a bit before seeing if it worked!')
    // use setTimeout in case the browser needs time to update menu state
    setTimeout(function() {
        console.log('toggling all items to', allChecked);
        for (let item of items) {
            chrome.contextMenus.update(item, {checked: allChecked});
        }
    }, 1000);
}

简而言之,在chrome.contextMenus.update()之后,检查状态在Firefox中是预期的,而在Chrome中,只有实际点击的项目切换状态。 (setTimeout中没有等待的数量有任何区别)。

我是以错误的方式解决这个问题吗?

0 个答案:

没有答案
相关问题