From 53a683747d3e065797d4086b8b108ab28416d2d4 Mon Sep 17 00:00:00 2001 From: hns258 Date: Sun, 14 Aug 2022 09:52:06 -0400 Subject: [PATCH 1/3] add generic search engine option Squashed commits: [WIP] Simpler implementation of adding generic "Search with ..." menu item (search engine title and url provided by user). Still needs some tweaking [WIP] Ensure URLs copied from Chrome's available search engines work with this implementation [WIP] Update documentation, renaming option apply fixes indicated by XO Merge branch 'sindresorhus:main' into add-other-search-engine wording tweaks, clean ups to be more consistent simplify more tweaks more tweaks reword use tabs instead of spaces for example (shows up in readme with refined github) properly handle case when addSearchWithOther option is not set add a "control" test where no options are set to ensure default scenario is working Revert "add a "control" test where no options are set to ensure default scenario is working" This reverts commit 39d8512f7959eb305464e2e5c99ca62e86a9f649. fix casing more doc tweaks missed rewording simplify @property items * move length check of addSearchWithOther closer to undefined check (confused when revisiting after awhile) * update ts types file to match readme's property section * fix whitespaces more tweaks fix XO trailing space errors add types for addSearchWithOther properties --- fixtures/fixture.js | 6 +++++- index.d.ts | 38 +++++++++++++++++++++++++++++++++++++- index.js | 14 ++++++++++++++ readme.md | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 2 deletions(-) diff --git a/fixtures/fixture.js b/fixtures/fixture.js index 70185a0..3223608 100644 --- a/fixtures/fixture.js +++ b/fixtures/fixture.js @@ -36,7 +36,11 @@ contextMenu({ } ], append: () => {}, - showSelectAll: true, + addSearchWithOther: + { + title: 'DuckDuckGo', + url: 'https://duckduckgo.com/?q=%s' + }, showCopyImageAddress: true, showSaveImageAs: true, showCopyVideoAddress: true, diff --git a/index.d.ts b/index.d.ts index cef5204..1e1cb2f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -25,6 +25,12 @@ declare namespace contextMenu { */ readonly searchWithGoogle?: string; + /** + The placeholder `{searchEngine}` will be replaced by the `title` property of the `addSearchWithOther` option. + @default 'Search with {searchEngine}' + */ + readonly searchWithOther?: string; + /** @default 'Cut' */ @@ -115,6 +121,7 @@ declare namespace contextMenu { readonly learnSpelling: (options: ActionOptions) => MenuItemConstructorOptions; readonly lookUpSelection: (options: ActionOptions) => MenuItemConstructorOptions; readonly searchWithGoogle: (options: ActionOptions) => MenuItemConstructorOptions; + readonly searchWithOther: (options: ActionOptions) => MenuItemConstructorOptions; readonly cut: (options: ActionOptions) => MenuItemConstructorOptions; readonly copy: (options: ActionOptions) => MenuItemConstructorOptions; readonly paste: (options: ActionOptions) => MenuItemConstructorOptions; @@ -185,6 +192,34 @@ declare namespace contextMenu { */ readonly showSearchWithGoogle?: boolean; + /** + Add a `Search with {searchEngine}` menu item when right-clicking text. + + This allows the use of a search engine besides Google (e.g., Bing and DuckDuckGo). The `title` and `url` of the desired search engine need to be provided. The `{searchEngine}` placeholder will be replaced by `title`. + @property {string} title Title/name of search engine + @property {string} url URL of search engine (with query syntax used in Chrome) + + @example + ``` + { + addSearchWithOther: { + title: 'Bing', + url: 'https://www.bing.com/search' + } + }; + ``` + @example + ``` + { + addSearchWithOther: { + title: 'DuckDuckGo', + url: 'https://duckduckgo.com' + } + }; + ``` + */ + readonly addSearchWithOther?: {title: string; url: string}; + /** Show the `Select All` menu item when right-clicking in a window. @@ -318,6 +353,7 @@ declare namespace contextMenu { - `showLearnSpelling` - `showLookUpSelection` - `showSearchWithGoogle` + - `addSearchWithOther` - `showSelectAll` - `showCopyImage` - `showCopyImageAddress` @@ -331,7 +367,7 @@ declare namespace contextMenu { To get spellchecking, “Correct Automatically”, and “Learn Spelling” in the menu, please enable the `spellcheck` preference in browser window: `new BrowserWindow({webPreferences: {spellcheck: true}})` - @default [...dictionarySuggestions, defaultActions.separator(), defaultActions.separator(), defaultActions.learnSpelling(), defaultActions.separator(), defaultActions.lookUpSelection(), defaultActions.separator(),defaultActions.searchWithGoogle(), defaultActions.cut(), defaultActions.copy(), defaultActions.paste(), defaultActions.selectAll(), defaultActions.separator(), defaultActions.saveImage(), defaultActions.saveImageAs(), defaultActions.saveVideo(), defaultActions.saveVideoAs(), defaultActions.copyLink(), defaultActions.copyImage(), defaultActions.copyImageAddress(), defaultActions.separator(), defaultActions.copyLink(), defaultActions.saveLinkAs(), defaultActions.separator(), defaultActions.inspect()] + @default [...dictionarySuggestions, defaultActions.separator(), defaultActions.separator(), defaultActions.learnSpelling(), defaultActions.separator(), defaultActions.lookUpSelection(), defaultActions.separator(),defaultActions.searchWithGoogle(), defaultActions.searchWithOther(), defaultActions.cut(), defaultActions.copy(), defaultActions.paste(), defaultActions.selectAll(), defaultActions.separator(), defaultActions.saveImage(), defaultActions.saveImageAs(), defaultActions.saveVideo(), defaultActions.saveVideoAs(), defaultActions.copyLink(), defaultActions.copyImage(), defaultActions.copyImageAddress(), defaultActions.separator(), defaultActions.copyLink(), defaultActions.saveLinkAs(), defaultActions.separator(), defaultActions.inspect()] */ readonly menu?: ( defaultActions: Actions, diff --git a/index.js b/index.js index 0137cec..67040e2 100644 --- a/index.js +++ b/index.js @@ -39,6 +39,9 @@ const create = (win, options) => { const isLink = Boolean(props.linkURL); const can = type => editFlags[`can${type}`] && hasText; + const addSearchWithOtherExists = typeof options.addSearchWithOther !== 'undefined' && 'title' in options.addSearchWithOther && 'url' in options.addSearchWithOther; + const addSearchWithOtherHasValues = addSearchWithOtherExists && options.addSearchWithOther.title.trim().length > 0 && options.addSearchWithOther.url.trim().length > 0; + const defaultActions = { separator: () => ({type: 'separator'}), learnSpelling: decorateMenuItem({ @@ -70,6 +73,16 @@ const create = (win, options) => { electron.shell.openExternal(url.toString()); } }), + searchWithOther: decorateMenuItem({ + id: 'searchWithOther', + label: `Search &with ${addSearchWithOtherHasValues ? options.addSearchWithOther.title : ''}`, + visible: hasText, + click() { + const url = new URL(addSearchWithOtherHasValues ? options.addSearchWithOther.url : ''); + url.searchParams.set('q', props.selectionText); + electron.shell.openExternal(url.toString()); + } + }), cut: decorateMenuItem({ id: 'cut', label: 'Cu&t', @@ -275,6 +288,7 @@ const create = (win, options) => { options.showLookUpSelection !== false && defaultActions.lookUpSelection(), defaultActions.separator(), options.showSearchWithGoogle !== false && defaultActions.searchWithGoogle(), + addSearchWithOtherHasValues && defaultActions.searchWithOther(), defaultActions.separator(), defaultActions.cut(), defaultActions.copy(), diff --git a/readme.md b/readme.md index a9da98a..941a87e 100644 --- a/readme.md +++ b/readme.md @@ -143,6 +143,39 @@ Default: `true` Show the `Search with Google` menu item when right-clicking text. +#### addSearchWithOther + +Type: `object: {title: string; url: string}`\ +Default: `{}` + +Add a `Search with {searchEngine}` menu item when right-clicking text. + +This allows the use of a search engine besides Google (e.g., Bing and DuckDuckGo). The `title` and `url` of the desired search engine need to be provided. The `{searchEngine}` placeholder will be replaced by `title`. + +Properties: + +`title` - string - Title/name of search engine + +`url` - string - URL of search engine (with query syntax used in Chrome) + +Examples: +```js +{ + addSearchWithOther: { + title: 'Bing', + url: 'https://www.bing.com/search' + } +}; +``` +```js +{ + addSearchWithOther: { + title: 'DuckDuckGo', + url: 'https://duckduckgo.com' + } +}; +``` + #### showSelectAll Type: `boolean`\ @@ -278,6 +311,7 @@ The following options are ignored when `menu` is used: - `showLookUpSelection` - `showSearchWithGoogle` +- `addSearchWithOther` - `showSelectAll` - `showCopyImage` - `showCopyImageAddress` @@ -296,6 +330,7 @@ Default actions: - `separator` - `lookUpSelection` - `searchWithGoogle` +- `searchWithOther` - `cut` - `copy` - `paste` From 6d805561340f6df60a908bca69c5d0e5bb8831ee Mon Sep 17 00:00:00 2001 From: hns258 Date: Thu, 22 Sep 2022 10:13:13 -0400 Subject: [PATCH 2/3] just replace `%s` (query field) since not all search engines use `q` param --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 67040e2..abbb615 100644 --- a/index.js +++ b/index.js @@ -78,8 +78,8 @@ const create = (win, options) => { label: `Search &with ${addSearchWithOtherHasValues ? options.addSearchWithOther.title : ''}`, visible: hasText, click() { - const url = new URL(addSearchWithOtherHasValues ? options.addSearchWithOther.url : ''); - url.searchParams.set('q', props.selectionText); + let url = addSearchWithOtherHasValues ? options.addSearchWithOther.url : ''; + url = new URL(url.replace('%s', props.selectionText)); electron.shell.openExternal(url.toString()); } }), From e934d0ee7d5d47e3c9d876d1881ee8598fb0e09c Mon Sep 17 00:00:00 2001 From: hns258 Date: Thu, 27 Oct 2022 10:27:15 -0400 Subject: [PATCH 3/3] clean up, tweaks --- fixtures/fixture.js | 7 +++---- index.d.ts | 17 ++++++++--------- readme.md | 16 ++++++++-------- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/fixtures/fixture.js b/fixtures/fixture.js index 3223608..84dac19 100644 --- a/fixtures/fixture.js +++ b/fixtures/fixture.js @@ -36,10 +36,9 @@ contextMenu({ } ], append: () => {}, - addSearchWithOther: - { - title: 'DuckDuckGo', - url: 'https://duckduckgo.com/?q=%s' + addSearchWithOther: { + title: 'Wolfram Alpha', + url: 'https://www.wolframalpha.com/input?i=%s' }, showCopyImageAddress: true, showSaveImageAs: true, diff --git a/index.d.ts b/index.d.ts index 1e1cb2f..bdfe88c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -196,26 +196,25 @@ declare namespace contextMenu { Add a `Search with {searchEngine}` menu item when right-clicking text. This allows the use of a search engine besides Google (e.g., Bing and DuckDuckGo). The `title` and `url` of the desired search engine need to be provided. The `{searchEngine}` placeholder will be replaced by `title`. - @property {string} title Title/name of search engine - @property {string} url URL of search engine (with query syntax used in Chrome) - + @property {string} title - The title/name of the search engine. + @property {string} url - The search engine's results [URL with `%s` in place of query](https://support.google.com/chrome/answer/95426#ts&zippy=%2Curl-with-s-in-place-of-query-field) (syntax used in Chrome). @example ``` { addSearchWithOther: { - title: 'Bing', - url: 'https://www.bing.com/search' + title: 'DuckDuckGo', + url: 'https://duckduckgo.com?q=%s' } - }; + } ``` @example ``` { addSearchWithOther: { - title: 'DuckDuckGo', - url: 'https://duckduckgo.com' + title: 'Wolfram Alpha', + url: 'https://www.wolframalpha.com/input?i=%s' } - }; + } ``` */ readonly addSearchWithOther?: {title: string; url: string}; diff --git a/readme.md b/readme.md index 941a87e..38db5e3 100644 --- a/readme.md +++ b/readme.md @@ -154,26 +154,26 @@ This allows the use of a search engine besides Google (e.g., Bing and DuckDuckGo Properties: -`title` - string - Title/name of search engine +`title` - string - The title/name of the search engine. -`url` - string - URL of search engine (with query syntax used in Chrome) +`url` - string - The search engine's results [URL with `%s` in place of query](https://support.google.com/chrome/answer/95426#ts&zippy=%2Curl-with-s-in-place-of-query-field) (syntax used in Chrome). Examples: ```js { addSearchWithOther: { - title: 'Bing', - url: 'https://www.bing.com/search' + title: 'DuckDuckGo', + url: 'https://duckduckgo.com?q=%s' } -}; +} ``` ```js { addSearchWithOther: { - title: 'DuckDuckGo', - url: 'https://duckduckgo.com' + title: 'Wolfram Alpha', + url: 'https://www.wolframalpha.com/input?i=%s' } -}; +} ``` #### showSelectAll