Pop-up dictionary browser extension for language learning. Successor to Yomichan. (PERSONAL FORK)

Don't send invalid cards to dupe check (#2267)

authored by

Kuuuube and committed by
GitHub
92e733ac d82684d9

+34 -6
+34 -6
ext/js/display/display-anki.js
··· 969 969 } 970 970 } 971 971 972 - const noteInfoList = await Promise.all(notePromises); 973 - const notes = noteInfoList.map(({note}) => note); 972 + const noteInfoList = (await Promise.all(notePromises)); 973 + const validNotes = []; 974 + /** @type {(import('anki').NoteInfoWrapper?)[]} */ 975 + const invalidAndPlaceholderNotes = []; 976 + for (const noteInfo of noteInfoList) { 977 + const note = noteInfo.note; 978 + if (note.deckName.length > 0 && note.modelName.length > 0) { 979 + validNotes.push(note); 980 + invalidAndPlaceholderNotes.push(null); 981 + } else { 982 + invalidAndPlaceholderNotes.push({ 983 + canAdd: false, 984 + valid: false, 985 + noteIds: null, 986 + }); 987 + } 988 + } 974 989 975 990 let infos; 976 991 let ankiError = null; 977 992 try { 978 993 if (this._checkForDuplicates) { 979 - infos = await this._display.application.api.getAnkiNoteInfo(notes, this._isAdditionalInfoEnabled()); 994 + infos = await this._display.application.api.getAnkiNoteInfo(validNotes, this._isAdditionalInfoEnabled()); 980 995 } else { 981 996 const isAnkiConnected = await this._display.application.api.isAnkiConnected(); 982 - infos = this._getAnkiNoteInfoForceValueIfValid(notes, isAnkiConnected); 997 + infos = this._getAnkiNoteInfoForceValueIfValid(validNotes, isAnkiConnected); 983 998 ankiError = isAnkiConnected ? null : new Error('Anki not connected'); 984 999 } 985 1000 } catch (e) { 986 - infos = this._getAnkiNoteInfoForceValueIfValid(notes, false); 1001 + infos = this._getAnkiNoteInfoForceValueIfValid(validNotes, false); 987 1002 ankiError = (e instanceof ExtensionError && e.message.includes('Anki connection failure')) ? 988 1003 new Error('Anki not connected') : 989 1004 toError(e); 990 1005 } 991 1006 1007 + /** @type {(import('anki').NoteInfoWrapper)[]} */ 1008 + const notesDupechecked = []; 1009 + for (const invalidAndPlaceholderNote of invalidAndPlaceholderNotes) { 1010 + if (invalidAndPlaceholderNote !== null) { 1011 + notesDupechecked.push(invalidAndPlaceholderNote); 1012 + } else { 1013 + const info = infos.shift(); 1014 + if (typeof info !== 'undefined') { 1015 + notesDupechecked.push(info); 1016 + } 1017 + } 1018 + } 1019 + 992 1020 /** @type {import('display-anki').DictionaryEntryDetails[]} */ 993 1021 const results = new Array(dictionaryEntries.length).fill(null).map(() => ({noteMap: new Map()})); 994 1022 995 1023 for (let i = 0, ii = noteInfoList.length; i < ii; ++i) { 996 1024 const {note, errors, requirements} = noteInfoList[i]; 997 - const {canAdd, valid, noteIds, noteInfos} = infos[i]; 1025 + const {canAdd, valid, noteIds, noteInfos} = notesDupechecked[i]; 998 1026 const {cardFormatIndex, cardFormat, index} = noteTargets[i]; 999 1027 results[index].noteMap.set(cardFormatIndex, {cardFormat, note, errors, requirements, canAdd, valid, noteIds, noteInfos, ankiError}); 1000 1028 }