Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generic search engine menu item #164

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion fixtures/fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ contextMenu({
}
],
append: () => {},
showSelectAll: true,
addSearchWithOther: {
title: 'Wolfram Alpha',
url: 'https://www.wolframalpha.com/input?i=%s'
},
showCopyImageAddress: true,
showSaveImageAs: true,
showCopyVideoAddress: true,
Expand Down
37 changes: 36 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,33 @@ 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 - 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: 'DuckDuckGo',
url: 'https://duckduckgo.com?q=%s'
}
}
```
@example
```
{
addSearchWithOther: {
title: 'Wolfram Alpha',
url: 'https://www.wolframalpha.com/input?i=%s'
}
}
```
*/
readonly addSearchWithOther?: {title: string; url: string};

/**
Show the `Select All` menu item when right-clicking in a window.

Expand Down Expand Up @@ -318,6 +352,7 @@ declare namespace contextMenu {
- `showLearnSpelling`
- `showLookUpSelection`
- `showSearchWithGoogle`
- `addSearchWithOther`
- `showSelectAll`
- `showCopyImage`
- `showCopyImageAddress`
Expand All @@ -331,7 +366,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() {
let url = addSearchWithOtherHasValues ? options.addSearchWithOther.url : '';
url = new URL(url.replace('%s', 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 - The title/name of the search engine.

`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: 'DuckDuckGo',
url: 'https://duckduckgo.com?q=%s'
}
}
```
```js
{
addSearchWithOther: {
title: 'Wolfram Alpha',
url: 'https://www.wolframalpha.com/input?i=%s'
}
}
```

#### 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