From d4b4cf5825ff120605f1450a5fb5cc69d6d9f56f Mon Sep 17 00:00:00 2001 From: fowr <89118232+perdedora@users.noreply.github.com> Date: Mon, 12 Aug 2024 15:35:16 -0300 Subject: [PATCH] script to expand original filename if truncated --- js/expand-filename.js | 53 +++++++++++++++++++++++++++++++++++++++++++ js/expand-too-long.js | 39 ------------------------------- 2 files changed, 53 insertions(+), 39 deletions(-) create mode 100644 js/expand-filename.js delete mode 100644 js/expand-too-long.js diff --git a/js/expand-filename.js b/js/expand-filename.js new file mode 100644 index 00000000..2f3ac0fd --- /dev/null +++ b/js/expand-filename.js @@ -0,0 +1,53 @@ +/* + * expand-filename.js + * https://github.com/vichan-devel/vichan/blob/master/js/expand-filename.js + * + * Released under the MIT license + * Copyright (c) 2024 Perdedora + * + * Usage: + * $config['additional_javascript'][] = 'js/expand-filename.js'; + * + */ + +function doFilename(element) { + const filenames = element.querySelectorAll('[data-truncate="true"]'); + filenames.forEach(filename => { + filename.addEventListener('mouseover', event => addHover(event.target)); + filename.addEventListener('mouseout', event => removeHover(event.target)); + }); +} + +function addHover(element) { + element.dataset.truncatedFilename = element.textContent; + element.textContent = element.download; +} + +function removeHover(element) { + element.textContent = element.dataset.truncatedFilename; + delete element.dataset.truncatedFilename; +} + +document.addEventListener('DOMContentLoaded', () => { + doFilename(document); + + // Create a MutationObserver to watch for new elements + const observer = new MutationObserver(mutationsList => { + mutationsList.forEach(mutation => { + if (mutation.type === 'childList') { + mutation.addedNodes.forEach(addedNode => { + if (addedNode.nodeType === Node.ELEMENT_NODE) { + // Apply `doFilename` to newly added elements + doFilename(addedNode); + } + }); + } + }); + }); + + // Start observing the document body for changes + observer.observe(document.body, { + childList: true, + subtree: true + }); +}); diff --git a/js/expand-too-long.js b/js/expand-too-long.js deleted file mode 100644 index bbadb5f1..00000000 --- a/js/expand-too-long.js +++ /dev/null @@ -1,39 +0,0 @@ -/* - * expand-too-long.js - * https://github.com/vichan-devel/vichan/blob/master/js/expand-too-long.js - * - * Released under the MIT license - * Copyright (c) 2013-2014 Marcin Ɓabanowski - * - * Usage: - * $config['additional_javascript'][] = 'js/jquery.min.js'; - * $config['additional_javascript'][] = 'js/expand-too-long.js'; - * - */ - -$(function() { - var do_expand = function() { - $(this).find('a').click(function(e) { - e.preventDefault(); - - var url = $(this).attr('href'); - var body = $(this).parents('.body'); - - $.ajax({ - url: url, - context: document.body, - success: function(data) { - var content = $(data).find('#'+url.split('#')[1]).parent().parent().find(".body").first().html(); - - body.html(content); - } - }); - }); - }; - - $('.toolong').each(do_expand); - - $(document).on("new_post", function(e, post) { - $(post).find('.toolong').each(do_expand) - }); -});