anki-connect/README.md

313 lines
9.3 KiB
Markdown
Raw Normal View History

2016-09-07 04:15:55 +00:00
# AnkiConnect #
2016-05-30 02:59:21 +00:00
2017-04-01 20:21:04 +00:00
The AnkiConnect plugin enables external applications such as [Yomichan](https://foosoft.net/projects/yomichan/) to communicate with
[Anki](https://apps.ankiweb.net/) over a network interface. This software makes it possible to execute queries against
the user's card deck, automatically create new vocabulary and Kanji flash cards, and more. AnkiConnect is compatible
with the latest stable (2.0.x) and alpha (2.1.x) releases of Anki and works on Linux, Windows, and Mac OS X.
2016-05-30 02:59:21 +00:00
2017-04-01 20:21:04 +00:00
## Installation ##
2016-05-30 02:59:21 +00:00
2017-03-15 04:10:45 +00:00
The installation process is similar to that of other Anki plugins and can be accomplished in three steps:
1. Open the *Install Add-on* dialog by selecting *Tools* > *Add-ons* > *Browse & Install* in Anki.
2017-04-01 20:21:04 +00:00
2. Input *[2055492159](https://ankiweb.net/shared/info/2055492159)* into the text box labeled *Code* and press the *OK* button to proceed.
2017-03-15 04:10:45 +00:00
3. Restart Anki when prompted to do so in order to complete the installation of AnkiConnect.
2017-04-01 20:21:04 +00:00
Anki must be kept running in the background in order for other applications to be able to use AnkiConnect. You can
verify that AnkiConnect is running at any time by accessing [localhost:8765](http://localhost:8765) in your browser. If
2017-04-22 06:36:47 +00:00
the server is running, you should see the message *AnkiConnect v.2* displayed in your browser window.
2016-07-03 02:46:01 +00:00
2017-04-22 06:36:47 +00:00
### Notes for Windows Users ###
2017-01-28 20:20:38 +00:00
2017-04-01 20:21:04 +00:00
Windows users may see a firewall nag dialog box appear on Anki startup. This occurs because AnkiConnect hosts a local
server in order to enable other applications to connect to it. The host application, Anki, must be unblocked for this
plugin to function correctly.
2017-01-28 20:20:38 +00:00
2017-04-22 06:36:47 +00:00
### Notes for Mac OS X Users ###
2017-01-28 20:20:38 +00:00
2017-02-12 00:41:02 +00:00
Starting with [Mac OS X Mavericks](https://en.wikipedia.org/wiki/OS_X_Mavericks), a feature named *App Nap* has been
introduced to the operating system. This feature causes certain applications which are open (but not visible) to be
2017-02-12 03:26:22 +00:00
placed in a suspended state. As this behavior causes AnkiConnect to stop working while you have another window in the
2017-02-12 00:41:02 +00:00
foreground, App Nap should be disabled for Anki:
1. Start the Terminal application.
2. Execute the following command in the terminal window:
```
defaults write net.ichi2.anki NSAppSleepDisabled -bool true
```
3. Restart Anki.
2017-04-22 06:36:47 +00:00
## Application Interface for Developers ##
2017-02-01 17:23:09 +00:00
2017-02-03 04:14:24 +00:00
AnkiConnect exposes Anki features to external applications via an easy to use
[RESTful](https://en.wikipedia.org/wiki/Representational_state_transfer) API. After it is installed, this plugin will
initialize a minimal HTTP sever running on port 8765 every time Anki executes. Other applications (including browser
extensions) can then communicate with it via HTTP POST requests.
2017-02-01 17:23:09 +00:00
2017-02-03 04:14:24 +00:00
### Sample Invocation ###
2017-02-01 17:23:09 +00:00
2017-05-06 12:37:31 +00:00
Every request consists of a JSON-encoded object containing an *action*, and a set of contextual *parameters*.
A simple example of a JavaScript application communicating with the extension is illustrated below:
2017-02-01 17:23:09 +00:00
```JavaScript
2017-02-03 04:14:24 +00:00
function ankiInvoke(action, params={}) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.addEventListener('loadend', () => {
if (xhr.responseText) {
resolve(JSON.parse(xhr.responseText));
} else {
reject('unable to connect to plugin');
}
});
xhr.open('POST', 'http://127.0.0.1:8765');
xhr.send(JSON.stringify({action, params}));
});
}
2017-02-01 17:23:09 +00:00
2017-02-03 04:14:24 +00:00
ankiInvoke('version').then(response => {
window.alert(`detected API version: ${response}`);
}).catch(error => {
window.alert(`could not get API version: ${error}`);
});
2017-02-01 17:23:09 +00:00
```
2017-02-03 04:14:24 +00:00
### Supported Actions ###
2017-02-01 17:23:09 +00:00
2017-05-06 12:37:31 +00:00
The following actions are currently supported.
Note that the sample requests are written here as Javascript objects rather than raw JSON.
If you are writing raw requests be sure to send valid JSON (i.e. quote all keys and strings with
`"`, and remove any comments).
2017-02-01 17:23:09 +00:00
2017-04-22 06:36:47 +00:00
* **version**
2017-02-01 17:23:09 +00:00
2017-04-22 06:36:47 +00:00
Gets the version of the API exposed by this plugin. Currently only versions `1` and `2` are defined.
2017-02-03 04:14:24 +00:00
This should be the first call you make to make sure that your application and AnkiConnect are able to communicate
properly with each other. New versions of AnkiConnect will backwards compatible; as long as you are using actions
which are available in the reported AnkiConnect version or earlier, everything should work fine.
2017-04-22 06:36:47 +00:00
*Sample request*:
2017-02-03 04:14:24 +00:00
```
{
action: 'version',
params: {}
2017-02-01 17:23:09 +00:00
}
2017-02-03 04:14:24 +00:00
```
2017-02-01 17:23:09 +00:00
2017-04-22 06:36:47 +00:00
*Sample response*:
2017-02-03 04:14:24 +00:00
```
2017-02-19 23:05:17 +00:00
2
2017-02-03 04:14:24 +00:00
```
2017-04-22 06:36:47 +00:00
* **deckNames**
2017-02-03 04:14:24 +00:00
Gets the complete list of deck names for the current user.
2017-04-22 06:36:47 +00:00
*Sample request*:
2017-02-03 04:14:24 +00:00
```
{
action: 'deckNames',
params: {}
}
```
2017-04-22 06:36:47 +00:00
*Sample response*:
2017-02-03 04:14:24 +00:00
```
[
'Default',
/* ... */
]
```
2017-04-22 06:36:47 +00:00
* **modelNames**
2017-02-03 04:14:24 +00:00
Gets the complete list of model names for the current user.
2017-04-22 06:36:47 +00:00
*Sample request*:
2017-02-03 04:14:24 +00:00
```
{
action: 'modelNames',
params: {}
}
```
2017-04-22 06:36:47 +00:00
*Sample response*:
2017-02-03 04:14:24 +00:00
```
[
'Basic',
'Basic (and reversed card)',
/* ... */
]
```
2017-04-22 06:36:47 +00:00
* **modelFieldNames**
2017-02-03 04:14:24 +00:00
Gets the complete list of field names for the provided model name.
2017-04-22 06:36:47 +00:00
*Sample request*:
2017-02-03 04:14:24 +00:00
```
{
action: 'modelFieldNames',
params: {
modelName: 'Basic'
2017-02-01 17:23:09 +00:00
}
}
2017-02-03 04:14:24 +00:00
```
2017-04-22 06:36:47 +00:00
*Sample response*:
2017-02-03 04:14:24 +00:00
```
[
'Front',
'Back',
/* ... */
]
```
2017-04-22 06:36:47 +00:00
* **addNote**
2017-02-03 04:14:24 +00:00
2017-02-05 20:08:52 +00:00
Creates a note using the given deck and model, with the provided field values and tags. Returns the identifier of
the created note created on success, and `null` on failure.
2017-02-03 04:14:24 +00:00
AnkiConnect can download audio files and embed them in newly created notes. The corresponding *audio* note member is
optional and can be omitted. If you choose to include it, the *url* and *filename* fields must be also defined. The
*skipHash* field can be optionally provided to skip the inclusion of downloaded files with an MD5 hash that matches
the provided value. This is useful for avoiding the saving of error pages and stub files.
2017-04-22 06:36:47 +00:00
*Sample request*:
2017-02-03 04:14:24 +00:00
```
{
action: 'addNote',
params: {
deckName: 'Default',
modelName: 'Basic',
2017-02-05 20:08:52 +00:00
fields: {
Front: 'front content',
Back: 'back content',
/* ... */
},
tags: [
'yomichan',
/* ... */
],
audio: /* optional */ {
url: 'https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?kanji=猫&kana=ねこ',
filename: 'yomichan_ねこ_猫.mp3',
skipHash: '7e2c2f954ef6051373ba916f000168dc'
2017-02-03 04:14:24 +00:00
}
}
}
```
2017-04-22 06:36:47 +00:00
*Sample response*:
2017-02-03 04:14:24 +00:00
```
null
```
2017-02-05 20:08:52 +00:00
2017-04-22 06:36:47 +00:00
* **addNotes**
2017-02-05 20:08:52 +00:00
Creates multiple notes using the given deck and model, with the provided field values and tags. Returns an array of
identifiers of the created notes (notes that could not be created will have a `null` identifier). Please see the
documentation for `addNote` for an explanation of objects in the `notes` array.
2017-04-22 06:36:47 +00:00
*Sample request*:
2017-02-05 20:08:52 +00:00
```
{
action: 'addNotes',
params: {
notes: [
{
deckName: 'Default',
modelName: 'Basic',
fields: {
Front: 'front content',
Back: 'back content',
/* ... */
},
tags: [
'yomichan',
/* ... */
],
audio: /* optional */ {
url: 'https://assets.languagepod101.com/dictionary/japanese/audiomp3.php?kanji=猫&kana=ねこ',
filename: 'yomichan_ねこ_猫.mp3',
skipHash: '7e2c2f954ef6051373ba916f000168dc'
}
},
/* ... */
]
}
}
```
2017-04-22 06:36:47 +00:00
*Sample response*:
2017-02-05 20:08:52 +00:00
```
[
null,
/* ... */
]
```
2017-04-22 06:36:47 +00:00
* **canAddNotes**
2017-02-03 04:14:24 +00:00
Accepts an array of objects which define parameters for candidate notes (see `addNote`) and returns an array of
booleans indicating whether or not the parameters at the corresponding index could be used to create a new note.
2017-04-22 06:36:47 +00:00
*Sample request*:
2017-02-03 04:14:24 +00:00
```
{
action: 'canAddNotes',
params: {
notes: [
2017-02-05 20:08:52 +00:00
{
deckName: 'Default',
modelName: 'Basic',
2017-02-03 04:14:24 +00:00
fields: {
Front: 'front content',
Back: 'back content',
/* ... */
},
tags: [
'yomichan',
/* ... */
]
2017-02-05 20:08:52 +00:00
},
2017-02-03 04:14:24 +00:00
/* ... */
]
}
}
```
2017-04-22 06:36:47 +00:00
*Sample response*:
2017-02-03 04:14:24 +00:00
```
[
true,
/* ... */
]
```
2017-02-01 17:23:09 +00:00
2017-04-22 06:36:47 +00:00
* **upgrade**
2017-02-19 23:05:17 +00:00
Displays a confirmation dialog box in Anki asking the user if they wish to upgrade AnkiConnect to the latest version
from the project's [master branch](https://raw.githubusercontent.com/FooSoft/anki-connect/master/AnkiConnect.py) on
GitHub. Returns a boolean value indicating if the plugin was upgraded or not.
2017-04-22 06:36:47 +00:00
*Sample request*:
2017-02-19 23:05:17 +00:00
```
{
action: 'upgrade',
params: {}
}
```
2017-04-22 06:36:47 +00:00
*Sample response*:
2017-02-19 23:05:17 +00:00
```
true
```
2016-07-03 02:46:01 +00:00
## License ##
GPL