aboutsummaryrefslogtreecommitdiff
path: root/ext/bg
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-05-24 13:30:40 -0400
committerGitHub <noreply@github.com>2020-05-24 13:30:40 -0400
commitc61a87b152b91bdebe01eefdbc3fa00670a3071d (patch)
tree63a94eacdc437da1e166a72a9b4d4794df294f22 /ext/bg
parent83a577fa569e5a6d468e3b304313106bba3e1e49 (diff)
API refactor (#532)
* Convert api.js into a class instance * Use new api.* functions * Fix missing binds * Group functions with progress callbacks together * Change style * Fix API override not working
Diffstat (limited to 'ext/bg')
-rw-r--r--ext/bg/js/context-main.js22
-rw-r--r--ext/bg/js/search-main.js7
-rw-r--r--ext/bg/js/search-query-parser-generator.js4
-rw-r--r--ext/bg/js/search-query-parser.js12
-rw-r--r--ext/bg/js/search.js14
-rw-r--r--ext/bg/js/settings/anki-templates.js21
-rw-r--r--ext/bg/js/settings/anki.js8
-rw-r--r--ext/bg/js/settings/backup.js10
-rw-r--r--ext/bg/js/settings/dictionaries.js24
-rw-r--r--ext/bg/js/settings/main.js10
-rw-r--r--ext/bg/js/settings/popup-preview-frame-main.js4
-rw-r--r--ext/bg/js/settings/popup-preview-frame.js9
-rw-r--r--ext/bg/js/settings/profiles.js6
-rw-r--r--ext/bg/js/settings/storage.js4
14 files changed, 65 insertions, 90 deletions
diff --git a/ext/bg/js/context-main.js b/ext/bg/js/context-main.js
index dbba0272..e90e7e2e 100644
--- a/ext/bg/js/context-main.js
+++ b/ext/bg/js/context-main.js
@@ -16,11 +16,7 @@
*/
/* global
- * apiCommandExec
- * apiForwardLogsToBackend
- * apiGetEnvironmentInfo
- * apiLogIndicatorClear
- * apiOptionsGet
+ * api
*/
function showExtensionInfo() {
@@ -36,12 +32,12 @@ function setupButtonEvents(selector, command, url) {
for (const node of nodes) {
node.addEventListener('click', (e) => {
if (e.button !== 0) { return; }
- apiCommandExec(command, {mode: e.ctrlKey ? 'newTab' : 'existingOrNewTab'});
+ api.commandExec(command, {mode: e.ctrlKey ? 'newTab' : 'existingOrNewTab'});
e.preventDefault();
}, false);
node.addEventListener('auxclick', (e) => {
if (e.button !== 1) { return; }
- apiCommandExec(command, {mode: 'newTab'});
+ api.commandExec(command, {mode: 'newTab'});
e.preventDefault();
}, false);
@@ -54,14 +50,14 @@ function setupButtonEvents(selector, command, url) {
}
async function mainInner() {
- apiForwardLogsToBackend();
+ api.forwardLogsToBackend();
await yomichan.prepare();
- await apiLogIndicatorClear();
+ await api.logIndicatorClear();
showExtensionInfo();
- apiGetEnvironmentInfo().then(({browser}) => {
+ api.getEnvironmentInfo().then(({browser}) => {
// Firefox mobile opens this page as a full webpage.
document.documentElement.dataset.mode = (browser === 'firefox-mobile' ? 'full' : 'mini');
});
@@ -76,14 +72,14 @@ async function mainInner() {
depth: 0,
url: window.location.href
};
- apiOptionsGet(optionsContext).then((options) => {
+ api.optionsGet(optionsContext).then((options) => {
const toggle = document.querySelector('#enable-search');
toggle.checked = options.general.enable;
- toggle.addEventListener('change', () => apiCommandExec('toggle'), false);
+ toggle.addEventListener('change', () => api.commandExec('toggle'), false);
const toggle2 = document.querySelector('#enable-search2');
toggle2.checked = options.general.enable;
- toggle2.addEventListener('change', () => apiCommandExec('toggle'), false);
+ toggle2.addEventListener('change', () => api.commandExec('toggle'), false);
setTimeout(() => {
for (const n of document.querySelectorAll('.toggle-group')) {
diff --git a/ext/bg/js/search-main.js b/ext/bg/js/search-main.js
index 54fa549d..3e089594 100644
--- a/ext/bg/js/search-main.js
+++ b/ext/bg/js/search-main.js
@@ -17,8 +17,7 @@
/* global
* DisplaySearch
- * apiForwardLogsToBackend
- * apiOptionsGet
+ * api
* dynamicLoader
*/
@@ -35,7 +34,7 @@ async function injectSearchFrontend() {
}
(async () => {
- apiForwardLogsToBackend();
+ api.forwardLogsToBackend();
await yomichan.prepare();
const displaySearch = new DisplaySearch();
@@ -45,7 +44,7 @@ async function injectSearchFrontend() {
const applyOptions = async () => {
const optionsContext = {depth: 0, url: window.location.href};
- const options = await apiOptionsGet(optionsContext);
+ const options = await api.optionsGet(optionsContext);
if (!options.scanning.enableOnSearchPage || optionsApplied) { return; }
optionsApplied = true;
diff --git a/ext/bg/js/search-query-parser-generator.js b/ext/bg/js/search-query-parser-generator.js
index 9e7ff8aa..6989e157 100644
--- a/ext/bg/js/search-query-parser-generator.js
+++ b/ext/bg/js/search-query-parser-generator.js
@@ -17,7 +17,7 @@
/* global
* TemplateHandler
- * apiGetQueryParserTemplatesHtml
+ * api
*/
class QueryParserGenerator {
@@ -26,7 +26,7 @@ class QueryParserGenerator {
}
async prepare() {
- const html = await apiGetQueryParserTemplatesHtml();
+ const html = await api.getQueryParserTemplatesHtml();
this._templateHandler = new TemplateHandler(html);
}
diff --git a/ext/bg/js/search-query-parser.js b/ext/bg/js/search-query-parser.js
index e1e37d1c..addfc686 100644
--- a/ext/bg/js/search-query-parser.js
+++ b/ext/bg/js/search-query-parser.js
@@ -18,9 +18,7 @@
/* global
* QueryParserGenerator
* TextScanner
- * apiModifySettings
- * apiTermsFind
- * apiTextParse
+ * api
* docSentenceExtract
*/
@@ -59,7 +57,7 @@ class QueryParser {
this._setPreview(text);
- this._parseResults = await apiTextParse(text, this._getOptionsContext());
+ this._parseResults = await api.textParse(text, this._getOptionsContext());
this._refreshSelectedParser();
this._renderParserSelect();
@@ -80,7 +78,7 @@ class QueryParser {
const searchText = this._textScanner.getTextSourceContent(textSource, this._options.scanning.length);
if (searchText.length === 0) { return null; }
- const {definitions, length} = await apiTermsFind(searchText, {}, this._getOptionsContext());
+ const {definitions, length} = await api.termsFind(searchText, {}, this._getOptionsContext());
if (definitions.length === 0) { return null; }
const sentence = docSentenceExtract(textSource, this._options.anki.sentenceExt);
@@ -99,7 +97,7 @@ class QueryParser {
_onParserChange(e) {
const value = e.target.value;
- apiModifySettings([{
+ api.modifySettings([{
action: 'set',
path: 'parsing.selectedParser',
value,
@@ -112,7 +110,7 @@ class QueryParser {
if (this._parseResults.length > 0) {
if (!this._getParseResult()) {
const value = this._parseResults[0].id;
- apiModifySettings([{
+ api.modifySettings([{
action: 'set',
path: 'parsing.selectedParser',
value,
diff --git a/ext/bg/js/search.js b/ext/bg/js/search.js
index 96e8a70b..75707e6c 100644
--- a/ext/bg/js/search.js
+++ b/ext/bg/js/search.js
@@ -20,9 +20,7 @@
* DOM
* Display
* QueryParser
- * apiClipboardGet
- * apiModifySettings
- * apiTermsFind
+ * api
* wanakana
*/
@@ -52,7 +50,7 @@ class DisplaySearch extends Display {
this.introVisible = true;
this.introAnimationTimer = null;
- this.clipboardMonitor = new ClipboardMonitor({getClipboard: apiClipboardGet});
+ this.clipboardMonitor = new ClipboardMonitor({getClipboard: api.clipboardGet.bind(api)});
this._onKeyDownIgnoreKeys = new Map([
['ANY_MOD', new Set([
@@ -234,7 +232,7 @@ class DisplaySearch extends Display {
this.setIntroVisible(!valid, animate);
this.updateSearchButton();
if (valid) {
- const {definitions} = await apiTermsFind(query, details, this.getOptionsContext());
+ const {definitions} = await api.termsFind(query, details, this.getOptionsContext());
this.setContent('terms', {definitions, context: {
focus: false,
disableHistory: true,
@@ -258,7 +256,7 @@ class DisplaySearch extends Display {
} else {
wanakana.unbind(this.query);
}
- apiModifySettings([{
+ api.modifySettings([{
action: 'set',
path: 'general.enableWanakana',
value,
@@ -274,7 +272,7 @@ class DisplaySearch extends Display {
(granted) => {
if (granted) {
this.clipboardMonitor.start();
- apiModifySettings([{
+ api.modifySettings([{
action: 'set',
path: 'general.enableClipboardMonitor',
value: true,
@@ -288,7 +286,7 @@ class DisplaySearch extends Display {
);
} else {
this.clipboardMonitor.stop();
- apiModifySettings([{
+ api.modifySettings([{
action: 'set',
path: 'general.enableClipboardMonitor',
value: false,
diff --git a/ext/bg/js/settings/anki-templates.js b/ext/bg/js/settings/anki-templates.js
index d5b6e677..0dadb433 100644
--- a/ext/bg/js/settings/anki-templates.js
+++ b/ext/bg/js/settings/anki-templates.js
@@ -19,10 +19,7 @@
* AnkiNoteBuilder
* ankiGetFieldMarkers
* ankiGetFieldMarkersHtml
- * apiGetDefaultAnkiFieldTemplates
- * apiOptionsGet
- * apiTemplateRender
- * apiTermsFind
+ * api
* getOptionsContext
* getOptionsMutable
* settingsSaveOptions
@@ -38,7 +35,7 @@ async function onAnkiFieldTemplatesResetConfirm(e) {
$('#field-template-reset-modal').modal('hide');
- const value = await apiGetDefaultAnkiFieldTemplates();
+ const value = await api.getDefaultAnkiFieldTemplates();
const element = document.querySelector('#field-templates');
element.value = value;
@@ -65,9 +62,9 @@ function ankiTemplatesInitialize() {
async function ankiTemplatesUpdateValue() {
const optionsContext = getOptionsContext();
- const options = await apiOptionsGet(optionsContext);
+ const options = await api.optionsGet(optionsContext);
let templates = options.anki.fieldTemplates;
- if (typeof templates !== 'string') { templates = await apiGetDefaultAnkiFieldTemplates(); }
+ if (typeof templates !== 'string') { templates = await api.getDefaultAnkiFieldTemplates(); }
$('#field-templates').val(templates);
onAnkiTemplatesValidateCompile();
@@ -79,7 +76,7 @@ const ankiTemplatesValidateGetDefinition = (() => {
return async (text, optionsContext) => {
if (cachedText !== text) {
- const {definitions} = await apiTermsFind(text, {}, optionsContext);
+ const {definitions} = await api.termsFind(text, {}, optionsContext);
if (definitions.length === 0) { return null; }
cachedValue = definitions[0];
@@ -97,15 +94,15 @@ async function ankiTemplatesValidate(infoNode, field, mode, showSuccessResult, i
const optionsContext = getOptionsContext();
const definition = await ankiTemplatesValidateGetDefinition(text, optionsContext);
if (definition !== null) {
- const options = await apiOptionsGet(optionsContext);
+ const options = await api.optionsGet(optionsContext);
const context = {
document: {
title: document.title
}
};
let templates = options.anki.fieldTemplates;
- if (typeof templates !== 'string') { templates = await apiGetDefaultAnkiFieldTemplates(); }
- const ankiNoteBuilder = new AnkiNoteBuilder({renderTemplate: apiTemplateRender});
+ if (typeof templates !== 'string') { templates = await api.getDefaultAnkiFieldTemplates(); }
+ const ankiNoteBuilder = new AnkiNoteBuilder({renderTemplate: api.templateRender.bind(api)});
result = await ankiNoteBuilder.formatField(field, definition, mode, context, options, templates, exceptions);
}
} catch (e) {
@@ -125,7 +122,7 @@ async function ankiTemplatesValidate(infoNode, field, mode, showSuccessResult, i
async function onAnkiFieldTemplatesChanged(e) {
// Get value
let templates = e.currentTarget.value;
- if (templates === await apiGetDefaultAnkiFieldTemplates()) {
+ if (templates === await api.getDefaultAnkiFieldTemplates()) {
// Default
templates = null;
}
diff --git a/ext/bg/js/settings/anki.js b/ext/bg/js/settings/anki.js
index ff1277ed..ba83f994 100644
--- a/ext/bg/js/settings/anki.js
+++ b/ext/bg/js/settings/anki.js
@@ -16,9 +16,7 @@
*/
/* global
- * apiGetAnkiDeckNames
- * apiGetAnkiModelFieldNames
- * apiGetAnkiModelNames
+ * api
* getOptionsContext
* getOptionsMutable
* onFormOptionsChanged
@@ -107,7 +105,7 @@ async function _ankiDeckAndModelPopulate(options) {
const kanjiModel = {value: options.anki.kanji.model, selector: '#anki-kanji-model'};
try {
_ankiSpinnerShow(true);
- const [deckNames, modelNames] = await Promise.all([apiGetAnkiDeckNames(), apiGetAnkiModelNames()]);
+ const [deckNames, modelNames] = await Promise.all([api.getAnkiDeckNames(), api.getAnkiModelNames()]);
deckNames.sort();
modelNames.sort();
termsDeck.values = deckNames;
@@ -180,7 +178,7 @@ async function _onAnkiModelChanged(e) {
let fieldNames;
try {
const modelName = node.value;
- fieldNames = await apiGetAnkiModelFieldNames(modelName);
+ fieldNames = await api.getAnkiModelFieldNames(modelName);
_ankiSetError(null);
} catch (error) {
_ankiSetError(error);
diff --git a/ext/bg/js/settings/backup.js b/ext/bg/js/settings/backup.js
index faf4e592..5eb55502 100644
--- a/ext/bg/js/settings/backup.js
+++ b/ext/bg/js/settings/backup.js
@@ -16,9 +16,7 @@
*/
/* global
- * apiGetDefaultAnkiFieldTemplates
- * apiGetEnvironmentInfo
- * apiOptionsGetFull
+ * api
* optionsGetDefault
* optionsUpdateVersion
* utilBackend
@@ -51,9 +49,9 @@ function _getSettingsExportDateString(date, dateSeparator, dateTimeSeparator, ti
}
async function _getSettingsExportData(date) {
- const optionsFull = await apiOptionsGetFull();
- const environment = await apiGetEnvironmentInfo();
- const fieldTemplatesDefault = await apiGetDefaultAnkiFieldTemplates();
+ const optionsFull = await api.optionsGetFull();
+ const environment = await api.getEnvironmentInfo();
+ const fieldTemplatesDefault = await api.getDefaultAnkiFieldTemplates();
// Format options
for (const {options} of optionsFull.profiles) {
diff --git a/ext/bg/js/settings/dictionaries.js b/ext/bg/js/settings/dictionaries.js
index 632c01ea..4d307f0f 100644
--- a/ext/bg/js/settings/dictionaries.js
+++ b/ext/bg/js/settings/dictionaries.js
@@ -17,13 +17,7 @@
/* global
* PageExitPrevention
- * apiDeleteDictionary
- * apiGetDictionaryCounts
- * apiGetDictionaryInfo
- * apiImportDictionaryArchive
- * apiOptionsGet
- * apiOptionsGetFull
- * apiPurgeDatabase
+ * api
* getOptionsContext
* getOptionsFullMutable
* getOptionsMutable
@@ -312,7 +306,7 @@ class SettingsDictionaryEntryUI {
progressBar.style.width = `${percent}%`;
};
- await apiDeleteDictionary(this.dictionaryInfo.title, onProgress);
+ await api.deleteDictionary(this.dictionaryInfo.title, onProgress);
} catch (e) {
dictionaryErrorsShow([e]);
} finally {
@@ -423,7 +417,7 @@ async function onDictionaryOptionsChanged() {
dictionaryUI.setOptionsDictionaries(options.dictionaries);
- const optionsFull = await apiOptionsGetFull();
+ const optionsFull = await api.optionsGetFull();
document.querySelector('#database-enable-prefix-wildcard-searches').checked = optionsFull.global.database.prefixWildcardsSupported;
await updateMainDictionarySelectValue();
@@ -431,7 +425,7 @@ async function onDictionaryOptionsChanged() {
async function onDatabaseUpdated() {
try {
- const dictionaries = await apiGetDictionaryInfo();
+ const dictionaries = await api.getDictionaryInfo();
dictionaryUI.setDictionaries(dictionaries);
document.querySelector('#dict-warning').hidden = (dictionaries.length > 0);
@@ -439,7 +433,7 @@ async function onDatabaseUpdated() {
updateMainDictionarySelectOptions(dictionaries);
await updateMainDictionarySelectValue();
- const {counts, total} = await apiGetDictionaryCounts(dictionaries.map((v) => v.title), true);
+ const {counts, total} = await api.getDictionaryCounts(dictionaries.map((v) => v.title), true);
dictionaryUI.setCounts(counts, total);
} catch (e) {
dictionaryErrorsShow([e]);
@@ -468,7 +462,7 @@ function updateMainDictionarySelectOptions(dictionaries) {
async function updateMainDictionarySelectValue() {
const optionsContext = getOptionsContext();
- const options = await apiOptionsGet(optionsContext);
+ const options = await api.optionsGet(optionsContext);
const value = options.general.mainDictionary;
@@ -618,7 +612,7 @@ async function onDictionaryPurge(e) {
dictionaryErrorsShow(null);
dictionarySpinnerShow(true);
- await apiPurgeDatabase();
+ await api.purgeDatabase();
for (const {options} of toIterable((await getOptionsFullMutable()).profiles)) {
options.dictionaries = utilBackgroundIsolate({});
options.general.mainDictionary = '';
@@ -666,7 +660,7 @@ async function onDictionaryImport(e) {
}
};
- const optionsFull = await apiOptionsGetFull();
+ const optionsFull = await api.optionsGetFull();
const importDetails = {
prefixWildcardsSupported: optionsFull.global.database.prefixWildcardsSupported
@@ -680,7 +674,7 @@ async function onDictionaryImport(e) {
}
const archiveContent = await dictReadFile(files[i]);
- const {result, errors} = await apiImportDictionaryArchive(archiveContent, importDetails, updateProgress);
+ const {result, errors} = await api.importDictionaryArchive(archiveContent, importDetails, updateProgress);
for (const {options} of toIterable((await getOptionsFullMutable()).profiles)) {
const dictionaryOptions = SettingsDictionaryListUI.createDictionaryOptions();
dictionaryOptions.enabled = true;
diff --git a/ext/bg/js/settings/main.js b/ext/bg/js/settings/main.js
index 61395b1c..94f7f8f5 100644
--- a/ext/bg/js/settings/main.js
+++ b/ext/bg/js/settings/main.js
@@ -21,9 +21,7 @@
* ankiInitialize
* ankiTemplatesInitialize
* ankiTemplatesUpdateValue
- * apiForwardLogsToBackend
- * apiGetEnvironmentInfo
- * apiOptionsSave
+ * api
* appearanceInitialize
* audioSettingsInitialize
* backupInitialize
@@ -265,7 +263,7 @@ function settingsGetSource() {
async function settingsSaveOptions() {
const source = await settingsGetSource();
- await apiOptionsSave(source);
+ await api.optionsSave(source);
}
async function onOptionsUpdated({source}) {
@@ -290,7 +288,7 @@ async function settingsPopulateModifierKeys() {
const scanModifierKeySelect = document.querySelector('#scan-modifier-key');
scanModifierKeySelect.textContent = '';
- const environment = await apiGetEnvironmentInfo();
+ const environment = await api.getEnvironmentInfo();
const modifierKeys = [
{value: 'none', name: 'None'},
...environment.modifiers.keys
@@ -305,7 +303,7 @@ async function settingsPopulateModifierKeys() {
async function onReady() {
- apiForwardLogsToBackend();
+ api.forwardLogsToBackend();
await yomichan.prepare();
showExtensionInformation();
diff --git a/ext/bg/js/settings/popup-preview-frame-main.js b/ext/bg/js/settings/popup-preview-frame-main.js
index 8228125f..a362efa5 100644
--- a/ext/bg/js/settings/popup-preview-frame-main.js
+++ b/ext/bg/js/settings/popup-preview-frame-main.js
@@ -17,10 +17,10 @@
/* global
* SettingsPopupPreview
- * apiForwardLogsToBackend
+ * api
*/
(() => {
- apiForwardLogsToBackend();
+ api.forwardLogsToBackend();
new SettingsPopupPreview();
})();
diff --git a/ext/bg/js/settings/popup-preview-frame.js b/ext/bg/js/settings/popup-preview-frame.js
index 8901a0c4..bd9357e9 100644
--- a/ext/bg/js/settings/popup-preview-frame.js
+++ b/ext/bg/js/settings/popup-preview-frame.js
@@ -20,14 +20,13 @@
* Popup
* PopupFactory
* TextSourceRange
- * apiFrameInformationGet
- * apiOptionsGet
+ * api
*/
class SettingsPopupPreview {
constructor() {
this.frontend = null;
- this.apiOptionsGetOld = apiOptionsGet;
+ this.apiOptionsGetOld = api.optionsGet.bind(api);
this.popup = null;
this.popupSetCustomOuterCssOld = null;
this.popupShown = false;
@@ -54,10 +53,10 @@ class SettingsPopupPreview {
document.querySelector('#theme-dark-checkbox').addEventListener('change', this.onThemeDarkCheckboxChanged.bind(this), false);
// Overwrite API functions
- window.apiOptionsGet = this.apiOptionsGet.bind(this);
+ api.optionsGet = this.apiOptionsGet.bind(this);
// Overwrite frontend
- const {frameId} = await apiFrameInformationGet();
+ const {frameId} = await api.frameInformationGet();
const popupFactory = new PopupFactory(frameId);
await popupFactory.prepare();
diff --git a/ext/bg/js/settings/profiles.js b/ext/bg/js/settings/profiles.js
index bdf5a13d..e32d5525 100644
--- a/ext/bg/js/settings/profiles.js
+++ b/ext/bg/js/settings/profiles.js
@@ -17,7 +17,7 @@
/* global
* ConditionsUI
- * apiOptionsGetFull
+ * api
* conditionsClearCaches
* formWrite
* getOptionsFullMutable
@@ -215,7 +215,7 @@ async function onProfileRemove(e) {
return await onProfileRemoveConfirm();
}
- const optionsFull = await apiOptionsGetFull();
+ const optionsFull = await api.optionsGetFull();
if (optionsFull.profiles.length <= 1) {
return;
}
@@ -278,7 +278,7 @@ async function onProfileMove(offset) {
}
async function onProfileCopy() {
- const optionsFull = await apiOptionsGetFull();
+ const optionsFull = await api.optionsGetFull();
if (optionsFull.profiles.length <= 1) {
return;
}
diff --git a/ext/bg/js/settings/storage.js b/ext/bg/js/settings/storage.js
index d754a109..73c93fa1 100644
--- a/ext/bg/js/settings/storage.js
+++ b/ext/bg/js/settings/storage.js
@@ -16,7 +16,7 @@
*/
/* global
- * apiGetEnvironmentInfo
+ * api
*/
function storageBytesToLabeledString(size) {
@@ -52,7 +52,7 @@ async function isStoragePeristent() {
async function storageInfoInitialize() {
storagePersistInitialize();
- const {browser, platform} = await apiGetEnvironmentInfo();
+ const {browser, platform} = await api.getEnvironmentInfo();
document.documentElement.dataset.browser = browser;
document.documentElement.dataset.operatingSystem = platform.os;