From 08e104f0fa47ca9faf9df83adc0e61b44102c92b Mon Sep 17 00:00:00 2001 From: Nicolas Stepien Date: Thu, 28 Jun 2012 21:01:56 +0200 Subject: [PATCH] Add a download link to the menu for compatible browsers. Close #102. --- 4chan_x.user.js | 37 ++++++++++++++++++++++++++++++++++--- script.coffee | 31 +++++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/4chan_x.user.js b/4chan_x.user.js index 17d614004..1b67152e6 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -77,7 +77,7 @@ */ (function() { - var $, $$, Anonymize, ArchiveLink, AutoGif, Conf, Config, DeleteLink, ExpandComment, ExpandThread, Favicon, FileInfo, Filter, Get, ImageExpand, ImageHover, Keybinds, Main, Menu, Nav, Options, QR, QuoteBacklink, QuoteCT, QuoteInline, QuoteOP, QuotePreview, Quotify, Redirect, ReplyHiding, ReportLink, RevealSpoilers, Sauce, StrikethroughQuotes, ThreadHiding, ThreadStats, Time, TitlePost, UI, Unread, Updater, Watcher, d, g, _base; + var $, $$, Anonymize, ArchiveLink, AutoGif, Conf, Config, DeleteLink, DownloadLink, ExpandComment, ExpandThread, Favicon, FileInfo, Filter, Get, ImageExpand, ImageHover, Keybinds, Main, Menu, Nav, Options, QR, QuoteBacklink, QuoteCT, QuoteInline, QuoteOP, QuotePreview, Quotify, Redirect, ReplyHiding, ReportLink, RevealSpoilers, Sauce, StrikethroughQuotes, ThreadHiding, ThreadStats, Time, TitlePost, UI, Unread, Updater, Watcher, d, g, _base; Config = { main: { @@ -112,7 +112,8 @@ 'Menu': [true, 'Add a drop-down menu in posts.'], 'Report Link': [true, 'Add a report link to the menu.'], 'Delete Link': [true, 'Add a delete link to the menu.'], - 'Archive Link': [true, 'Add a archive link to the menu.'] + 'Download Link': [true, 'Add a download with original filename link to the menu. Chrome-only currently.'], + 'Archive Link': [true, 'Add an archive link to the menu.'] }, Monitoring: { 'Thread Updater': [true, 'Update threads. Has more options in its own dialog.'], @@ -3203,6 +3204,7 @@ fullname: span.title, shortname: span.textContent }; + node.setAttribute('data-filename', span.title); return node.innerHTML = FileInfo.funk(FileInfo); }, setFormats: function() { @@ -3996,6 +3998,31 @@ } }; + DownloadLink = { + init: function() { + var a; + if ($.el('a').download === void 0) { + return; + } + a = $.el('a', { + className: 'download_link', + textContent: 'Download file' + }); + return Menu.addEntry({ + el: a, + open: function(post) { + var fileText; + a.href = post.img.parentNode.href; + fileText = post.fileInfo.firstElementChild; + return a.download = Conf['File Info Formatting'] ? fileText.dataset.filename : $('span', fileText).title; + }, + requirement: function(post) { + return post.img; + } + }); + } + }; + ArchiveLink = { init: function() { var a; @@ -4675,6 +4702,9 @@ if (Conf['Delete Link']) { DeleteLink.init(); } + if (Conf['Download Link']) { + DownloadLink.init(); + } if (Conf['Archive Link']) { ArchiveLink.init(); } @@ -4949,7 +4979,8 @@ a[href="javascript:;"] {\ border-bottom: 1px solid rgba(0, 0, 0, .25);\ display: block;\ outline: none;\ - padding: 3px 4px;\ + padding: 3px 7px;\ + text-decoration: none;\ }\ .entry:last-child {\ border: none;\ diff --git a/script.coffee b/script.coffee index f3b4084e2..8ed4fff4a 100644 --- a/script.coffee +++ b/script.coffee @@ -28,7 +28,8 @@ Config = 'Menu': [true, 'Add a drop-down menu in posts.'] 'Report Link': [true, 'Add a report link to the menu.'] 'Delete Link': [true, 'Add a delete link to the menu.'] - 'Archive Link': [true, 'Add a archive link to the menu.'] + 'Download Link': [true, 'Add a download with original filename link to the menu. Chrome-only currently.'] + 'Archive Link': [true, 'Add an archive link to the menu.'] Monitoring: 'Thread Updater': [true, 'Update threads. Has more options in its own dialog.'] 'Unread Count': [true, 'Show unread post count in tab title'] @@ -2497,6 +2498,8 @@ FileInfo = resolution: span.previousSibling.textContent.match(/\d+x\d+|PDF/)[0] fullname: span.title shortname: span.textContent + # XXX GM/Scriptish + node.setAttribute 'data-filename', span.title node.innerHTML = FileInfo.funk FileInfo setFormats: -> code = Conf['fileInfo'].replace /%([BKlLMnNprs])/g, (s, c) -> @@ -3137,6 +3140,26 @@ ReportLink = set = "toolbar=0,scrollbars=0,location=0,status=1,menubar=0,resizable=1,width=685,height=200" window.open url, id, set +DownloadLink = + init: -> + # Test for download feature support. + return if $.el('a').download is undefined + a = $.el 'a', + className: 'download_link' + textContent: 'Download file' + Menu.addEntry + el: a + open: (post) -> + a.href = post.img.parentNode.href + fileText = post.fileInfo.firstElementChild + a.download = + if Conf['File Info Formatting'] + fileText.dataset.filename + else + $('span', fileText).title + requirement: (post) -> + post.img + ArchiveLink = init: -> a = $.el 'a', @@ -3633,6 +3656,9 @@ Main = if Conf['Delete Link'] DeleteLink.init() + if Conf['Download Link'] + DownloadLink.init() + if Conf['Archive Link'] ArchiveLink.init() @@ -3848,7 +3874,8 @@ a[href="javascript:;"] { border-bottom: 1px solid rgba(0, 0, 0, .25); display: block; outline: none; - padding: 3px 4px; + padding: 3px 7px; + text-decoration: none; } .entry:last-child { border: none;