yomichan/ext/fg/js/frame.js

80 lines
2.7 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-04-24 23:48:30 +00:00
function registerKanjiLinks() {
for (let link of [].slice.call(document.getElementsByClassName('kanji-link'))) {
2016-04-24 23:48:30 +00:00
link.addEventListener('click', (e) => {
e.preventDefault();
2016-05-06 01:55:43 +00:00
window.parent.postMessage({action: 'displayKanji', params: e.target.innerHTML}, '*');
2016-04-24 23:48:30 +00:00
});
}
}
2016-06-14 16:05:40 +00:00
function registerAddNoteLinks() {
for (let link of [].slice.call(document.getElementsByClassName('action-add-note'))) {
2016-05-05 03:08:22 +00:00
link.addEventListener('click', (e) => {
e.preventDefault();
const ds = e.currentTarget.dataset;
2016-05-06 01:55:43 +00:00
window.parent.postMessage({action: 'addNote', params: {index: ds.index, mode: ds.mode}}, '*');
2016-05-05 03:08:22 +00:00
});
}
}
2016-06-14 05:09:39 +00:00
function registerPronounceLinks() {
for (let link of [].slice.call(document.getElementsByClassName('action-pronounce'))) {
2016-06-14 05:09:39 +00:00
link.addEventListener('click', (e) => {
e.preventDefault();
const ds = e.currentTarget.dataset;
window.parent.postMessage({action: 'pronounce', params: ds.index}, '*');
});
}
}
2016-05-05 03:43:29 +00:00
function onDomContentLoaded() {
2016-05-05 03:08:22 +00:00
registerKanjiLinks();
2016-06-14 16:05:40 +00:00
registerAddNoteLinks();
2016-06-14 05:09:39 +00:00
registerPronounceLinks();
2016-05-05 03:08:22 +00:00
}
2016-05-05 03:43:29 +00:00
function onMessage(e) {
2016-05-22 05:59:29 +00:00
const {action, params} = e.data, method = window['api_' + action];
if (typeof(method) === 'function') {
method(params);
}
}
2016-05-06 03:23:06 +00:00
2016-05-22 05:59:29 +00:00
function api_setActionState({index, state, sequence}) {
for (let 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
}
document.addEventListener('DOMContentLoaded', onDomContentLoaded, false);
window.addEventListener('message', onMessage);