2016-08-23 02:28:30 +12:00
|
|
|
/* global chrome */
|
|
|
|
'use strict'
|
2016-06-20 08:28:28 +12:00
|
|
|
|
2016-06-28 09:19:01 +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
|
|
|
|
2016-06-28 09:19:01 +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)
|
2016-08-23 07:57:19 +12:00
|
|
|
this.xpos = m.prop(0)
|
|
|
|
this.ypos = m.prop(0)
|
2016-08-23 02:28:30 +12:00
|
|
|
this.el = m.prop(null)
|
2016-08-23 07:57:19 +12:00
|
|
|
this.progress = m.prop(0)
|
2016-08-23 02:28:30 +12:00
|
|
|
this.ondown = (e) => {
|
2016-08-23 07:57:19 +12:00
|
|
|
let rect = this.el().firstChild.getBoundingClientRect()
|
|
|
|
let offset = {x: e.pageX - rect.left - document.body.scrollLeft, y: e.pageY - rect.top - document.body.scrollTop}
|
2016-08-23 02:28:30 +12:00
|
|
|
this.dragging(true)
|
|
|
|
let onmove = (e) => {
|
|
|
|
e.preventDefault()
|
|
|
|
if (this.dragging()) {
|
2016-08-23 07:57:19 +12:00
|
|
|
this.xpos(Math.max(0, e.pageX - offset.x))
|
|
|
|
this.ypos(Math.max(0, e.pageY - offset.y))
|
|
|
|
this.move()
|
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)
|
|
|
|
}
|
2016-08-23 07:57:19 +12:00
|
|
|
this.onOpen = function (el, first) {
|
|
|
|
if (!first) {
|
|
|
|
this.el(el)
|
|
|
|
let rect = this.el().firstChild.getBoundingClientRect()
|
|
|
|
this.xpos((window.innerWidth / 2) - (rect.width / 2) + document.body.scrollLeft)
|
|
|
|
this.ypos((window.innerHeight / 2) - (rect.height / 2) + document.body.scrollTop)
|
|
|
|
this.move()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.move = () => {
|
|
|
|
this.el().style.left = this.xpos() + 'px'
|
|
|
|
this.el().style.top = this.ypos() + 'px'
|
|
|
|
}
|
|
|
|
this.createEpub = (e) => {
|
|
|
|
e.target.disabled = true
|
|
|
|
ffc.download()
|
|
|
|
.then(ffc.build.bind(ffc))
|
|
|
|
.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')
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
saveAs(file, ffc.filename)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-08-23 02:28:30 +12:00
|
|
|
},
|
|
|
|
view (ctrl, args, extras) {
|
2016-08-23 07:57:19 +12:00
|
|
|
return m('.drop-down-pop-up-container', {config: ctrl.onOpen.bind(ctrl)}, m('.drop-down-pop-up', [
|
|
|
|
m('h1', {onmousedown: ctrl.ondown}, m('i.fa.fa-book'), 'Export to EPUB', m('a.close_button', {onclick: closeDialog})),
|
2016-08-23 02:28:30 +12:00
|
|
|
m('.drop-down-pop-up-content', [
|
2016-08-23 07:57:19 +12:00
|
|
|
m('table.properties', [
|
|
|
|
m('tr', m('td.label', 'Cover image'), m('td', 'some config')),
|
|
|
|
m('tr', m('td.label', 'Chapter headings'), m('td', m(checkbox, {name: 'toggle-chapter-headings'})))
|
|
|
|
]),
|
|
|
|
m('.drop-down-pop-up-footer', [
|
|
|
|
m('button.styled_button', {onclick: ctrl.createEpub}, 'Create EPUB'),
|
|
|
|
ctrl.progress() > 0 ? m('.rating_container', m('.bars_container', m('.bar_container', m('.bar_dislike', m('.bar.bar_like', {style: {width: ctrl.progress() * 100 + '%'}}))))) : null
|
|
|
|
])
|
2016-08-23 02:28:30 +12:00
|
|
|
])
|
|
|
|
]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|