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 1 commit
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
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)
hns258 marked this conversation as resolved.
Show resolved Hide resolved

@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);
hns258 marked this conversation as resolved.
Show resolved Hide resolved
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