diff --git a/4chan_x.user.js b/4chan_x.user.js index c52c08547..4b348f5ad 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -469,6 +469,16 @@ }); $.add(d.head, script); return $.rm(script); + }, + bytesToString: function(size) { + var unit; + unit = 0; + while (size >= 1024) { + size /= 1024; + unit++; + } + size = unit > 1 ? Math.round(a * 100) / 100 : Math.round(size); + return "" + size + " " + ['B', 'KB', 'MB', 'GB'][unit]; } }); @@ -3058,7 +3068,7 @@ } }, parseArchivedPost: function(req, board, postID, root, cb) { - var bq, data, date, email, file, filesize, isOP, nameBlock, p, pc, pi, piM, span, time, unit; + var bq, data, date, email, file, filesize, isOP, nameBlock, p, pc, pi, piM, span, time; data = JSON.parse(req.response); if (data.error) { root.textContent = data.error; @@ -3182,16 +3192,10 @@ id: "f" + postID, className: 'file' }); - filesize = data.media_size; - unit = 0; - while (filesize >= 1024) { - filesize /= 1024; - unit++; - } - filesize = unit > 1 ? (a * 100).toFixed() / 100 : filesize.toFixed(); + filesize = $.bytesToString(data.media_size); $.add(file, $.el('div', { className: 'fileInfo', - innerHTML: "File: " + data.media_orig + "-(" + filesize + " " + ['B', 'KB', 'MB', 'GB'][unit] + ", " + data.media_w + "x" + data.media_h + ", )" + innerHTML: "File: " + data.media_orig + "-(" + filesize + ", " + data.media_w + "x" + data.media_h + ", )" })); span = $('span[title]', file); span.title = data.media_filename; @@ -3200,7 +3204,7 @@ className: 'fileThumb', href: data.media_link || data.remote_media_link, target: '_blank', - innerHTML: "" + (data.spoiler === " + innerHTML: "" + (data.spoiler === " })); $.after((isOP ? $('.postInfoM', p) : $('.postInfo', p)), file); } diff --git a/script.coffee b/script.coffee index b5278cf15..0dfe819ea 100644 --- a/script.coffee +++ b/script.coffee @@ -356,6 +356,21 @@ $.extend $, script = $.el 'script', textContent: code $.add d.head, script $.rm script + bytesToString: (size) -> + unit = 0 # Bytes + while size >= 1024 + size /= 1024 + unit++ + # Remove trailing 0s. + size = + if unit > 1 + # Keep the size as a float if the size is greater than 2^20 B. + # Round to hundredth. + Math.round(a * 100) / 100 + else + # Round to an integer otherwise. + Math.round size + "#{size} #{['B', 'KB', 'MB', 'GB'][unit]}" $.cache.requests = {} @@ -2477,21 +2492,10 @@ Get = file = $.el 'div', id: "f#{postID}" className: 'file' - filesize = data.media_size - unit = 0 # Bytes - while filesize >= 1024 - filesize /= 1024 - unit++ - # Keep the filesize as a float if the unit is in MBs. - # Remove trailing 0s. - filesize = - if unit > 1 - (a * 100).toFixed() / 100 - else - filesize.toFixed() + filesize = $.bytesToString data.media_size $.add file, $.el 'div', className: 'fileInfo' - innerHTML: "File: #{data.media_orig}-(#{filesize} #{['B', 'KB', 'MB', 'GB'][unit]}, #{data.media_w}x#{data.media_h}, )" + innerHTML: "File: #{data.media_orig}-(#{filesize}, #{data.media_w}x#{data.media_h}, )" span = $ 'span[title]', file span.title = data.media_filename span.textContent = @@ -2503,7 +2507,7 @@ Get = className: 'fileThumb' href: data.media_link or data.remote_media_link target: '_blank' - innerHTML: "#{if data.spoiler is " + innerHTML: "#{if data.spoiler is " $.after (if isOP then $('.postInfoM', p) else $('.postInfo', p)), file