summaryrefslogtreecommitdiff
path: root/ext/bg/js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-04-01 21:12:59 -0400
committertoasted-nutbread <toasted-nutbread@users.noreply.github.com>2020-04-07 19:04:47 -0400
commit97b7b521dd73251dcfb0799793929395e03c8328 (patch)
tree09bf445894514d11d0b27743f22d34994dfa3074 /ext/bg/js
parenta49e061545ef1d794f5f30d609168afbea9fdf6c (diff)
Create _containsMarker to reduce redundant code
Diffstat (limited to 'ext/bg/js')
-rw-r--r--ext/bg/js/anki-note-builder.js34
1 files changed, 12 insertions, 22 deletions
diff --git a/ext/bg/js/anki-note-builder.js b/ext/bg/js/anki-note-builder.js
index 595d56a8..e396bec6 100644
--- a/ext/bg/js/anki-note-builder.js
+++ b/ext/bg/js/anki-note-builder.js
@@ -85,17 +85,7 @@ class AnkiNoteBuilder {
}
async injectAudio(definition, fields, sources, audioSystem, optionsContext) {
- let usesAudio = false;
- for (const fieldValue of Object.values(fields)) {
- if (fieldValue.includes('{audio}')) {
- usesAudio = true;
- break;
- }
- }
-
- if (!usesAudio) {
- return true;
- }
+ if (!this._containsMarker(fields, 'audio')) { return; }
try {
const expressions = definition.expressions;
@@ -114,17 +104,7 @@ class AnkiNoteBuilder {
}
async injectScreenshot(definition, fields, screenshot, anki) {
- let usesScreenshot = false;
- for (const fieldValue of Object.values(fields)) {
- if (fieldValue.includes('{screenshot}')) {
- usesScreenshot = true;
- break;
- }
- }
-
- if (!usesScreenshot) {
- return;
- }
+ if (!this._containsMarker(fields, 'screenshot')) { return; }
const now = new Date(Date.now());
const filename = `yomichan_browser_screenshot_${definition.reading}_${this._dateToString(now)}.${screenshot.format}`;
@@ -160,6 +140,16 @@ class AnkiNoteBuilder {
return `${year}-${month}-${day}-${hours}-${minutes}-${seconds}`;
}
+ _containsMarker(fields, marker) {
+ marker = `{${marker}}`;
+ for (const fieldValue of Object.values(fields)) {
+ if (fieldValue.includes(marker)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
static stringReplaceAsync(str, regex, replacer) {
let match;
let index = 0;