# Class: FrontendScriptApi

## FrontendScriptApi

Source:
  - services/frontend_script_api.js, line 23

### Members

#### $container

##### Properties:

Name | Type | Description
--- | --- | ---
`container` | jQuery | of all the rendered script content

Source:
  - services/frontend_script_api.js, line 27

#### BasicWidget

##### Properties:

Type | Description
--- | ---
BasicWidget |

Source:
  - services/frontend_script_api.js, line 61

#### CollapsibleWidget

##### Properties:

Type | Description
--- | ---
CollapsibleWidget |

Source:
  - services/frontend_script_api.js, line 40

#### currentNote

##### Properties:

Name | Type | Description
--- | --- | ---
`note` | object | where script is currently executing

Source:
  - services/frontend_script_api.js, line 32

#### NoteContextAwareWidget

##### Properties:

Type | Description
--- | ---
NoteContextAwareWidget |

Source:
  - services/frontend_script_api.js, line 49

#### NoteContextCachingWidget

##### Properties:

Type | Description
--- | ---
NoteContextAwareWidget |

Source:
  - services/frontend_script_api.js, line 58

#### originEntity

##### Properties:

Name | Type | Description
--- | --- | ---
`entity` | object | null | whose event triggered this execution

Source:
  - services/frontend_script_api.js, line 34

#### startNote

##### Properties:

Name | Type | Description
--- | --- | ---
`note` | object | where script started executing

Source:
  - services/frontend_script_api.js, line 30

#### TabAwareWidget

##### Properties:

Type | Description
--- | ---
NoteContextAwareWidget |

Deprecated:
  - use NoteContextAwareWidget instead
Source:
  - services/frontend_script_api.js, line 46

#### TabCachingWidget

##### Properties:

Type | Description
--- | ---
NoteContextCachingWidget |

Deprecated:
  - use NoteContextCachingWidget instead
Source:
  - services/frontend_script_api.js, line 55

### Methods

#### activateNewNote(notePath) → {Promise.<void>}

Activates newly created note. Compared to this.activateNote() also makes sure that frontend has been fully synced.

##### Parameters:

Name | Type | Description
--- | --- | ---
`notePath` | string | (or noteId)

Source:
  - services/frontend_script_api.js, line 80

##### Returns:

Type
  Promise.<void>

#### activateNote(notePath) → {Promise.<void>}

Activates note in the tree and in the note detail.

##### Parameters:

Name | Type | Description
--- | --- | ---
`notePath` | string | (or noteId)

Source:
  - services/frontend_script_api.js, line 70

##### Returns:

Type
  Promise.<void>

#### addButtonToToolbar(opts)

Adds new button the the plugin area.

##### Parameters:

Name | Type | Description
--- | --- | ---
`opts` | ToolbarButtonOptions |

Source:
  - services/frontend_script_api.js, line 137

#### addTextToActiveContextEditor(text)

Adds given text to the editor cursor

##### Parameters:

Name | Type | Description
--- | --- | ---
`text` | string | this must be clear text, HTML is not supported.

Source:
  - services/frontend_script_api.js, line 378

#### addTextToActiveTabEditor(text)

Adds given text to the editor cursor

##### Parameters:

Name | Type | Description
--- | --- | ---
`text` | string | this must be clear text, HTML is not supported.

Deprecated:
  - use addTextToActiveContextEditor() instead
Source:
  - services/frontend_script_api.js, line 366

#### bindGlobalShortcut(keyboardShortcut, handler)

##### Parameters:

Name | Type | Description
--- | --- | ---
`keyboardShortcut` | string | e.g. "ctrl+shift+a"
`handler` | function |

Source:
  - services/frontend_script_api.js, line 569

#### createNoteLink(notePath, paramsopt, title=opt)

Create note link (jQuery object) for given note.

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`notePath` | string |  | (or noteId)
`params` | object | <optional> | ###### Properties
Name | Type | Attributes | Default | Description
--- | --- | --- | --- | ---
`showTooltip` | boolean | <optional> | true | enable/disable tooltip on the link
`showNotePath` | boolean | <optional> | false | show also whole note's path as part of the link
`showNoteIcon` | boolean | <optional> | false | show also note icon before the title
`title=` | string | <optional> | custom link tile with note's title as default

Source:
  - services/frontend_script_api.js, line 357

#### formatDateISO(date) → {string}

##### Parameters:

Name | Type | Description
--- | --- | ---
`date` | Date |

Source:
  - services/frontend_script_api.js, line 297

##### Returns:

date in YYYY-MM-DD format

Type
  string

#### getActiveContextCodeEditor() → {Promise.<CodeMirror>}

See https://codemirror.net/doc/manual.html#api

Source:
  - services/frontend_script_api.js, line 424

##### Returns:

instance of CodeMirror

Type
  Promise.<CodeMirror>

#### getActiveContextNote() → {NoteShort}

Source:
  - services/frontend_script_api.js, line 395

##### Returns:

active note (loaded into right pane)

Type
  NoteShort

#### getActiveContextNotePath() → {Promise.<(string|null)>}

Source:
  - services/frontend_script_api.js, line 450

##### Returns:

returns note path of active note or null if there isn't active note

Type
  Promise.<(string|null)>

#### getActiveContextTextEditor() → {Promise.<CKEditor>}

See https://ckeditor.com/docs/ckeditor5/latest/api/module_core_editor_editor-Editor.html for a documentation on the returned instance.

Source:
  - services/frontend_script_api.js, line 416

##### Returns:

instance of CKEditor

Type
  Promise.<CKEditor>

#### getActiveNoteDetailWidget() → {Promise.<NoteDetailWidget>}

Get access to the widget handling note detail. Methods like `getWidgetType()` and `getTypeWidget()` to get to the implementation of actual widget type.

Source:
  - services/frontend_script_api.js, line 433

##### Returns:

Type
  Promise.<NoteDetailWidget>

#### getActiveTabNote() → {NoteShort}

Deprecated:
  - use getActiveContextNote() instead
Source:
  - services/frontend_script_api.js, line 385

##### Returns:

active note (loaded into right pane)

Type
  NoteShort

#### getActiveTabNotePath() → {Promise.<(string|null)>}

Deprecated:
  - use getActiveContextNotePath() instead
Source:
  - services/frontend_script_api.js, line 440

##### Returns:

returns note path of active note or null if there isn't active note

Type
  Promise.<(string|null)>

#### getActiveTabTextEditor(callbackopt)

See https://ckeditor.com/docs/ckeditor5/latest/api/module_core_editor_editor-Editor.html for a documentation on the returned instance.

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`callback` |  | <optional> | callback receiving "textEditor" instance

Deprecated:
  - use getActiveContextTextEditor()
Source:
  - services/frontend_script_api.js, line 404

#### getComponentByEl(el) → {Component}

Returns component which owns given DOM element (the nearest parent component in DOM tree)

##### Parameters:

Name | Type | Description
--- | --- | ---
`el` | Element | DOM element

Source:
  - services/frontend_script_api.js, line 459

##### Returns:

Type
  Component

#### getDateNote(date) → {Promise.<NoteShort>}

Returns day note for a given date. If it doesn't exist, it is automatically created.

##### Parameters:

Name | Type | Description
--- | --- | ---
`date` | string | e.g. "2019-04-29"

Deprecated:
  - use getDayNote instead
Source:
  - services/frontend_script_api.js, line 511

##### Returns:

Type
  Promise.<NoteShort>

#### getDayNote(date) → {Promise.<NoteShort>}

Returns day note for a given date. If it doesn't exist, it is automatically created.

##### Parameters:

Name | Type | Description
--- | --- | ---
`date` | string | e.g. "2019-04-29"

Source:
  - services/frontend_script_api.js, line 520

##### Returns:

Type
  Promise.<NoteShort>

#### getInstanceName() → {string}

Instance name identifies particular Trilium instance. It can be useful for scripts if some action needs to happen on only one specific instance.

Source:
  - services/frontend_script_api.js, line 290

##### Returns:

Type
  string

#### getMonthNote(month) → {Promise.<NoteShort>}

Returns month-note. If it doesn't exist, it is automatically created.

##### Parameters:

Name | Type | Description
--- | --- | ---
`month` | string | e.g. "2019-04"

Source:
  - services/frontend_script_api.js, line 538

##### Returns:

Type
  Promise.<NoteShort>

#### getNote(noteId) → {Promise.<NoteShort>}

Returns note by given noteId. If note is missing from cache, it's loaded. *

##### Parameters:

Name | Type | Description
--- | --- | ---
`noteId` | string |

Source:
  - services/frontend_script_api.js, line 262

##### Returns:

Type
  Promise.<NoteShort>

#### getNotes(noteIds, silentNotFoundErroropt) → {Promise.<Array.<NoteShort>>}

Returns list of notes. If note is missing from cache, it's loaded. This is often used to bulk-fill the cache with notes which would have to be picked one by one otherwise (by e.g. createNoteLink())

##### Parameters:

Name | Type | Attributes | Description
--- | --- | --- | ---
`noteIds` | Array.<string> |  |
`silentNotFoundError` | boolean | <optional> | don't report error if the note is not found

Source:
  - services/frontend_script_api.js, line 274

##### Returns:

Type
  Promise.<Array.<NoteShort>>

#### getTodayNote() → {Promise.<NoteShort>}

Returns date-note for today. If it doesn't exist, it is automatically created.

Source:
  - services/frontend_script_api.js, line 501

##### Returns:

Type
  Promise.<NoteShort>

#### getWeekNote(date) → {Promise.<NoteShort>}

Returns day note for the first date of the week of the given date. If it doesn't exist, it is automatically created.

##### Parameters:

Name | Type | Description
--- | --- | ---
`date` | string | e.g. "2019-04-29"

Source:
  - services/frontend_script_api.js, line 529

##### Returns:

Type
  Promise.<NoteShort>

#### getYearNote(year) → {Promise.<NoteShort>}

Returns year-note. If it doesn't exist, it is automatically created.

##### Parameters:

Name | Type | Description
--- | --- | ---
`year` | string | e.g. "2019"

Source:
  - services/frontend_script_api.js, line 547

##### Returns:

Type
  Promise.<NoteShort>

#### openSplitWithNote(notePath, activate) → {Promise.<void>}

Open a note in a new split.

##### Parameters:

Name | Type | Description
--- | --- | ---
`notePath` | string | (or noteId)
`activate` | boolean | set to true to activate the new split, false to stay on the current split

Source:
  - services/frontend_script_api.js, line 111

##### Returns:

Type
  Promise.<void>

#### openTabWithNote(notePath, activate) → {Promise.<void>}

Open a note in a new tab.

##### Parameters:

Name | Type | Description
--- | --- | ---
`notePath` | string | (or noteId)
`activate` | boolean | set to true to activate the new tab, false to stay on the current tab

Source:
  - services/frontend_script_api.js, line 94

##### Returns:

Type
  Promise.<void>

#### parseDate(str) → {Date}

##### Parameters:

Name | Type | Description
--- | --- | ---
`str` | string |

Source:
  - services/frontend_script_api.js, line 304

##### Returns:

parsed object

Type
  Date

#### protectActiveNote()

Deprecated:
  - use protectNote and protectSubtree instead
Source:
  - services/frontend_script_api.js, line 471

#### protectNote(noteId, protect)

##### Parameters:

Name | Type | Description
--- | --- | ---
`noteId` | string |
`protect` | boolean | true to protect note, false to unprotect

Source:
  - services/frontend_script_api.js, line 482

#### protectSubTree(noteId, protect)

##### Parameters:

Name | Type | Description
--- | --- | ---
`noteId` | string |
`protect` | boolean | true to protect subtree, false to unprotect

Source:
  - services/frontend_script_api.js, line 491

#### randomString(length) → {string}

Return randomly generated string of given length. This random string generation is NOT cryptographically secure.

##### Parameters:

Name | Type | Description
--- | --- | ---
`length` | number | of the string

Source:
  - services/frontend_script_api.js, line 596

##### Returns:

random string

Type
  string

#### refreshIncludedNote(includedNoteId)

This will refresh all currently opened notes which have included note specified in the parameter

##### Parameters:

Name | Type | Description
--- | --- | ---
`includedNoteId` |  | noteId of the included note

Source:
  - services/frontend_script_api.js, line 587

#### refreshTree()

Deprecated:
  - - this is now no-op since all the changes should be gracefully handled per widget
Source:
  - services/frontend_script_api.js, line 344

#### reloadNotes(noteIds)

Update frontend tree (note) cache from the backend.

##### Parameters:

Name | Type | Description
--- | --- | ---
`noteIds` | Array.<string> |

Source:
  - services/frontend_script_api.js, line 282

#### runOnBackend(script, params) → {Promise.<*>}

Executes given anonymous function on the backend. Internally this serializes the anonymous function into string and sends it to backend via AJAX.

##### Parameters:

Name | Type | Description
--- | --- | ---
`script` | string | script to be executed on the backend
`params` | Array.<?> | list of parameters to the anonymous function to be send to backend

Source:
  - services/frontend_script_api.js, line 200

##### Returns:

return value of the executed function on the backend

Type
  Promise.<*>

#### runOnServer()

Deprecated:
  - new name of this API call is runOnBackend so use that
Source:
  - services/frontend_script_api.js, line 228

#### searchForNote(searchString) → {Promise.<(NoteShort|null)>}

This is a powerful search method - you can search by attributes and their values, e.g.: "#dateModified =* MONTH AND #log". See full documentation for all options at: https://github.com/zadam/trilium/wiki/Search

##### Parameters:

Name | Type | Description
--- | --- | ---
`searchString` | string |

Source:
  - services/frontend_script_api.js, line 250

##### Returns:

Type
  Promise.<(NoteShort|null)>

#### searchForNotes(searchString) → {Promise.<Array.<NoteShort>>}

This is a powerful search method - you can search by attributes and their values, e.g.: "#dateModified =* MONTH AND #log". See full documentation for all options at: https://github.com/zadam/trilium/wiki/Search

##### Parameters:

Name | Type | Description
--- | --- | ---
`searchString` | string |

Source:
  - services/frontend_script_api.js, line 238

##### Returns:

Type
  Promise.<Array.<NoteShort>>

#### setHoistedNoteId(noteId) → {Promise}

Hoist note in the current tab. See https://github.com/zadam/trilium/wiki/Note-hoisting

##### Parameters:

Name | Type | Description
--- | --- | ---
`noteId` | string | set hoisted note. 'root' will effectively unhoist

Source:
  - services/frontend_script_api.js, line 556

##### Returns:

Type
  Promise

#### setupElementTooltip($el)

##### Parameters:

Name | Type | Description
--- | --- | ---
`$el` | object | jquery object on which to setup the tooltip

Source:
  - services/frontend_script_api.js, line 465

#### showError(message)

Show error message to the user.

##### Parameters:

Name | Type | Description
--- | --- | ---
`message` | string |

Source:
  - services/frontend_script_api.js, line 320

#### showMessage(message)

Show info message to the user.

##### Parameters:

Name | Type | Description
--- | --- | ---
`message` | string |

Source:
  - services/frontend_script_api.js, line 312

#### triggerCommand(name, data)

Trigger command.

##### Parameters:

Name | Type | Description
--- | --- | ---
`name` | string |
`data` | object |

Source:
  - services/frontend_script_api.js, line 329

#### triggerEvent(name, data)

Trigger event.

##### Parameters:

Name | Type | Description
--- | --- | ---
`name` | string |
`data` | object |

Source:
  - services/frontend_script_api.js, line 338

#### waitUntilSynced()

Trilium runs in backend and frontend process, when something is changed on the backend from script, frontend will get asynchronously synchronized. This method returns a promise which resolves once all the backend -> frontend synchronization is finished. Typical use case is when new note has been created, we should wait until it is synced into frontend and only then activate it.

Source:
  - services/frontend_script_api.js, line 580