aboutsummaryrefslogtreecommitdiff
path: root/ext/fg
diff options
context:
space:
mode:
Diffstat (limited to 'ext/fg')
-rw-r--r--ext/fg/js/api.js8
-rw-r--r--ext/fg/js/client.js55
-rw-r--r--ext/fg/js/frame.js18
-rw-r--r--ext/fg/js/popup.js6
4 files changed, 42 insertions, 45 deletions
diff --git a/ext/fg/js/api.js b/ext/fg/js/api.js
index 97c14640..da5f6d5a 100644
--- a/ext/fg/js/api.js
+++ b/ext/fg/js/api.js
@@ -17,6 +17,10 @@
*/
+//
+// Background APIs
+//
+
function sendMessage(action, params, callback) {
chrome.runtime.sendMessage({action: action, params: params}, callback);
}
@@ -40,3 +44,7 @@ function getOptions(callback) {
function getState(callback) {
sendMessage('getState', null, callback);
}
+
+function canAddNotes(definitions, callback) {
+ sendMessage('canAddNotes', {definitions: definitions}, callback);
+}
diff --git a/ext/fg/js/client.js b/ext/fg/js/client.js
index 12b04066..aea2d607 100644
--- a/ext/fg/js/client.js
+++ b/ext/fg/js/client.js
@@ -26,8 +26,7 @@ class Client {
this.activateBtn = 2;
this.enabled = false;
this.options = {};
- this.results = null;
- this.xhr = null;
+ this.definitions = null;
this.fgRoot = chrome.extension.getURL('fg');
chrome.runtime.onMessage.addListener(this.onBgMessage.bind(this));
@@ -102,17 +101,18 @@ class Client {
}
range.setLength(this.options.scanLength);
- findTerm(range.text(), ({results, length}) => {
+ findTerm(range.text(), ({definitions, length}) => {
if (length === 0) {
this.hidePopup();
} else {
range.setLength(length);
renderText(
- {defs: results, root: this.fgRoot, options: this.options},
+ {defs: definitions, root: this.fgRoot, options: this.options},
'term-list.html',
(content) => {
- this.results = results;
- this.showPopup(range, content, results);
+ this.definitions = definitions;
+ this.showPopup(range, content);
+ canAddNotes(definitions, (states) => this.popup.sendMessage('setActionStates', states));
}
);
}
@@ -120,23 +120,17 @@ class Client {
}
actionAddNote(mode, index, callback) {
- callback({
- action: 'disableAction',
- params: {
- mode: mode,
- index: index
- }
- });
+
}
actionDisplayKanji(kanji) {
- findKanji(kanji, (results) => {
+ findKanji(kanji, (definitions) => {
renderText(
- {defs: results, root: this.fgRoot, options: this.options},
+ {defs: definitions, root: this.fgRoot, options: this.options},
'kanji-list.html',
(content) => {
- this.results = results;
- this.popup.setContent(content, results);
+ this.definitions = definitions;
+ this.popup.setContent(content, definitions);
}
);
});
@@ -159,8 +153,8 @@ class Client {
this.lastRange.deselect();
}
- this.lastRange = null;
- this.results = null;
+ this.lastRange = null;
+ this.definitions = null;
}
setEnabled(enabled) {
@@ -172,29 +166,6 @@ class Client {
setOptions(opts) {
this.options = opts;
}
-
- callAnkiApi(action, params, callback) {
- if (!this.options.enableAnkiConnect) {
- callback(null);
- return;
- }
-
- if (this.xhr !== null) {
- this.xhr.abort();
- }
-
- this.xhr = new XMLHttpRequest();
- this.xhr.addEventListener('loadend', () => {
- const resp = this.xhr.responseText;
- callback(resp ? JSON.parse(resp) : null);
- this.xhr = null;
- });
-
- this.xhr.open('POST', 'http://127.0.0.1:8888');
- this.xhr.withCredentials = true;
- this.xhr.setRequestHeader('Content-Type', 'text/json');
- this.xhr.send(JSON.stringify({action: action, params: params}));
- }
}
window.yomiClient = new Client();
diff --git a/ext/fg/js/frame.js b/ext/fg/js/frame.js
index 80fa099a..5a910dfc 100644
--- a/ext/fg/js/frame.js
+++ b/ext/fg/js/frame.js
@@ -43,9 +43,21 @@ function onDomContentLoaded() {
function onMessage(e) {
const {action, params} = e.data, handlers = {
- disableAction: ({mode, index}) => {
- const matches = document.querySelectorAll(`.action-link[data-index="${index}"][data-mode="${mode}"]`);
- matches[0].classList.add('disabled');
+ setActionStates: (states) => {
+ for (let i = 0, count = states.length; i < count; ++i) {
+ const state = states[i];
+ for (const mode in state) {
+ const matches = document.querySelectorAll(`.action-link[data-index="${i}"][data-mode="${mode}"]`);
+ if (matches.length > 0) {
+ const classes = matches[0].classList;
+ if (state[mode]) {
+ classes.remove('disabled');
+ } else {
+ classes.add('disabled');
+ }
+ }
+ }
+ }
}
};
diff --git a/ext/fg/js/popup.js b/ext/fg/js/popup.js
index 16a62b1c..53359cf3 100644
--- a/ext/fg/js/popup.js
+++ b/ext/fg/js/popup.js
@@ -68,6 +68,12 @@ class Popup {
doc.close();
}
+ sendMessage(action, params, callback) {
+ if (this.popup !== null) {
+ this.popup.contentWindow.postMessage({action: action, params: params}, '*');
+ }
+ }
+
inject() {
if (this.popup !== null) {
return;