yomichan/ext/fg/js/frame.js

69 lines
2.3 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 (const link of [].slice.call(document.getElementsByClassName('kanji-link'))) {
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-05-05 03:08:22 +00:00
function registerActionLinks() {
for (const link of [].slice.call(document.getElementsByClassName('action-link'))) {
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-05-05 03:43:29 +00:00
function onDomContentLoaded() {
2016-05-05 03:08:22 +00:00
registerKanjiLinks();
registerActionLinks();
}
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 (const mode in state) {
const matches = document.querySelectorAll(`.action-link[data-sequence="${sequence}"][data-index="${index}"][data-mode="${mode}"]`);
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);