Is it possible to hide the "view" link for attachments depending on their file extension?

bonsai
Mega Sage

I have changed the download link for attachments to "View".

However, files with the "zip" extension cannot be viewed and are instead downloaded.
I understand this specification because it is stated in the product documentation.
However, it is not so good that they are downloaded even though they are "View".
Is it possible to hide the "View" link for "zip" files?
Or I would like to change it back to "Download"

If you can disable the link, that's fine too.

 

zip_.JPG

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@bonsai 

you will have to manipulate that using DOM manipulation and hide the view word

Note: DOM manipulation is not recommended

This onLoad script worked for me. Ensure "Isolate Script" = False for your client script

If this field is not on form then from list make it False

You can enhance it for other file extensions as well

AnkurBawiskar_0-1750323892127.png

 

function onLoad() {
    //Type appropriate comment here, and begin script below

    setTimeout(function() {

        // Get all elements with the class 'attachment_list_items'
        document.querySelectorAll('.attachment_list_items').forEach(function(item) {
            alert('inside');
            // Find all anchor tags within this item
            var anchors = item.getElementsByTagName('a');
            // Check if any anchor's aria-label or innerText contains 'zip'
            var hasZip = Array.from(anchors).some(function(a) {
                // Check for filename in aria-label or innerText
                var text = (a.getAttribute('aria-label') || '') + (a.innerText || '');
                return text.toLowerCase().includes('zip');
            });
            if (hasZip) {
                // Hide 'view' anchor tags within this item
                Array.from(anchors).forEach(function(a) {
                    if (a.innerHTML && a.innerHTML.toLowerCase().includes('view')) {
                        a.style.display = 'none';
                    }
                });
            }
        });
    }, 2000);
}

Output: 2 files were zip so View was removed from them

attachment extension hide view for zip file.gif

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

3 REPLIES 3

anshul_goyal
Kilo Sage

Thank you.

I learned from this article which file extensions are supported and that clicking the "View" link on a zip file will download it.

What I want to know now is whether it is possible to hide the "View" link depending on the file extension.

Ankur Bawiskar
Tera Patron
Tera Patron

@bonsai 

you will have to manipulate that using DOM manipulation and hide the view word

Note: DOM manipulation is not recommended

This onLoad script worked for me. Ensure "Isolate Script" = False for your client script

If this field is not on form then from list make it False

You can enhance it for other file extensions as well

AnkurBawiskar_0-1750323892127.png

 

function onLoad() {
    //Type appropriate comment here, and begin script below

    setTimeout(function() {

        // Get all elements with the class 'attachment_list_items'
        document.querySelectorAll('.attachment_list_items').forEach(function(item) {
            alert('inside');
            // Find all anchor tags within this item
            var anchors = item.getElementsByTagName('a');
            // Check if any anchor's aria-label or innerText contains 'zip'
            var hasZip = Array.from(anchors).some(function(a) {
                // Check for filename in aria-label or innerText
                var text = (a.getAttribute('aria-label') || '') + (a.innerText || '');
                return text.toLowerCase().includes('zip');
            });
            if (hasZip) {
                // Hide 'view' anchor tags within this item
                Array.from(anchors).forEach(function(a) {
                    if (a.innerHTML && a.innerHTML.toLowerCase().includes('view')) {
                        a.style.display = 'none';
                    }
                });
            }
        });
    }, 2000);
}

Output: 2 files were zip so View was removed from them

attachment extension hide view for zip file.gif

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader