fimfic2epub/src/main.js

119 lines
3.3 KiB
JavaScript
Raw Normal View History

2016-08-23 02:28:30 +12:00
/* global chrome */
'use strict'
2016-06-20 08:28:28 +12:00
import FimFic2Epub from './FimFic2Epub'
2016-08-23 02:28:30 +12:00
import m from 'mithril'
2016-08-15 21:11:20 +12:00
import { saveAs } from 'file-saver'
function blobToDataURL (blob, callback) {
let a = new FileReader()
a.onloadend = function (e) { callback(a.result) }
a.readAsDataURL(blob)
}
2016-06-20 08:28:28 +12:00
2016-08-23 02:28:30 +12:00
const isChromeExt = typeof chrome !== 'undefined'
2016-06-21 09:04:08 +12:00
const STORY_ID = document.location.pathname.match(/^\/story\/(\d*)/)[1]
2016-06-20 08:28:28 +12:00
2016-08-23 02:28:30 +12:00
let ffc
2016-06-20 08:28:28 +12:00
const epubButton = document.querySelector('.story_container ul.chapters li.bottom a[title="Download Story (.epub)"]')
2016-06-21 09:04:08 +12:00
2016-08-23 02:28:30 +12:00
const dialogContainer = document.createElement('div')
dialogContainer.id = 'epubDialogContainer'
document.body.appendChild(dialogContainer)
let checkbox = {
view: function (ctrl, args, text) {
return m('label.toggleable-switch', [
m('input', {type: 'checkbox', name: args.name, checked: args.checked}),
m('a'),
text
])
}
}
let dialog = {
controller (args) {
this.dragging = m.prop(false)
this.xpos = m.prop(100)
this.ypos = m.prop(100)
this.el = m.prop(null)
this.ondown = (e) => {
let el = this.el().firstChild
let rect = el.getBoundingClientRect()
let offset = {x: e.pageX - rect.left, y: e.pageY - rect.top}
this.dragging(true)
let onmove = (e) => {
e.preventDefault()
if (this.dragging()) {
let rect = el.getBoundingClientRect()
this.xpos(Math.max(0, Math.min(e.pageX - offset.x, window.innerWidth - rect.width)))
this.ypos(Math.max(0, Math.min(e.pageY - offset.y, window.innerHeight - rect.height)))
// console.log(e.pageX, e.pageY)
m.redraw()
2016-08-15 21:11:20 +12:00
}
2016-08-23 02:28:30 +12:00
}
let onup = () => {
this.dragging(false)
window.removeEventListener('mousemove', onmove)
window.removeEventListener('mouseup', onup)
}
window.addEventListener('mousemove', onmove, false)
window.addEventListener('mouseup', onup, false)
}
},
view (ctrl, args, extras) {
return m('.drop-down-pop-up-container', {config: ctrl.el, style: {left: ctrl.xpos() + 'px', top: ctrl.ypos() + 'px'}}, m('.drop-down-pop-up', [
m('h1', {onmousedown: ctrl.ondown}, m('i.fa.fa-book'), 'Export EPUB', m('a.close_button', {onclick: closeDialog})),
m('.drop-down-pop-up-content', [
m(checkbox, {name: 'toggle-chapter-headings'}, 'Toggle chapter headings')
])
]))
}
}
function openDialog (args, extras) {
m.mount(dialogContainer, m(dialog, args, extras))
}
function closeDialog () {
m.mount(dialogContainer, null)
}
function clickButton () {
if (!STORY_ID) return
if (!ffc) ffc = new FimFic2Epub(STORY_ID)
openDialog()
return
ffc.download().then(ffc.getFile.bind(ffc)).then((file) => {
console.log('Saving file...')
if (typeof safari !== 'undefined') {
blobToDataURL(file, (dataurl) => {
document.location.href = dataurl
alert('Add .epub to the filename of the downloaded file')
2016-08-15 21:11:20 +12:00
})
2016-08-23 02:28:30 +12:00
} else {
saveAs(file, ffc.filename)
}
})
}
if (epubButton) {
if (isChromeExt) {
chrome.runtime.sendMessage({showPageAction: true})
chrome.runtime.onMessage.addListener(function (request) {
if (request === 'pageAction') {
clickButton()
}
2016-06-28 19:39:31 +12:00
})
2016-08-23 02:28:30 +12:00
}
epubButton.addEventListener('click', function (e) {
e.preventDefault()
clickButton()
2016-06-21 18:39:26 +12:00
}, false)
2016-06-21 09:04:08 +12:00
}