Skip to content

Commit

Permalink
add generic search engine option
Browse files Browse the repository at this point in the history
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 39d8512.

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
  • Loading branch information
hns258 committed Sep 11, 2022
1 parent 27f85a1 commit 53a6837
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
6 changes: 5 additions & 1 deletion fixtures/fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ contextMenu({
}
],
append: () => {},
showSelectAll: true,
addSearchWithOther:
{
title: 'DuckDuckGo',
url: 'https://duckduckgo.com/?q=%s'
},
showCopyImageAddress: true,
showSaveImageAs: true,
showCopyVideoAddress: true,
Expand Down
38 changes: 37 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
*/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -318,6 +353,7 @@ declare namespace contextMenu {
- `showLearnSpelling`
- `showLookUpSelection`
- `showSearchWithGoogle`
- `addSearchWithOther`
- `showSelectAll`
- `showCopyImage`
- `showCopyImageAddress`
Expand All @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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(),
Expand Down
35 changes: 35 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`\
Expand Down Expand Up @@ -278,6 +311,7 @@ The following options are ignored when `menu` is used:

- `showLookUpSelection`
- `showSearchWithGoogle`
- `addSearchWithOther`
- `showSelectAll`
- `showCopyImage`
- `showCopyImageAddress`
Expand All @@ -296,6 +330,7 @@ Default actions:
- `separator`
- `lookUpSelection`
- `searchWithGoogle`
- `searchWithOther`
- `cut`
- `copy`
- `paste`
Expand Down

0 comments on commit 53a6837

Please sign in to comment.