diff --git a/4chan_x.user.js b/4chan_x.user.js index f07f3adbb..845d83bca 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -487,6 +487,15 @@ $.add(d.head, script); return $.rm(script); }, + shortenFilename: function(filename, isOP) { + var threshold; + threshold = isOP ? 40 : 30; + if (filename.replace(/\.\w+$/, '').length > threshold) { + return "" + filename.slice(0, threshold - 5) + "(...)" + (filename.match(/\.\w+$/)); + } else { + return filename; + } + }, bytesToString: function(size) { var unit; unit = 0; @@ -3378,23 +3387,23 @@ return Main.callbacks.push(this.node); }, node: function(post) { - var alt, node, span; + var alt, filename, node, _ref; if (post.isInlined && !post.isCrosspost || !post.fileInfo) { return; } node = post.fileInfo.firstElementChild; alt = post.img.alt; - span = $('span', node); + filename = ((_ref = $('span', node)) != null ? _ref.title : void 0) || node.title; FileInfo.data = { link: post.img.parentNode.href, spoiler: /^Spoiler/.test(alt), size: alt.match(/\d+\.?\d*/)[0], unit: alt.match(/\w+$/)[0], - resolution: span.previousSibling.textContent.match(/\d+x\d+|PDF/)[0], - fullname: span.title, - shortname: span.textContent + resolution: node.textContent.match(/\d+x\d+|PDF/)[0], + fullname: filename, + shortname: $.shortenFilename(filename, post.isOP) }; - node.setAttribute('data-filename', span.title); + node.setAttribute('data-filename', filename); return node.innerHTML = FileInfo.funk(FileInfo); }, setFormats: function() { @@ -3537,7 +3546,7 @@ } }, parseArchivedPost: function(req, board, postID, root, cb) { - var bq, br, capcode, data, email, file, filename, filesize, isOP, name, nameBlock, pc, pi, piM, span, spoiler, subject, threadID, threshold, thumb_src, timestamp, trip, userID; + var bq, br, capcode, data, email, file, filename, filesize, isOP, name, nameBlock, pc, pi, piM, span, spoiler, subject, threadID, thumb_src, timestamp, trip, userID; data = JSON.parse(req.response); $.addClass(root, 'archivedPost'); if (data.error) { @@ -3710,8 +3719,7 @@ })); span = $('span[title]', file); span.title = filename; - threshold = isOP ? 40 : 30; - span.textContent = filename.replace(/\.\w+$/, '').length > threshold ? "" + filename.slice(0, threshold - 5) + "(...)" + (filename.match(/\.\w+$/)) : filename; + span.textContent = $.shortenFilename(filename, isOP); thumb_src = data.media_status === 'available' ? "src=" + data.thumb_link : ''; $.add(file, $.el('a', { className: spoiler ? 'fileThumb imgspoiler' : 'fileThumb', diff --git a/changelog b/changelog index 0e7291d76..e6998b29f 100644 --- a/changelog +++ b/changelog @@ -1,4 +1,6 @@ master +-Mayhem + Fix support of change in 4chan's HTML about hidden filename in case of spoiler. 2.34.5 - Mayhem diff --git a/script.coffee b/script.coffee index c44864ab6..f5b190c1d 100644 --- a/script.coffee +++ b/script.coffee @@ -368,6 +368,15 @@ $.extend $, script = $.el 'script', textContent: code $.add d.head, script $.rm script + shortenFilename: (filename, isOP) -> + # FILENAME SHORTENING SCIENCE: + # OPs have a +10 characters threshold. + # The file extension is not taken into account. + threshold = if isOP then 40 else 30 + if filename.replace(/\.\w+$/, '').length > threshold + "#{filename[...threshold - 5]}(...)#{filename.match(/\.\w+$/)}" + else + filename bytesToString: (size) -> unit = 0 # Bytes while size >= 1024 @@ -2675,17 +2684,17 @@ FileInfo = return if post.isInlined and not post.isCrosspost or not post.fileInfo node = post.fileInfo.firstElementChild alt = post.img.alt - span = $ 'span', node + filename = $('span', node)?.title or node.title FileInfo.data = link: post.img.parentNode.href spoiler: /^Spoiler/.test alt size: alt.match(/\d+\.?\d*/)[0] unit: alt.match(/\w+$/)[0] - resolution: span.previousSibling.textContent.match(/\d+x\d+|PDF/)[0] - fullname: span.title - shortname: span.textContent + resolution: node.textContent.match(/\d+x\d+|PDF/)[0] + fullname: filename + shortname: $.shortenFilename filename, post.isOP # XXX GM/Scriptish - node.setAttribute 'data-filename', span.title + node.setAttribute 'data-filename', filename node.innerHTML = FileInfo.funk FileInfo setFormats: -> code = Conf['fileInfo'].replace /%([BKlLMnNprs])/g, (s, c) -> @@ -2954,15 +2963,7 @@ Get = innerHTML: "File: #{data.media_orig}-(#{if spoiler then 'Spoiler Image, ' else ''}#{filesize}, #{data.media_w}x#{data.media_h}, )" span = $ 'span[title]', file span.title = filename - threshold = if isOP then 40 else 30 - span.textContent = - # FILENAME SHORTENING SCIENCE: - # OPs have a +10 characters threshold. - # The file extension is not taken into account. - if filename.replace(/\.\w+$/, '').length > threshold - "#{filename[...threshold - 5]}(...)#{filename.match(/\.\w+$/)}" - else - filename + span.textContent = $.shortenFilename filename, isOP thumb_src = if data.media_status is 'available' then "src=#{data.thumb_link}" else '' $.add file, $.el 'a', className: if spoiler then 'fileThumb imgspoiler' else 'fileThumb'