Skip to content
This repository was archived by the owner on Feb 25, 2023. It is now read-only.

Commit 096bde4

Browse files
Documentation updates (#2257)
* Document Backend * Document RequestBuilder * Document some of AnkiConnect
1 parent a47cbc3 commit 096bde4

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

ext/js/background/backend.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,14 @@
3838
* wanakana
3939
*/
4040

41+
/**
42+
* This class controls the core logic of the extension, including API calls
43+
* and various forms of communication between browser tabs and external applications.
44+
*/
4145
class Backend {
46+
/**
47+
* Creates a new instance.
48+
*/
4249
constructor() {
4350
this._japaneseUtil = new JapaneseUtil(wanakana);
4451
this._environment = new Environment();
@@ -145,6 +152,10 @@ class Backend {
145152
]);
146153
}
147154

155+
/**
156+
* Initializes the instance.
157+
* @returns {Promise<void>} A promise which is resolved when initialization completes.
158+
*/
148159
prepare() {
149160
if (this._preparePromise === null) {
150161
const promise = this._prepareInternal();

ext/js/background/request-builder.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,29 @@
1515
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1616
*/
1717

18+
/**
19+
* This class is used to generate `fetch()` requests on the background page
20+
* with additional controls over anonymity and error handling.
21+
*/
1822
class RequestBuilder {
23+
/**
24+
* A progress callback for a fetch read.
25+
* @callback ProgressCallback
26+
* @param {boolean} complete Whether or not the data has been completely read.
27+
*/
28+
29+
/**
30+
* Creates a new instance.
31+
*/
1932
constructor() {
2033
this._onBeforeSendHeadersExtraInfoSpec = ['blocking', 'requestHeaders', 'extraHeaders'];
2134
this._textEncoder = new TextEncoder();
2235
this._ruleIds = new Set();
2336
}
2437

38+
/**
39+
* Initializes the instance.
40+
*/
2541
async prepare() {
2642
try {
2743
await this._clearDynamicRules();
@@ -30,7 +46,14 @@ class RequestBuilder {
3046
}
3147
}
3248

49+
/**
50+
* Runs an anonymized fetch request, which strips the `Cookie` header and adjust the `Origin` header.
51+
* @param {string} url The URL to fetch.
52+
* @param {RequestInit} init The initialization parameters passed to the `fetch` function.
53+
* @returns {Promise<Response>} The response of the `fetch` call.
54+
*/
3355
async fetchAnonymous(url, init) {
56+
fetch(1, 2);
3457
if (isObject(chrome.declarativeNetRequest)) {
3558
return await this._fetchAnonymousDeclarative(url, init);
3659
}
@@ -42,6 +65,12 @@ class RequestBuilder {
4265
return await this._fetchInternal(url, init, headerModifications);
4366
}
4467

68+
/**
69+
* Reads the array buffer body of a fetch response, with an optional `onProgress` callback.
70+
* @param {Response} response The response of a `fetch` call.
71+
* @param {ProgressCallback} onProgress The progress callback
72+
* @returns {Promise<Uint8Array>} The resulting binary data.
73+
*/
4574
static async readFetchResponseArrayBuffer(response, onProgress) {
4675
let reader;
4776
try {

ext/js/comm/anki-connect.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@
1919
* AnkiUtil
2020
*/
2121

22+
/**
23+
* This class controls communication with Anki via the AnkiConnect plugin.
24+
*/
2225
class AnkiConnect {
26+
/**
27+
* Creates a new instance.
28+
*/
2329
constructor() {
2430
this._enabled = false;
2531
this._server = null;
@@ -29,30 +35,59 @@ class AnkiConnect {
2935
this._apiKey = null;
3036
}
3137

38+
/**
39+
* Gets the URL of the AnkiConnect server.
40+
* @type {string}
41+
*/
3242
get server() {
3343
return this._server;
3444
}
3545

46+
/**
47+
* Assigns the URL of the AnkiConnect server.
48+
* @param {string} value The new server URL to assign.
49+
*/
3650
set server(value) {
3751
this._server = value;
3852
}
3953

54+
/**
55+
* Gets whether or not server communication is enabled.
56+
* @type {boolean}
57+
*/
4058
get enabled() {
4159
return this._enabled;
4260
}
4361

62+
/**
63+
* Sets whether or not server communication is enabled.
64+
* @param {boolean} value The enabled state.
65+
*/
4466
set enabled(value) {
4567
this._enabled = value;
4668
}
4769

70+
/**
71+
* Gets the API key used when connecting to AnkiConnect.
72+
* The value will be `null` if no API key is used.
73+
* @type {?string}
74+
*/
4875
get apiKey() {
4976
return this._apiKey;
5077
}
5178

79+
/**
80+
* Sets the API key used when connecting to AnkiConnect.
81+
* @param {?string} value The API key to use, or `null` if no API key should be used.
82+
*/
5283
set apiKey(value) {
5384
this._apiKey = value;
5485
}
5586

87+
/**
88+
* Checks whether a connection to AnkiConnect can be established.
89+
* @returns {Promise<boolean>} `true` if the connection was made, `false` otherwise.
90+
*/
5691
async isConnected() {
5792
try {
5893
await this._invoke('version');
@@ -62,6 +97,10 @@ class AnkiConnect {
6297
}
6398
}
6499

100+
/**
101+
* Gets the AnkiConnect API version number.
102+
* @returns {Promise<number>} The version number
103+
*/
65104
async getVersion() {
66105
if (!this._enabled) { return null; }
67106
await this._checkVersion();

0 commit comments

Comments
 (0)