stylus/background/context-menus.js

88 lines
2.5 KiB
JavaScript
Raw Permalink Normal View History

/* global browserCommands */// background.js
/* global msg */
/* global prefs */
2022-02-24 03:49:06 +00:00
/* global CHROME URLS ignoreChromeError */// toolbox.js
'use strict';
2022-02-24 03:49:06 +00:00
chrome.management.getSelf(ext => {
const contextMenus = Object.assign({
'show-badge': {
title: 'menuShowBadge',
2022-02-24 03:49:06 +00:00
click: togglePref,
},
'disableAll': {
title: 'disableAllStyles',
click: browserCommands.styleDisableAll,
},
'open-manager': {
2022-02-24 03:49:06 +00:00
title: 'optionsOpenManager',
click: browserCommands.openManage,
},
'open-options': {
title: 'openOptions',
click: browserCommands.openOptions,
},
2022-02-24 03:49:06 +00:00
}, ext.installType === 'development' && {
'reload': {
title: 'reload',
click: browserCommands.reload,
},
2022-02-24 03:49:06 +00:00
}, CHROME && {
'editor.contextDelete': {
title: 'editDeleteText',
type: 'normal',
contexts: ['editable'],
documentUrlPatterns: [URLS.ownOrigin + '*'],
click: (info, tab) => {
msg.sendTab(tab.id, {method: 'editDeleteText'}, undefined, 'extension')
.catch(msg.ignoreError);
},
},
2022-02-24 03:49:06 +00:00
});
2022-02-24 03:49:06 +00:00
createContextMenus(Object.keys(contextMenus));
chrome.contextMenus.onClicked.addListener((info, tab) =>
contextMenus[info.menuItemId].click(info, tab));
2022-02-24 03:49:06 +00:00
function createContextMenus(ids) {
for (const id of ids) {
2022-02-24 03:49:06 +00:00
const item = Object.assign({id, contexts: ['browser_action']}, contextMenus[id]);
item.title = chrome.i18n.getMessage(item.title);
2022-02-24 03:49:06 +00:00
if (typeof prefs.defaults[id] === 'boolean') {
if (item.type) {
prefs.subscribe(id, togglePresence);
} else {
item.type = 'checkbox';
item.checked = prefs.get(id);
prefs.subscribe(id, CHROME >= 62 && CHROME <= 64 ? toggleCheckmarkBugged : toggleCheckmark);
}
}
delete item.click;
chrome.contextMenus.create(item, ignoreChromeError);
}
}
function toggleCheckmark(id, checked) {
chrome.contextMenus.update(id, {checked}, ignoreChromeError);
}
/** Circumvents the bug with disabling check marks in Chrome 62-64 */
async function toggleCheckmarkBugged(id) {
await browser.contextMenus.remove(id).catch(ignoreChromeError);
createContextMenus([id]);
}
2022-02-24 03:49:06 +00:00
/** @param {chrome.contextMenus.OnClickData} info */
function togglePref(info) {
prefs.set(info.menuItemId, info.checked);
}
function togglePresence(id, checked) {
if (checked) {
createContextMenus([id]);
} else {
chrome.contextMenus.remove(id, ignoreChromeError);
}
}
2022-02-24 03:49:06 +00:00
});