diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 71a79aa3..32fc1343 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -683,6 +683,9 @@ "optionsAdvancedExposeIframesNote": { "message": "Enables writing iframe-specific CSS like 'html[stylus-iframe] h1 { display:none }'" }, + "optionsAdvancedContextDelete": { + "message": "Add 'Delete' in editor context menu" + }, "optionsActions": { "message": "Actions" }, diff --git a/background.js b/background.js index e0367f3e..2f70e159 100644 --- a/background.js +++ b/background.js @@ -99,16 +99,10 @@ contextMenus = Object.assign({ title: 'openStylesManager', click: browserCommands.openManage, }, -}, - // detect browsers without Delete by looking at the end of UA string - /Vivaldi\/[\d.]+$/.test(navigator.userAgent) || - // Chrome and co. - /Safari\/[\d.]+$/.test(navigator.userAgent) && - // skip forks with Flash as those are likely to have the menu e.g. CentBrowser - !Array.from(navigator.plugins).some(p => p.name == 'Shockwave Flash') -&& { - 'editDeleteText': { +}, prefs.get('editor.contextDelete') && { + 'editor.contextDelete': { title: 'editDeleteText', + type: 'normal', contexts: ['editable'], documentUrlPatterns: [URLS.ownOrigin + 'edit*'], click: (info, tab) => { @@ -117,26 +111,37 @@ contextMenus = Object.assign({ } }); -for (const id of Object.keys(contextMenus)) { - const item = Object.assign({id}, contextMenus[id]); - const prefValue = prefs.readOnlyValues[id]; - const isBoolean = typeof prefValue == 'boolean'; - item.title = chrome.i18n.getMessage(item.title); - if (isBoolean) { - item.type = 'checkbox'; - item.checked = prefValue; - } - if (!item.contexts) { - item.contexts = ['browser_action']; - } - delete item.click; - chrome.contextMenus.create(item, ignoreChromeError); +{ + const createContextMenus = (ids = Object.keys(contextMenus)) => { + for (const id of ids) { + const item = Object.assign({id}, contextMenus[id]); + const prefValue = prefs.readOnlyValues[id]; + item.title = chrome.i18n.getMessage(item.title); + if (!item.type && typeof prefValue == 'boolean') { + item.type = 'checkbox'; + item.checked = prefValue; + } + if (!item.contexts) { + item.contexts = ['browser_action']; + } + delete item.click; + chrome.contextMenus.create(item, ignoreChromeError); + } + }; + createContextMenus(); + prefs.subscribe((id, checked) => { + if (id == 'editor.contextDelete') { + if (checked) { + createContextMenus([id]); + } else { + chrome.contextMenus.remove(id, ignoreChromeError); + } + } else { + chrome.contextMenus.update(id, {checked}, ignoreChromeError); + } + }, Object.keys(contextMenus).filter(key => typeof prefs.readOnlyValues[key] == 'boolean')); } -prefs.subscribe((id, checked) => { - chrome.contextMenus.update(id, {checked}, ignoreChromeError); -}, Object.keys(contextMenus)); - // ************************************************************************* // [re]inject content scripts { diff --git a/edit.js b/edit.js index 451e45a0..b9575c18 100644 --- a/edit.js +++ b/edit.js @@ -1234,8 +1234,8 @@ function initHooks() { function toggleContextMenuDelete(event) { - if (event.button == 2) { - chrome.contextMenus.update('editDeleteText', { + if (event.button == 2 && prefs.get('editor.contextDelete')) { + chrome.contextMenus.update('editor.contextDelete', { enabled: Boolean( this.selectionStart != this.selectionEnd || this.somethingSelected && this.somethingSelected() diff --git a/options/index.html b/options/index.html index 37be4bba..ff31c16b 100644 --- a/options/index.html +++ b/options/index.html @@ -71,6 +71,13 @@ + diff --git a/prefs.js b/prefs.js index ef4a89cd..c632d1e9 100644 --- a/prefs.js +++ b/prefs.js @@ -44,6 +44,7 @@ var prefs = new function Prefs() { 'editor.matchHighlight': 'token', // token = token/word under cursor even if nothing is selected // selection = only when something is selected // '' (empty string) = disabled + 'editor.contextDelete': contextDeleteMissing(), // "Delete" item in context menu 'badgeDisabled': '#8B0000', // badge background color when disabled 'badgeNormal': '#006666', // badge background color @@ -301,6 +302,17 @@ var prefs = new function Prefs() { } return true; } + + function contextDeleteMissing() { + return ( + // detect browsers without Delete by looking at the end of UA string + /Vivaldi\/[\d.]+$/.test(navigator.userAgent) || + // Chrome and co. + /Safari\/[\d.]+$/.test(navigator.userAgent) && + // skip forks with Flash as those are likely to have the menu e.g. CentBrowser + !Array.from(navigator.plugins).some(p => p.name == 'Shockwave Flash') + ); + } }();