yomichan/ext/fg/js/frame.js

81 lines
2.6 KiB
JavaScript
Raw Normal View History

2016-04-24 23:18:47 +00:00
/*
* Copyright (C) 2016 Alex Yatskov <alex@foosoft.net>
* Author: Alex Yatskov <alex@foosoft.net>
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2016-09-16 04:03:58 +00:00
function invokeApi(action, params, target) {
target.postMessage({action, params}, '*');
}
2016-04-24 23:48:30 +00:00
function registerKanjiLinks() {
2016-08-21 02:32:50 +00:00
for (const link of Array.from(document.getElementsByClassName('kanji-link'))) {
2016-09-12 03:59:42 +00:00
link.addEventListener('click', e => {
2016-04-24 23:48:30 +00:00
e.preventDefault();
2016-09-16 04:03:58 +00:00
invokeApi('displayKanji', e.target.innerHTML, window.parent);
2016-04-24 23:48:30 +00:00
});
}
}
2016-06-14 16:05:40 +00:00
function registerAddNoteLinks() {
2016-08-21 02:32:50 +00:00
for (const link of Array.from(document.getElementsByClassName('action-add-note'))) {
2016-09-12 03:59:42 +00:00
link.addEventListener('click', e => {
2016-05-05 03:08:22 +00:00
e.preventDefault();
const ds = e.currentTarget.dataset;
2016-09-16 04:03:58 +00:00
invokeApi('addNote', {index: ds.index, mode: ds.mode}, window.parent);
2016-05-05 03:08:22 +00:00
});
}
}
2016-07-18 15:08:31 +00:00
function registerAudioLinks() {
2016-08-21 02:32:50 +00:00
for (const link of Array.from(document.getElementsByClassName('action-play-audio'))) {
2016-09-12 03:59:42 +00:00
link.addEventListener('click', e => {
2016-06-14 05:09:39 +00:00
e.preventDefault();
const ds = e.currentTarget.dataset;
2016-09-16 04:03:58 +00:00
invokeApi('playAudio', ds.index, window.parent);
2016-06-14 05:09:39 +00:00
});
}
}
2016-05-22 05:59:29 +00:00
function api_setActionState({index, state, sequence}) {
2016-08-21 02:32:50 +00:00
for (const mode in state) {
2016-06-14 16:05:40 +00:00
const matches = document.querySelectorAll(`.action-bar[data-sequence="${sequence}"] .action-add-note[data-index="${index}"][data-mode="${mode}"]`);
2016-05-22 05:59:29 +00:00
if (matches.length === 0) {
return;
2016-05-05 03:43:29 +00:00
}
2016-05-22 05:59:29 +00:00
const classes = matches[0].classList;
if (state[mode]) {
classes.remove('disabled');
} else {
classes.add('disabled');
}
2016-05-06 01:55:43 +00:00
}
2016-05-05 03:43:29 +00:00
}
2016-09-15 05:34:05 +00:00
document.addEventListener('DOMContentLoaded', () => {
registerKanjiLinks();
registerAddNoteLinks();
registerAudioLinks();
});
2016-09-16 03:16:23 +00:00
window.addEventListener('message', e => {
2016-09-15 05:34:05 +00:00
const {action, params} = e.data, method = window['api_' + action];
if (typeof(method) === 'function') {
method(params);
}
});