2021-01-01 14:27:58 +00:00
|
|
|
/* global browserCommands */// background.js
|
|
|
|
/* global msg */
|
|
|
|
/* global prefs */
|
2022-02-24 03:49:06 +00:00
|
|
|
/* global CHROME URLS ignoreChromeError */// toolbox.js
|
2021-01-01 14:27:58 +00:00
|
|
|
'use strict';
|
|
|
|
|
2022-02-24 03:49:06 +00:00
|
|
|
chrome.management.getSelf(ext => {
|
|
|
|
const contextMenus = Object.assign({
|
2021-01-01 14:27:58 +00:00
|
|
|
'show-badge': {
|
|
|
|
title: 'menuShowBadge',
|
2022-02-24 03:49:06 +00:00
|
|
|
click: togglePref,
|
2021-01-01 14:27:58 +00:00
|
|
|
},
|
|
|
|
'disableAll': {
|
|
|
|
title: 'disableAllStyles',
|
|
|
|
click: browserCommands.styleDisableAll,
|
|
|
|
},
|
|
|
|
'open-manager': {
|
2022-02-24 03:49:06 +00:00
|
|
|
title: 'optionsOpenManager',
|
2021-01-01 14:27:58 +00:00
|
|
|
click: browserCommands.openManage,
|
|
|
|
},
|
|
|
|
'open-options': {
|
|
|
|
title: 'openOptions',
|
|
|
|
click: browserCommands.openOptions,
|
|
|
|
},
|
2022-02-24 03:49:06 +00:00
|
|
|
}, ext.installType === 'development' && {
|
2021-01-01 14:27:58 +00:00
|
|
|
'reload': {
|
|
|
|
title: 'reload',
|
|
|
|
click: browserCommands.reload,
|
|
|
|
},
|
2022-02-24 03:49:06 +00:00
|
|
|
}, CHROME && {
|
2021-01-01 14:27:58 +00:00
|
|
|
'editor.contextDelete': {
|
|
|
|
title: 'editDeleteText',
|
|
|
|
type: 'normal',
|
|
|
|
contexts: ['editable'],
|
2022-01-28 00:11:25 +00:00
|
|
|
documentUrlPatterns: [URLS.ownOrigin + '*'],
|
2021-01-01 14:27:58 +00:00
|
|
|
click: (info, tab) => {
|
|
|
|
msg.sendTab(tab.id, {method: 'editDeleteText'}, undefined, 'extension')
|
|
|
|
.catch(msg.ignoreError);
|
|
|
|
},
|
|
|
|
},
|
2022-02-24 03:49:06 +00:00
|
|
|
});
|
2021-01-01 14:27:58 +00:00
|
|
|
|
2022-02-24 03:49:06 +00:00
|
|
|
createContextMenus(Object.keys(contextMenus));
|
2021-01-01 14:27:58 +00:00
|
|
|
chrome.contextMenus.onClicked.addListener((info, tab) =>
|
|
|
|
contextMenus[info.menuItemId].click(info, tab));
|
|
|
|
|
2022-02-24 03:49:06 +00:00
|
|
|
function createContextMenus(ids) {
|
2021-01-01 14:27:58 +00:00
|
|
|
for (const id of ids) {
|
2022-02-24 03:49:06 +00:00
|
|
|
const item = Object.assign({id, contexts: ['browser_action']}, contextMenus[id]);
|
2021-01-01 14:27:58 +00:00
|
|
|
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);
|
|
|
|
}
|
2021-01-01 14:27:58 +00:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-01-01 14:27:58 +00:00
|
|
|
function togglePresence(id, checked) {
|
|
|
|
if (checked) {
|
|
|
|
createContextMenus([id]);
|
|
|
|
} else {
|
|
|
|
chrome.contextMenus.remove(id, ignoreChromeError);
|
|
|
|
}
|
|
|
|
}
|
2022-02-24 03:49:06 +00:00
|
|
|
});
|