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'
2017-10-19 02:02:07 +13:00
import prop from 'mithril/stream'
2018-03-26 19:50:45 +13:00
import { saveAs } from 'file-saver'
2016-08-24 19:09:43 +12:00
import autosize from 'autosize'
2018-03-13 23:10:04 +13:00
import { htmlToText } from './utils'
2016-08-15 21:11:20 +12:00
2019-10-08 22:31:42 +13:00
m . withAttr = function ( attrName , callback , context ) {
return function ( e ) {
callback . call ( context || this , attrName in e . currentTarget ? e . currentTarget [ attrName ] : e . currentTarget . getAttribute ( attrName ) )
}
2019-10-08 22:26:15 +13:00
}
2016-08-24 08:04:38 +12:00
function blobToDataURL ( blob ) {
return new Promise ( ( resolve , reject ) => {
2019-10-08 22:31:42 +13:00
const fr = new FileReader ( )
2016-08-24 08:04:38 +12:00
fr . onloadend = function ( e ) { resolve ( fr . result ) }
fr . readAsDataURL ( blob )
} )
2016-08-24 02:32:55 +12:00
}
function blobToArrayBuffer ( blob ) {
return new Promise ( ( resolve , reject ) => {
2019-10-08 22:31:42 +13:00
const fr = new FileReader ( )
2016-08-24 02:32:55 +12:00
fr . onloadend = function ( e ) { resolve ( fr . result ) }
fr . readAsArrayBuffer ( blob )
} )
2016-08-15 21:11:20 +12:00
}
2016-06-20 08:28:28 +12:00
2016-08-23 02:28:30 +12:00
const isChromeExt = typeof chrome !== 'undefined'
2016-08-28 05:04:22 +12:00
let pageStoryId
try {
pageStoryId = document . location . pathname . match ( /^\/story\/(\d*)/ ) [ 1 ]
} catch ( e ) { }
2019-10-08 22:31:42 +13:00
const logoUrl = chrome . extension . getURL ( 'fimfic2epub-logo.png' )
2016-08-23 02:28:30 +12:00
let ffc
2019-10-08 22:31:42 +13:00
const stories = document . querySelectorAll ( '.story_container' )
2016-08-28 05:04:22 +12:00
stories . forEach ( ( story ) => {
2019-10-08 22:31:42 +13:00
const id = story . dataset . story
2017-06-13 03:44:05 +12:00
function epubClick ( e ) {
2016-08-28 05:04:22 +12:00
e . preventDefault ( )
openStory ( id )
2017-06-13 03:44:05 +12:00
}
2019-10-08 22:31:42 +13:00
const epubButtons = story . querySelectorAll ( '.drop-down ul li a[title="Download Story (.epub)"]' )
2017-06-13 03:44:05 +12:00
if ( epubButtons . length === 0 ) return
for ( let i = 0 ; i < epubButtons . length ; i ++ ) {
epubButtons [ i ] . addEventListener ( 'click' , epubClick , false )
}
2019-10-08 22:31:42 +13:00
const logo = new Image ( )
2016-08-28 05:04:22 +12:00
logo . className = 'fimfic2epub-logo'
logo . title = 'Download EPUB with fimfic2epub'
logo . src = logoUrl
2017-06-07 08:15:05 +12:00
story . querySelector ( '.story_content_box .title' ) . appendChild ( logo )
2017-06-13 03:44:05 +12:00
logo . addEventListener ( 'click' , epubClick , false )
2016-08-28 05:04:22 +12:00
} )
2019-10-08 22:31:42 +13:00
const cards = document . querySelectorAll ( '.story-card-container' )
2016-08-28 05:04:22 +12:00
cards . forEach ( ( card ) => {
let id
2019-10-08 22:31:42 +13:00
const classes = card . className . split ( ' ' )
2016-08-28 05:04:22 +12:00
for ( let i = 0 ; i < classes . length && ! id ; i ++ ) {
2019-10-08 22:31:42 +13:00
const c = classes [ i ]
2016-08-28 05:04:22 +12:00
id = c . substring ( 21 )
}
if ( ! id ) return
2019-10-08 22:31:42 +13:00
const flip = card . querySelector ( 'a.card-flip' )
const epubButton = card . querySelector ( 'a[title="Download .ePub"]' )
2016-08-28 05:04:22 +12:00
if ( ! epubButton ) return
epubButton . addEventListener ( 'click' , function ( e ) {
e . preventDefault ( )
openStory ( id )
flip . click ( )
} , false )
} )
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 )
2019-10-08 22:31:42 +13:00
const checkbox = {
2019-10-08 19:37:27 +13:00
view : function ( { attrs , children } ) {
2016-08-30 02:20:20 +12:00
return m ( 'label.toggleable-switch' , [
m ( 'input' , Object . assign ( {
type : 'checkbox'
2017-10-19 02:02:07 +13:00
} , attrs ) ) ,
2016-08-30 02:20:20 +12:00
m ( 'a' ) ,
2017-10-19 02:02:07 +13:00
children ? m ( 'span' , children ) : null
2016-08-23 02:28:30 +12:00
] )
}
}
2017-06-07 08:15:05 +12:00
function selectOptions ( list , selected = '' ) {
return list . map ( ( item ) => {
return m ( 'option' , {
value : item [ 0 ] ,
selected : selected === item [ 0 ]
} , item [ 1 ] )
} )
}
2017-06-08 02:17:40 +12:00
function redraw ( arg ) {
try {
m . redraw ( arg )
} catch ( err ) {
console . log ( err )
}
}
2019-10-08 22:31:42 +13:00
const ffcProgress = prop ( 0 )
const ffcStatus = prop ( '' )
2016-08-24 02:32:55 +12:00
2019-10-08 22:31:42 +13:00
const dialog = {
2017-10-19 02:02:07 +13:00
oninit ( ) {
2016-08-24 19:09:43 +12:00
const ctrl = this
2016-08-25 00:47:48 +12:00
ffcProgress ( 0 )
2017-10-19 02:02:07 +13:00
this . isLoading = prop ( true )
this . dragging = prop ( false )
this . xpos = prop ( 0 )
this . ypos = prop ( 0 )
this . el = prop ( null )
this . coverFile = prop ( null )
this . coverUrl = prop ( '' )
this . checkboxCoverUrl = prop ( false )
this . title = prop ( '' )
this . author = prop ( '' )
this . description = prop ( '' )
this . subjects = prop ( [ ] )
2018-03-16 05:43:11 +13:00
this . typogrify = prop ( ffc . options . typogrify )
2017-10-19 02:02:07 +13:00
this . addCommentsLink = prop ( ffc . options . addCommentsLink )
this . includeAuthorNotes = prop ( ffc . options . includeAuthorNotes )
this . useAuthorNotesIndex = prop ( ffc . options . useAuthorNotesIndex )
2018-07-31 20:27:49 +12:00
this . showChapterHeadings = prop ( ffc . options . showChapterHeadings )
this . showChapterWordCount = prop ( ffc . options . showChapterWordCount )
this . showChapterDuration = prop ( ffc . options . showChapterDuration )
2017-10-19 02:02:07 +13:00
this . includeExternal = prop ( ffc . options . includeExternal )
2018-03-27 07:32:02 +13:00
this . kepubify = prop ( ffc . options . kepubify )
2017-10-19 02:02:07 +13:00
this . joinSubjects = prop ( ffc . options . joinSubjects )
this . paragraphStyle = prop ( ffc . options . paragraphStyle )
2018-03-13 23:10:04 +13:00
this . calculateReadingEase = prop ( ffc . options . calculateReadingEase )
2018-03-16 04:14:57 +13:00
this . addChapterBars = prop ( ffc . options . addChapterBars )
this . wordsPerMinute = prop ( ffc . options . wordsPerMinute )
2017-10-19 02:02:07 +13:00
2018-03-13 10:08:16 +13:00
this . onOpen = ( vnode ) => {
2017-10-19 02:02:07 +13:00
this . el ( vnode . dom )
this . center ( )
this . isLoading ( true )
ffc . fetchMetadata ( ) . then ( ( ) => {
this . isLoading ( false )
ffcProgress ( - 1 )
this . title ( ffc . storyInfo . title )
this . author ( ffc . storyInfo . author . name )
2018-03-13 23:10:04 +13:00
this . description ( htmlToText ( ffc . storyInfo . description ) || ffc . storyInfo . short _description )
2017-10-19 02:02:07 +13:00
this . subjects ( ffc . subjects . slice ( 0 ) )
redraw ( true )
2018-03-16 04:14:57 +13:00
// this.center()
2017-10-19 02:02:07 +13:00
ffc . fetchChapters ( ) . then ( ( ) => {
2016-08-25 00:47:48 +12:00
ffcProgress ( - 1 )
2017-10-19 02:02:07 +13:00
redraw ( )
2016-08-25 00:47:48 +12:00
} )
2017-10-19 02:02:07 +13:00
} ) . catch ( ( err ) => {
2018-03-13 10:08:16 +13:00
console . error ( err )
2017-10-19 02:02:07 +13:00
} )
2016-08-25 00:47:48 +12:00
}
2016-08-24 02:32:55 +12:00
this . setCoverFile = ( e ) => {
2019-10-08 22:31:42 +13:00
const el = e . dom || e . target
2019-04-11 01:31:29 +12:00
if ( el . target ) {
this . coverUrl ( '' )
}
this . coverFile ( el . files ? el . files [ 0 ] : null )
2016-08-24 02:32:55 +12:00
}
2016-08-24 19:09:43 +12:00
this . setSubjects = function ( ) {
// 'this' is the textarea
2019-10-08 22:31:42 +13:00
const set = new Set ( )
2016-08-25 00:47:48 +12:00
ctrl . subjects ( this . value . split ( '\n' ) . map ( ( s ) => s . trim ( ) ) . filter ( ( s ) => {
if ( ! s ) return false
if ( set . has ( s ) ) return false
set . add ( s )
return true
} ) )
2016-08-24 19:09:43 +12:00
this . value = ctrl . subjects ( ) . join ( '\n' )
autosize . update ( this )
}
2016-08-30 02:20:20 +12:00
this . setDescription = function ( ) {
ctrl . description ( this . value . trim ( ) )
this . value = ctrl . description ( )
autosize . update ( this )
}
2016-08-23 02:28:30 +12:00
this . ondown = ( e ) => {
2019-10-08 22:31:42 +13:00
const rect = this . el ( ) . firstChild . getBoundingClientRect ( )
const offset = { x : e . pageX - rect . left - document . documentElement . scrollLeft , y : e . pageY - rect . top - document . documentElement . scrollTop }
2016-08-23 02:28:30 +12:00
this . dragging ( true )
2019-10-08 22:31:42 +13:00
const onmove = ( e ) => {
2016-08-23 02:28:30 +12:00
e . preventDefault ( )
if ( this . dragging ( ) ) {
2016-08-24 09:49:27 +12:00
this . move ( e . pageX - offset . x , e . pageY - offset . y )
2016-08-15 21:11:20 +12:00
}
2016-08-23 02:28:30 +12:00
}
2019-10-08 22:31:42 +13:00
const onup = ( ) => {
2016-08-23 02:28:30 +12:00
this . dragging ( false )
window . removeEventListener ( 'mousemove' , onmove )
window . removeEventListener ( 'mouseup' , onup )
}
window . addEventListener ( 'mousemove' , onmove , false )
window . addEventListener ( 'mouseup' , onup , false )
}
2016-08-25 00:47:48 +12:00
2016-08-24 09:49:27 +12:00
this . move = ( xpos , ypos ) => {
2019-10-08 22:31:42 +13:00
const bc = document . querySelector ( '.body_container' )
const rect = this . el ( ) . firstChild . getBoundingClientRect ( )
2016-08-25 00:47:48 +12:00
this . xpos ( Math . max ( 0 , Math . min ( xpos , bc . offsetWidth - rect . width ) ) )
this . ypos ( Math . max ( 0 , Math . min ( ypos , bc . offsetHeight - rect . height ) ) )
2016-08-23 07:57:19 +12:00
this . el ( ) . style . left = this . xpos ( ) + 'px'
this . el ( ) . style . top = this . ypos ( ) + 'px'
}
2016-08-24 08:04:38 +12:00
this . center = ( ) => {
2016-08-25 00:47:48 +12:00
if ( this . dragging ( ) ) return
2019-10-08 22:31:42 +13:00
const rect = this . el ( ) . firstChild . getBoundingClientRect ( )
2016-08-24 09:49:27 +12:00
this . move (
2017-10-19 02:02:07 +13:00
Math . max ( document . documentElement . scrollLeft , ( window . innerWidth / 2 ) - ( rect . width / 2 ) + document . documentElement . scrollLeft ) ,
2018-03-16 04:14:57 +13:00
Math . max ( document . documentElement . scrollTop , 100 + document . documentElement . scrollTop )
2016-08-24 09:49:27 +12:00
)
2016-08-24 08:04:38 +12:00
}
2016-08-23 07:57:19 +12:00
this . createEpub = ( e ) => {
e . target . disabled = true
2018-03-13 10:08:16 +13:00
createEpub ( this )
2016-08-23 07:57:19 +12:00
}
2016-08-23 02:28:30 +12:00
} ,
2016-08-24 02:32:55 +12:00
2017-10-19 02:02:07 +13:00
view ( vnode ) {
2019-10-08 22:31:42 +13:00
const ctrl = vnode . state
2019-10-08 19:37:27 +13:00
return m ( '.drop-down-pop-up-container' , { oncreate : ctrl . onOpen . bind ( ctrl ) } , m ( '.drop-down-pop-up' , { style : { 'min-width' : '720px' } } , [
m ( 'h1' , { onmousedown : ctrl . ondown } , m ( 'i.fa.fa-book' ) , 'Export to EPUB (v' + FIMFIC2EPUB _VERSION + ')' , m ( 'a.close_button' , { onclick : closeDialog } ) ) ,
2016-08-25 00:47:48 +12:00
m ( '.drop-down-pop-up-content' , [
2019-10-08 19:37:27 +13:00
ctrl . isLoading ( ) ? m ( 'div' , { style : 'text-align:center;' } , m ( 'i.fa.fa-spin.fa-spinner' , { style : 'font-size:50px; margin:20px; color:#777;' } ) ) : m ( 'table.properties' , [
m ( 'tr' , m ( 'td.section_header' , { colspan : 3 } , m ( 'b' , 'General settings' ) ) ) ,
m ( 'tr' , m ( 'td.label' , 'Title' ) , m ( 'td' , { colspan : 2 } , m ( 'input' , { type : 'text' , value : ctrl . title ( ) , onchange : m . withAttr ( 'value' , ctrl . title ) } ) ) ) ,
m ( 'tr' , m ( 'td.label' , 'Author' ) , m ( 'td' , { colspan : 2 } , m ( 'input' , { type : 'text' , value : ctrl . author ( ) , onchange : m . withAttr ( 'value' , ctrl . author ) } ) ) ) ,
2016-08-24 09:49:27 +12:00
m ( 'tr' , m ( 'td.label' , 'Custom cover image' ) ,
m ( 'td' ,
2019-10-08 19:37:27 +13:00
ctrl . checkboxCoverUrl ( ) ? m ( 'input' , { type : 'url' , placeholder : 'Image URL' , onchange : m . withAttr ( 'value' , ctrl . coverUrl ) } ) : m ( 'input' , { type : 'file' , accept : 'image/*' , onchange : ctrl . setCoverFile , onupdate : ctrl . setCoverFile } )
2016-08-24 09:49:27 +12:00
) ,
2019-10-08 19:37:27 +13:00
m ( 'td' , { style : 'width: 1px' } , m ( checkbox , { checked : ctrl . checkboxCoverUrl ( ) , onchange : m . withAttr ( 'checked' , ctrl . checkboxCoverUrl ) } , 'Use image URL' ) )
2016-08-24 09:49:27 +12:00
) ,
2019-10-08 19:37:27 +13:00
m ( 'tr' , m ( 'td.label' , 'Paragraph style' ) , m ( 'td' , { colspan : 2 } ,
m ( 'select' , { onchange : m . withAttr ( 'value' , ctrl . paragraphStyle ) } , selectOptions ( [
2017-06-07 08:15:05 +12:00
[ 'indented' , 'Indent first line in all paragraphs except the first (Traditional Paperback)' ] ,
[ 'spaced' , 'Separate each paragraph with double space (Traditional Web)' ] ,
[ 'both' , 'Double space and indent all paragraphs except first (Fusion)' ] ,
[ 'indentedall' , 'Indent all paragraphs including the first (Modified Traditional)' ]
] , ctrl . paragraphStyle ( ) ) )
) ) ,
2019-10-08 19:37:27 +13:00
m ( 'tr' , m ( 'td.label' , { style : 'vertical-align: top;' } , 'Options' ) , m ( 'td' , { colspan : 2 } ,
m ( checkbox , { checked : ctrl . typogrify ( ) , onchange : m . withAttr ( 'checked' , ctrl . typogrify ) } , 'Apply typographic fixes (smart quotes, dashes etc.)' ) ,
m ( checkbox , { checked : ctrl . showChapterHeadings ( ) , onchange : m . withAttr ( 'checked' , ctrl . showChapterHeadings ) } , 'Add chapter headings' ) ,
m ( checkbox , { checked : ctrl . showChapterWordCount ( ) , onchange : m . withAttr ( 'checked' , ctrl . showChapterWordCount ) , disabled : ! ctrl . showChapterHeadings ( ) } , 'Include word count in chapter heading' ) ,
m ( checkbox , { checked : ctrl . showChapterDuration ( ) , onchange : m . withAttr ( 'checked' , ctrl . showChapterDuration ) , disabled : ! ctrl . showChapterHeadings ( ) } , 'Include time to read in chapter heading' ) ,
m ( checkbox , { checked : ctrl . addCommentsLink ( ) , onchange : m . withAttr ( 'checked' , ctrl . addCommentsLink ) } , 'Add link to online comments (at the end of chapters)' ) ,
m ( checkbox , { checked : ctrl . includeAuthorNotes ( ) , onchange : m . withAttr ( 'checked' , ctrl . includeAuthorNotes ) } , 'Include author\'s notes' ) ,
m ( checkbox , { checked : ctrl . useAuthorNotesIndex ( ) , onchange : m . withAttr ( 'checked' , ctrl . useAuthorNotesIndex ) , disabled : ! ctrl . includeAuthorNotes ( ) } , 'Put all notes at the end of the ebook' ) ,
m ( checkbox , { checked : ctrl . calculateReadingEase ( ) , onchange : m . withAttr ( 'checked' , ctrl . calculateReadingEase ) } , 'Calculate Flesch reading ease' ) ,
m ( checkbox , { checked : ctrl . addChapterBars ( ) , onchange : m . withAttr ( 'checked' , ctrl . addChapterBars ) } , 'Show reading progress and chapter lengths as bars' ) ,
m ( checkbox , { checked : ctrl . includeExternal ( ) , onchange : m . withAttr ( 'checked' , ctrl . includeExternal ) } , 'Download & include remote content (embed images)' ) ,
m ( 'div' , { style : 'font-size: 0.9em; line-height: 1em; margin-top: 4px; margin-bottom: 6px; opacity: 0.6;' } , 'Note: Disabling this creates invalid EPUBs and requires internet access to see remote content. Only cover image will be embedded.' ) ,
m ( checkbox , { checked : ctrl . kepubify ( ) , onchange : m . withAttr ( 'checked' , ctrl . kepubify ) } , 'Export as Kobo EPUB, this adds some Kobo-specific div/span tags.' )
2018-03-16 04:14:57 +13:00
) ) ,
2019-10-08 19:37:27 +13:00
m ( 'tr' , m ( 'td.label' , 'Words per minute' ) , m ( 'td' , { colspan : 2 } ,
m ( 'input' , { type : 'number' , min : 0 , step : 1 , value : ctrl . wordsPerMinute ( ) , onchange : m . withAttr ( 'value' , ctrl . wordsPerMinute ) , placeholder : '200 (default)' , style : { width : '140px' , float : 'left' , marginRight : '.75rem' , marginTop : '.35rem' , position : 'relative' , zIndex : 1 } } ) ,
m ( 'div' , { style : 'font-size: 0.9em; line-height: 1em; margin-top: 4px; margin-bottom: 6px; opacity: 0.6;' } , 'This is used to estimate the time it takes to read the story. Take a test to find out your reading speed.' , m ( 'br' ) , 'Set to 0 to disable.' )
2016-08-24 09:49:27 +12:00
) ) ,
2019-10-08 19:37:27 +13:00
m ( 'tr' , m ( 'td.section_header' , { colspan : 3 } , m ( 'b' , 'Metadata customization' ) ) ) ,
m ( 'tr' , m ( 'td.label' , { style : 'vertical-align: top;' } , 'Description' ) , m ( 'td' , { colspan : 2 } , m ( 'textarea' , { oncreate : ( { dom } ) => autosize ( dom ) , onchange : ctrl . setDescription } , ctrl . description ( ) ) ) ) ,
m ( 'tr' , m ( 'td.label' , { style : 'vertical-align: top;' } , 'Categories' ) , m ( 'td' , { colspan : 2 } ,
m ( 'textarea' , { rows : 2 , oncreate : ( { dom } ) => autosize ( dom ) , onchange : ctrl . setSubjects } , ctrl . subjects ( ) . join ( '\n' ) ) ,
m ( checkbox , { checked : ctrl . joinSubjects ( ) , onchange : m . withAttr ( 'checked' , ctrl . joinSubjects ) } , 'Join categories and separate with commas' )
2016-08-24 02:32:55 +12:00
) )
2016-08-23 07:57:19 +12:00
] ) ,
m ( '.drop-down-pop-up-footer' , [
2019-10-08 19:37:27 +13:00
m ( 'button.styled_button' , { onclick : ctrl . createEpub , disabled : ffcProgress ( ) >= 0 && ffcProgress ( ) < 1 , style : 'float: right' } , 'Download ' + ( ctrl . kepubify ( ) ? 'Kobo EPUB' : 'EPUB' ) ) ,
2016-08-25 00:47:48 +12:00
ffcProgress ( ) >= 0 ? m ( '.rating_container' ,
2019-10-08 19:37:27 +13:00
m ( '.rating-bar' , { style : { background : 'rgba(0, 0, 0, 0.2)' , 'margin-right' : '5px' } } , m ( '.like-bar' , { style : { width : Math . max ( 0 , ffcProgress ( ) ) * 100 + '%' } } ) ) ,
2016-08-24 02:32:55 +12:00
' ' ,
2019-10-08 22:31:42 +13:00
ffcProgress ( ) >= 0 && ffcProgress ( ) < 1 ? [ m ( 'i.fa.fa-spin.fa-spinner' ) , m . trust ( ' ' ) ] : null ,
2016-08-24 02:32:55 +12:00
ffcStatus ( )
2016-08-25 00:47:48 +12:00
) : null ,
2019-10-08 19:37:27 +13:00
m ( 'div' , { style : 'clear: both' } )
2016-08-23 07:57:19 +12:00
] )
2016-08-23 02:28:30 +12:00
] )
] ) )
}
}
2016-08-24 09:49:27 +12:00
let dialogOpen = false
2017-10-19 02:02:07 +13:00
function openDialog ( ) {
2016-08-24 09:49:27 +12:00
if ( dialogOpen ) {
return
}
dialogOpen = true
2017-10-19 02:02:07 +13:00
m . mount ( dialogContainer , dialog )
2016-08-23 02:28:30 +12:00
}
function closeDialog ( ) {
2016-08-24 09:49:27 +12:00
dialogOpen = false
2016-08-23 02:28:30 +12:00
m . mount ( dialogContainer , null )
}
2018-03-13 10:08:16 +13:00
function createEpub ( model ) {
ffcProgress ( 0 )
ffcStatus ( '' )
let chain = Promise . resolve ( )
ffc . coverUrl = ''
ffc . coverImage = null
if ( model . checkboxCoverUrl ( ) ) {
ffc . coverUrl = model . coverUrl ( )
} else if ( model . coverFile ( ) ) {
chain = chain
. then ( ( ) => blobToArrayBuffer ( model . coverFile ( ) ) )
2019-04-11 01:31:29 +12:00
. then ( ( buf ) => {
ffc . setCoverImage ( buf )
} ) . catch ( ( err ) => console . error ( err ) )
2018-03-13 10:08:16 +13:00
}
ffc . setTitle ( model . title ( ) )
ffc . setAuthorName ( model . author ( ) )
ffc . storyInfo . short _description = model . description ( )
2018-03-16 05:43:11 +13:00
ffc . options . typogrify = model . typogrify ( )
2018-03-13 10:08:16 +13:00
ffc . options . addCommentsLink = model . addCommentsLink ( )
ffc . options . includeAuthorNotes = model . includeAuthorNotes ( )
ffc . options . useAuthorNotesIndex = model . useAuthorNotesIndex ( )
2018-07-31 20:27:49 +12:00
ffc . options . showChapterHeadings = model . showChapterHeadings ( )
ffc . options . showChapterWordCount = model . showChapterWordCount ( )
ffc . options . showChapterDuration = model . showChapterDuration ( )
2018-03-13 10:08:16 +13:00
ffc . options . includeExternal = model . includeExternal ( )
ffc . options . paragraphStyle = model . paragraphStyle ( )
2018-03-27 07:32:02 +13:00
ffc . options . kepubify = model . kepubify ( )
2018-03-13 10:08:16 +13:00
ffc . subjects = model . subjects ( )
ffc . options . joinSubjects = model . joinSubjects ( )
2018-03-13 23:10:04 +13:00
ffc . options . calculateReadingEase = model . calculateReadingEase ( )
2018-03-16 04:14:57 +13:00
ffc . options . addChapterBars = model . addChapterBars ( )
if ( model . wordsPerMinute ( ) === '' ) ffc . options . wordsPerMinute = 200
else ffc . options . wordsPerMinute = parseInt ( model . wordsPerMinute ( ) , 10 ) || 0
model . wordsPerMinute ( ffc . options . wordsPerMinute )
2018-03-13 10:08:16 +13:00
redraw ( )
2019-10-08 19:37:27 +13:00
chrome . storage . sync . set ( { ffcOptions : ffc . options , version : FIMFIC2EPUB _VERSION } )
2018-03-16 05:42:11 +13:00
2018-03-13 10:08:16 +13:00
chain
. then ( ffc . fetchAll . bind ( ffc ) )
. then ( ffc . build . bind ( ffc ) )
. then ( ffc . getFile . bind ( ffc ) ) . then ( ( file ) => {
if ( typeof safari !== 'undefined' ) {
blobToDataURL ( file ) . then ( ( dataurl ) => {
document . location . href = dataurl
alert ( 'Add .epub to the filename of the downloaded file' )
} )
} else {
2018-03-27 07:32:02 +13:00
let filename = ffc . filename
if ( ffc . options . kepubify ) {
filename = filename . replace ( /\.epub$/ , '.kepub.epub' )
}
saveAs ( file , filename )
2018-03-13 10:08:16 +13:00
}
} )
}
2016-08-28 05:04:22 +12:00
function openStory ( id ) {
2018-03-16 05:42:11 +13:00
chrome . storage . sync . get ( [ 'ffcOptions' , 'version' ] , function ( result ) {
let options = result . ffcOptions
// Reset options on new version
if ( result . version !== FIMFIC2EPUB _VERSION ) {
options = { }
}
if ( ! ffc ) {
ffc = new FimFic2Epub ( id , options )
ffc . on ( 'progress' , onProgress )
} else if ( ffc . storyId !== id ) {
ffc . off ( 'progress' , onProgress )
closeDialog ( )
ffc = new FimFic2Epub ( id , options )
ffc . on ( 'progress' , onProgress )
}
openDialog ( )
} )
2016-08-23 02:28:30 +12:00
}
2016-08-28 05:04:22 +12:00
function onProgress ( percent , status ) {
ffcProgress ( percent )
if ( status ) {
ffcStatus ( status )
2016-08-23 02:28:30 +12:00
}
2017-06-08 02:17:40 +12:00
redraw ( )
2016-08-28 05:04:22 +12:00
}
2016-08-23 02:28:30 +12:00
2016-08-28 05:04:22 +12:00
if ( pageStoryId && isChromeExt ) {
2019-10-08 19:37:27 +13:00
chrome . runtime . sendMessage ( { showPageAction : true } )
2016-08-28 05:04:22 +12:00
chrome . runtime . onMessage . addListener ( function ( request ) {
if ( request === 'pageAction' ) {
openStory ( pageStoryId )
}
} )
2016-06-21 09:04:08 +12:00
}