-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
96 lines (77 loc) · 3.2 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
onDocumentReady(() => {
Array.from(document.querySelectorAll('td > a[href^=\'https://www.ici-japon.com/cours_japonais/kanji/\']'))
.forEach(link => {
const baseList = link.closest('table')
const hasDisplayLinksArea =
baseList.nextSibling
&& baseList.nextSibling.classList
&& baseList.nextSibling.classList.contains('kanji-list-toggler')
const displayLinksArea = hasDisplayLinksArea
? baseList.nextSibling
: makeElement('p', {class: 'kanji-list-toggler'}, element =>
baseList.parentNode.insertBefore(element, baseList.nextSibling))
if (!hasDisplayLinksArea) {
const displayLink = makeElement('a', {href: '#'})
displayLink.textContent = 'afficher les fiches complètes'
displayLink.addEventListener('click', event => {
event.preventDefault()
unsoftHide(injectedList)
softHide(displayLink)
unsoftHide(hideLink)
})
const hideLink = makeElement('a', {href: '#'}, softHide)
hideLink.textContent = 'masquer les fiches complètes'
hideLink.addEventListener('click', event => {
event.preventDefault()
softHide(injectedList)
softHide(hideLink)
unsoftHide(displayLink)
})
displayLinksArea.appendChild(displayLink)
displayLinksArea.appendChild(hideLink)
}
const injectedList =
displayLinksArea.nextSibling
&& displayLinksArea.nextSibling.classList
&& displayLinksArea.nextSibling.classList.contains('kanji-list')
? displayLinksArea.nextSibling
: makeElement('ul', {class: 'kanji-list'}, element =>
displayLinksArea.parentNode.insertBefore(element, displayLinksArea.nextSibling))
softHide(injectedList)
if (!injectedList.lastChild) injectedList.appendChild(makeElement('br', {class: 'clear-fix'}))
const clearFix = injectedList.lastChild
const kanjiItem = document.createElement('li')
softHide(kanjiItem)
const kanjiBox = makeElement('object', {data: link.href})
kanjiBox.addEventListener('load', () => {
let isAwaitingResize = false
const resizeBox = () => {
!isAwaitingResize && window.requestAnimationFrame(() => {
isAwaitingResize = false
kanjiBox.height = kanjiBox.contentDocument.body.children[0].children[0].offsetHeight
})
}
window.addEventListener('resize', resizeBox)
resizeBox()
unsoftHide(kanjiItem)
})
kanjiItem.appendChild(kanjiBox)
injectedList.insertBefore(kanjiItem, clearFix)
})
})
function makeElement(type, attributes = {}, configure = () => {}) {
const element = document.createElement(type)
Object.entries(attributes).forEach(([key, value]) => element.setAttribute(key, value))
configure(element)
return element
}
function softHide(element) {
element.classList.add('soft-hidden')
}
function unsoftHide(element) {
element.classList.remove('soft-hidden')
}
function onDocumentReady(callback) {
if (document.readyState === 'complete' || document.readyState === 'interactive') setTimeout(callback, 1)
else document.addEventListener('DOMContentLoaded', callback)
}