2017-07-21 04:21:22 +00:00
|
|
|
/*
|
2021-01-01 19:50:41 +00:00
|
|
|
* Copyright (C) 2016-2021 Yomichan Authors
|
2017-07-21 04:21:22 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2020-01-01 17:00:31 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2017-07-21 04:21:22 +00:00
|
|
|
*/
|
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
class API {
|
|
|
|
constructor(yomichan) {
|
|
|
|
this._yomichan = yomichan;
|
|
|
|
}
|
2020-06-08 01:40:11 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
optionsGet(optionsContext) {
|
|
|
|
return this._invoke('optionsGet', {optionsContext});
|
|
|
|
}
|
2020-05-24 17:30:40 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
optionsGetFull() {
|
|
|
|
return this._invoke('optionsGetFull');
|
|
|
|
}
|
2020-05-24 17:30:40 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
termsFind(text, details, optionsContext) {
|
|
|
|
return this._invoke('termsFind', {text, details, optionsContext});
|
|
|
|
}
|
2020-05-24 17:30:40 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
textParse(text, optionsContext) {
|
|
|
|
return this._invoke('textParse', {text, optionsContext});
|
|
|
|
}
|
2020-05-24 17:30:40 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
kanjiFind(text, optionsContext) {
|
|
|
|
return this._invoke('kanjiFind', {text, optionsContext});
|
|
|
|
}
|
2020-05-24 17:30:40 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
isAnkiConnected() {
|
|
|
|
return this._invoke('isAnkiConnected');
|
|
|
|
}
|
2020-05-24 17:30:40 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
getAnkiConnectVersion() {
|
|
|
|
return this._invoke('getAnkiConnectVersion');
|
|
|
|
}
|
2020-11-08 20:53:06 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
addAnkiNote(note) {
|
|
|
|
return this._invoke('addAnkiNote', {note});
|
|
|
|
}
|
2020-12-12 19:57:24 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
getAnkiNoteInfo(notes) {
|
|
|
|
return this._invoke('getAnkiNoteInfo', {notes});
|
|
|
|
}
|
2020-09-10 20:05:17 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
injectAnkiNoteMedia(timestamp, definitionDetails, audioDetails, screenshotDetails, clipboardDetails) {
|
|
|
|
return this._invoke('injectAnkiNoteMedia', {timestamp, definitionDetails, audioDetails, screenshotDetails, clipboardDetails});
|
|
|
|
}
|
2020-09-10 20:05:17 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
noteView(noteId) {
|
|
|
|
return this._invoke('noteView', {noteId});
|
|
|
|
}
|
2020-09-10 20:18:36 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
suspendAnkiCardsForNote(noteId) {
|
|
|
|
return this._invoke('suspendAnkiCardsForNote', {noteId});
|
|
|
|
}
|
2020-05-24 17:30:40 +00:00
|
|
|
|
2021-04-04 20:22:35 +00:00
|
|
|
getTermAudioInfoList(source, term, reading, details) {
|
|
|
|
return this._invoke('getTermAudioInfoList', {source, term, reading, details});
|
2021-02-14 20:53:35 +00:00
|
|
|
}
|
2021-01-15 03:42:11 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
commandExec(command, params) {
|
|
|
|
return this._invoke('commandExec', {command, params});
|
|
|
|
}
|
2020-09-26 17:41:26 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
sendMessageToFrame(frameId, action, params) {
|
|
|
|
return this._invoke('sendMessageToFrame', {frameId, action, params});
|
|
|
|
}
|
2017-07-21 04:21:22 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
broadcastTab(action, params) {
|
|
|
|
return this._invoke('broadcastTab', {action, params});
|
|
|
|
}
|
2019-08-17 22:42:36 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
frameInformationGet() {
|
|
|
|
return this._invoke('frameInformationGet');
|
|
|
|
}
|
2020-05-24 17:30:40 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
injectStylesheet(type, value) {
|
|
|
|
return this._invoke('injectStylesheet', {type, value});
|
|
|
|
}
|
2020-05-24 17:30:40 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
getStylesheetContent(url) {
|
|
|
|
return this._invoke('getStylesheetContent', {url});
|
|
|
|
}
|
2020-05-24 17:30:40 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
getEnvironmentInfo() {
|
|
|
|
return this._invoke('getEnvironmentInfo');
|
|
|
|
}
|
2020-05-24 17:30:40 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
clipboardGet() {
|
|
|
|
return this._invoke('clipboardGet');
|
|
|
|
}
|
2020-05-24 17:30:40 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
getDisplayTemplatesHtml() {
|
|
|
|
return this._invoke('getDisplayTemplatesHtml');
|
|
|
|
}
|
2020-05-24 17:50:34 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
getZoom() {
|
|
|
|
return this._invoke('getZoom');
|
|
|
|
}
|
2020-05-30 20:23:56 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
getDefaultAnkiFieldTemplates() {
|
|
|
|
return this._invoke('getDefaultAnkiFieldTemplates');
|
|
|
|
}
|
2020-07-19 00:30:10 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
getDictionaryInfo() {
|
|
|
|
return this._invoke('getDictionaryInfo');
|
|
|
|
}
|
2020-09-04 21:57:51 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
getDictionaryCounts(dictionaryNames, getTotal) {
|
|
|
|
return this._invoke('getDictionaryCounts', {dictionaryNames, getTotal});
|
|
|
|
}
|
2020-09-13 22:43:44 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
purgeDatabase() {
|
|
|
|
return this._invoke('purgeDatabase');
|
|
|
|
}
|
2021-02-09 00:40:49 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
getMedia(targets) {
|
|
|
|
return this._invoke('getMedia', {targets});
|
|
|
|
}
|
2020-05-24 17:30:40 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
log(error, level, context) {
|
|
|
|
return this._invoke('log', {error, level, context});
|
|
|
|
}
|
2020-05-24 17:30:40 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
logIndicatorClear() {
|
|
|
|
return this._invoke('logIndicatorClear');
|
|
|
|
}
|
2020-05-24 17:30:40 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
modifySettings(targets, source) {
|
|
|
|
return this._invoke('modifySettings', {targets, source});
|
|
|
|
}
|
2020-05-24 17:30:40 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
getSettings(targets) {
|
|
|
|
return this._invoke('getSettings', {targets});
|
|
|
|
}
|
2020-05-24 17:30:40 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
setAllSettings(value, source) {
|
|
|
|
return this._invoke('setAllSettings', {value, source});
|
|
|
|
}
|
2020-05-24 17:30:40 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
getOrCreateSearchPopup(details) {
|
|
|
|
return this._invoke('getOrCreateSearchPopup', isObject(details) ? details : {});
|
|
|
|
}
|
|
|
|
|
|
|
|
isTabSearchPopup(tabId) {
|
|
|
|
return this._invoke('isTabSearchPopup', {tabId});
|
|
|
|
}
|
|
|
|
|
|
|
|
triggerDatabaseUpdated(type, cause) {
|
|
|
|
return this._invoke('triggerDatabaseUpdated', {type, cause});
|
|
|
|
}
|
|
|
|
|
|
|
|
testMecab() {
|
|
|
|
return this._invoke('testMecab', {});
|
|
|
|
}
|
|
|
|
|
2021-02-28 19:18:18 +00:00
|
|
|
textHasJapaneseCharacters(text) {
|
|
|
|
return this._invoke('textHasJapaneseCharacters', {text});
|
|
|
|
}
|
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
// Utilities
|
|
|
|
|
|
|
|
_createActionPort(timeout=5000) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let timer = null;
|
|
|
|
const portDetails = deferPromise();
|
|
|
|
|
|
|
|
const onConnect = async (port) => {
|
|
|
|
try {
|
|
|
|
const {name: expectedName, id: expectedId} = await portDetails.promise;
|
|
|
|
const {name, id} = JSON.parse(port.name);
|
|
|
|
if (name !== expectedName || id !== expectedId || timer === null) { return; }
|
|
|
|
} catch (e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
clearTimeout(timer);
|
|
|
|
timer = null;
|
|
|
|
|
|
|
|
chrome.runtime.onConnect.removeListener(onConnect);
|
|
|
|
resolve(port);
|
|
|
|
};
|
2020-05-24 17:30:40 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
const onError = (e) => {
|
|
|
|
if (timer !== null) {
|
|
|
|
clearTimeout(timer);
|
|
|
|
timer = null;
|
2020-05-24 17:30:40 +00:00
|
|
|
}
|
2021-02-14 20:53:35 +00:00
|
|
|
chrome.runtime.onConnect.removeListener(onConnect);
|
|
|
|
portDetails.reject(e);
|
|
|
|
reject(e);
|
|
|
|
};
|
2020-05-24 17:30:40 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
timer = setTimeout(() => onError(new Error('Timeout')), timeout);
|
2020-05-24 17:30:40 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
chrome.runtime.onConnect.addListener(onConnect);
|
|
|
|
this._invoke('createActionPort').then(portDetails.resolve, onError);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_invokeWithProgress(action, params, onProgress, timeout=5000) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let port = null;
|
|
|
|
|
|
|
|
if (typeof onProgress !== 'function') {
|
|
|
|
onProgress = () => {};
|
|
|
|
}
|
|
|
|
|
|
|
|
const onMessage = (message) => {
|
|
|
|
switch (message.type) {
|
|
|
|
case 'progress':
|
|
|
|
try {
|
|
|
|
onProgress(...message.data);
|
|
|
|
} catch (e) {
|
|
|
|
// NOP
|
2020-05-31 22:17:12 +00:00
|
|
|
}
|
2021-02-14 20:53:35 +00:00
|
|
|
break;
|
|
|
|
case 'complete':
|
2020-05-24 17:30:40 +00:00
|
|
|
cleanup();
|
2021-02-14 20:53:35 +00:00
|
|
|
resolve(message.data);
|
|
|
|
break;
|
|
|
|
case 'error':
|
|
|
|
cleanup();
|
|
|
|
reject(deserializeError(message.data));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const onDisconnect = () => {
|
|
|
|
cleanup();
|
|
|
|
reject(new Error('Disconnected'));
|
|
|
|
};
|
|
|
|
|
|
|
|
const cleanup = () => {
|
|
|
|
if (port !== null) {
|
|
|
|
port.onMessage.removeListener(onMessage);
|
|
|
|
port.onDisconnect.removeListener(onDisconnect);
|
|
|
|
port.disconnect();
|
|
|
|
port = null;
|
|
|
|
}
|
|
|
|
onProgress = null;
|
|
|
|
};
|
2020-05-24 17:30:40 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
(async () => {
|
2020-05-24 17:30:40 +00:00
|
|
|
try {
|
2021-02-14 20:53:35 +00:00
|
|
|
port = await this._createActionPort(timeout);
|
|
|
|
port.onMessage.addListener(onMessage);
|
|
|
|
port.onDisconnect.addListener(onDisconnect);
|
|
|
|
|
|
|
|
// Chrome has a maximum message size that can be sent, so longer messages must be fragmented.
|
|
|
|
const messageString = JSON.stringify({action, params});
|
|
|
|
const fragmentSize = 1e7; // 10 MB
|
|
|
|
for (let i = 0, ii = messageString.length; i < ii; i += fragmentSize) {
|
|
|
|
const data = messageString.substring(i, i + fragmentSize);
|
|
|
|
port.postMessage({action: 'fragment', data});
|
|
|
|
}
|
|
|
|
port.postMessage({action: 'invoke'});
|
2020-05-24 17:30:40 +00:00
|
|
|
} catch (e) {
|
2021-02-14 20:53:35 +00:00
|
|
|
cleanup();
|
2020-05-24 17:30:40 +00:00
|
|
|
reject(e);
|
2021-02-14 20:53:35 +00:00
|
|
|
} finally {
|
|
|
|
action = null;
|
|
|
|
params = null;
|
2019-11-26 22:23:55 +00:00
|
|
|
}
|
2021-02-14 20:53:35 +00:00
|
|
|
})();
|
|
|
|
});
|
|
|
|
}
|
2021-02-14 20:19:31 +00:00
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
_invoke(action, params={}) {
|
|
|
|
const data = {action, params};
|
|
|
|
return new Promise((resolve, reject) => {
|
2021-02-14 20:19:31 +00:00
|
|
|
try {
|
2021-02-14 20:53:35 +00:00
|
|
|
this._yomichan.sendMessage(data, (response) => {
|
|
|
|
this._checkLastError(chrome.runtime.lastError);
|
|
|
|
if (response !== null && typeof response === 'object') {
|
|
|
|
if (typeof response.error !== 'undefined') {
|
|
|
|
reject(deserializeError(response.error));
|
|
|
|
} else {
|
|
|
|
resolve(response.result);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const message = response === null ? 'Unexpected null response' : `Unexpected response of type ${typeof response}`;
|
|
|
|
reject(new Error(`${message} (${JSON.stringify(data)})`));
|
|
|
|
}
|
|
|
|
});
|
2021-02-14 20:19:31 +00:00
|
|
|
} catch (e) {
|
2021-02-14 20:53:35 +00:00
|
|
|
reject(e);
|
2021-02-14 20:19:31 +00:00
|
|
|
}
|
2021-02-14 20:53:35 +00:00
|
|
|
});
|
2020-05-24 17:30:40 +00:00
|
|
|
}
|
|
|
|
|
2021-02-14 20:53:35 +00:00
|
|
|
_checkLastError() {
|
|
|
|
// NOP
|
|
|
|
}
|
|
|
|
}
|