4chan-x/src/Menu/DownloadLink.coffee
2014-08-03 18:34:02 -07:00

28 lines
760 B
CoffeeScript
Executable File

DownloadLink =
init: ->
return if g.VIEW is 'catalog' or !Conf['Menu'] or !Conf['Download Link']
a = $.el 'a',
className: 'download-link'
textContent: 'Download file'
# Specifying the filename with the download attribute only works for same-origin links.
$.on a, 'click', (e) ->
return true if @protocol is 'blob:'
e.preventDefault()
CrossOrigin.file @href, (blob) =>
if blob
@href = URL.createObjectURL blob
@click()
else
new Notice 'error', "Could not download #{file.URL}", 30
Menu.menu.addEntry
el: a
order: 100
open: ({file}) ->
return false unless file
a.href = file.URL
a.download = file.name
true