iShell: API

iShell API Reference

iShell supports the modern cmdAPI namespace and the legacy CmdUtils namespace from Ubiquity API. The use of native JavaScript and WebExtensions APIs is suggested whenever appropriate.

cmdAPI.createCommand(options)

Legacy name: CmdUtils.CreateCommand

Creates and registers an iShell command. Equivalent annotations for the class-based syntax are specified in parentheses.

options is a dictionary object which must have the following properties:

The following properties are optional but strongly recommended:

The following properties are optional:

cmdAPI.createSearchCommand(options)

Legacy name: CmdUtils.makeSearchCommand

A specialized version of cmdAPI.createCommand(). This lets you make commands that interface with search engines, without having to write so much boilerplate code. Use the @search annotation to mark class-based search commands.

options is same as the argument of cmdAPI.createCommand(), except that instead of options.arguments, options.execute, and options.preview, you only need a single property:

If not specified, options.name, options.icon, options.description, options.execute will be auto generated.

Other optional parameters of options are:

cmdAPI.notify(message, title)

Legacy names: displayMessage

Display a popup notification with the specified message and title.

cmdAPI.notifyError(message, title)since v0.8

Display an error popup notification with the specified message and title.

cmdAPI.previewList([prefix], pblock, htmls, [callback], [css])

Legacy name: CmdUtils.previewList

Also called cmdAPI.htmlPreviewList. Creates a simple clickable list in the preview block and returns the list element.

cmdAPI.imagePreviewList([prefix], pblock, imageURLs, [callback], [css])since v0.8

Creates an image preview list in the preview block and returns the list element.

cmdAPI.objectPreviewList([prefix], pblock, items, cfg, [css])

Creates a styled clickable list in the preview block from a set of JavaScript objects and returns the list element.

cmdAPI.reduceTemplate(array, func)since v0.5

Also called R in the global namespace. Reduces the given array by concatenating the string values returned by the func applied to the array items. Useful for the generation of HTML lists with JavaScript template literals.

The function accepted through the func parameter has the following arguments:

cmdAPI.getSelection()

Legacy name: CmdUtils.getSelection

Returns a string containing the text and just the text of the user's current selection, i.e. with HTML tags stripped out.

cmdAPI.getHtmlSelection()

Legacy name: CmdUtils.getHtmlSelection

Returns a string containing the HTML representation of the user's current selection, i.e. text including tags.

cmdAPI.setSelection(content)

Legacy name: CmdUtils.setSelection

Replaces the current selection with content.

cmdAPI.copyToClipboard(text)

Legacy name: CmdUtils.copyToClipboard

This function places the passed-in text into the OS's clipboard. If the text is empty, the copy isn't performed.

cmdAPI.getLocation()

Legacy name: CmdUtils.getLocation

Returns the location URL of the active tab, if available.

cmdAPI.newTab(url, callback)

Legacy names: Utils.openUrlInBrowser

Opens the given URL in the user's browser, using their current preferences for how new URLs should be opened (e.g., in a new window vs. a new tab, etc). Passes the newly opened tab object to the callback function or returns it as a promise if the callback is omitted.

cmdAPI.getActiveTab()

Also available as a read-only property cmdAPI.activeTab. Returns the Tab object of the currently active tab. May return null for the special tabs.

cmdAPI.executeScript(tabId, options)since v0.5

Executes a content script in the tab with the given ID.

Returns an array of objects with the following properties:

cmdAPI.previewFetch(pblock, resource, init)since v0.2

Does an asynchronous request to a remote web service. It is a wrapper around the standard fetch() function. The difference is that cmdAPI.previewFetch() is designed to handle command previews, which can be canceled by a user action between the time it is requested and the time it displays. If the preview is canceled, an "AbortError" exception is thrown.

The init argument may contain the following additional properties:

cmdAPI.fetchAborted(error)since v0.2

Determines if the call to cmdAPI.previewFetch was aborted by a user action.

cmdAPI.backendFetch(pblock, path, init)since v0.5

Does an asynchronous request to the web server being run by the iShell native backend application. If pblock parameter is specified, cmdAPI.previewFetch() function is used to make a request. Built-in JavaScript fetch is used otherwise. If the backend application could not be run, the function throws "IShellNoBackendApp" exception.

cmdAPI.previewAjax(pblock, options)

Legacy name: CmdUtils.previewAjax

Does an asynchronous request to a remote web service. It is used just like jQuery.ajax(), which is documented here.
The difference is that cmdAPI.previewAjax() is designed to handle command previews, which can be canceled by a user action between the time it is requested and the time it displays. If the preview is canceled, no callbacks in the options object will be called.

cmdAPI.previewGet(pblock, url, data, callback, type)
cmdAPI.previewPost(pblock, url, data, callback, type)

Legacy names: CmdUtils.previewGet, CmdUtils.previewPost

Does an asynchronous request to a remote web service. It is used just like jQuery.get()/jQuery.post(), which is documented here.
The difference is that previewGet()/previewPost() is designed to handle command previews, which can be canceled by a user action between the time it is requested and the time it displays. If the preview is canceled, the given callback will not be called.

cmdAPI.absUrl(data, baseUrl)

Legacy name: CmdUtils.absUrl

Fixes relative URLs in data (e.g. as returned by Ajax calls). Useful for displaying fetched content in command previews.

cmdAPI.paramsToString(params, prefix = "?")

Legacy name: Utils.paramsToString

Takes the given object containing keys and values into a query string suitable for inclusion in an HTTP GET or POST request.

cmdAPI.urlToParams(url)

Legacy name: Utils.urlToParams

Given a url, returns an object containing keys and values retrieved from its query-part.

cmdAPI.parseHtml(htmlText)

Legacy name: Utils.parseHtml

An alternative to jQuery() which allows access to the entire document content. Returns HTML document object.

cmdAPI.escapeHtml(string)

Legacy names: Utils.escapeHtml, H()

Returns a version of the string safe for insertion into HTML.

cmdAPI.matchScore(match)

Legacy name: NounUtils.matchScore

Calculates the score for use in suggestions from a result array (match) of RegExp#exec run against user input.

cmdAPI.makeSugg(text, html, data, score, selectionIndices)

Legacy name: NounUtils.makeSugg

Creates a suggestion object that are used by the parser to make suggestions displayed at the popup window. Fills in text and html if missing and constructs summary from text and selectionIndices. At least one of text, html, or data is required.

cmdAPI.grepSuggs(input, suggs, key)

Legacy name: NounUtils.grepSuggs

A helper function to grep a list of suggestion objects by the user input. Returns an array of filtered suggetions, each of them assigned score calculated by cmdAPI.matchScore().

cmdAPI.createBin(uuid, callback)

Creates an instance of the Bin interface and passes it to the supplied callback function or returns it as a promise. This allows to conveniently share persistent storage between commands or noun types.

cmdAPI.getCommandLine()

Returns the current user input.

cmdAPI.setCommandLine(text)

Sets the content of the command line.

cmdAPI.closeCommandLine()since v0.8

Close the iShell popup window.

cmdAPI.getCommandAttributes(className)since v0.8

For a class-based command returns the command attributes as they are defined in the object-based syntax (passed to cmdAPI.createCommand).

cmdAPI.executeCommand(commandText, suggestion = 0)since v1.0

Runs command parser on the commandText and calls the execute method of the suggested command with the index defined by the suggestion parameter. The function could be used to make shortcuts for commands that are tedious to type.

cmdAPI.settings

Returns an object with key/value pairs customizable through the main setting page (read-only).

cmdAPI.arrowSelectionsince v0.8

A boolean property that indicates whether Ctrl+ARROWS selection is active. Could be used in the event handlers of cmdAPI.previewList and cmdAPI.objectPreviewList.

Bin

A persistent data storage interface that is available through the last argument of any command handler function. Calling any method of this interface with an argument will persistently store the supplied value. Calling the same method without arguments will return the value stored by the earlier call of this method.

    preview: function(pblock, args, Bin) {
        pblock.innerHTML = Bin.myProperty() || "Nothing stored yet";
        Bin.myProperty("this value persists between browser restarts");
        Bin.anotherProperty("this call persists another value");
    }
    

The interface offers separate storage space for each command.

jQuery, $

The entry points of jQuery API.

Built-in Noun Types

NounType system of the iShell parser is a powerful tool that allows adding of dynamic entities (such as the set of current tabs) to argument autocompletion and suggestion list. See the command authoring tutorial for an example.

Currently, the following noun types are built-in into iShell: