User:Aspallar/common.js

// First draft of copy to clipboard utility // Adds a "Clipboard Copy" to the My Tools menu for Special:Images // Special:UnusedFiles and Special:KistFiles, which allows files // to be selected and their filenames copied to the clipboard. // Some of the code in this came from https://dev.fandom.com/wiki/MultipleFileDelete // @author Aspallar (function ($) {   /*global mw*/    'use strict';

var pageName = mw.config.get('wgCanonicalSpecialPageName');

if (!['Unusedimages', 'Images', 'Listfiles'].includes(pageName)) return;

console.log('filetoclipboard W');

var started = false;

function selectAll { /*jshint -W040 */ // allow old school jquery this var btn = $(this); if (btn.text === 'Deselect All') { $('.clipboard-check').each(function {               this.checked = false;            }); btn.text('Select All'); } else { $('.clipboard-check').each(function {               this.checked = true;            }); btn.text('Deselect All'); }   }

function addToClipboard { var selected = $('.clipboard-check:checked'); var clipText = []; if (pageName === 'Images') { selected.each(function {                var page = $(this).closest('.wikia-gallery-item')                    .find('.gallery-image-wrapper').attr('id');                clipText.push(page);            }); }       else {           selected.each(function {                var page =                     $(this).parent.find('a').first.attr('title') ||                    $(this).parent.find('a').first.text;                clipText.push(page.substring(5));            }); }       navigator.clipboard.writeText(clipText.join('\n')); //.then(function  {        //     alert('done');        // }); }

function addCkeckboxes { var checkbox = ''; if (pageName === 'Unusedimages') { $('.gallerytext > a').each(function {               $(this).before(checkbox);            }); } else if (pageName === 'Images') { $('.lightbox-caption').each(function {                $(this).prepend(checkbox);            }); }       else { $('.TablePager_col_img_name').each(function {                $(this).prepend(checkbox);            }); }   }

function addButtons { var buttonCommon = { 'class': 'button', css: { cursor: 'pointer', height: 'initial', 'margin-left': 3, }       };        var buttons = [ $(' ', $.extend({ id: 'btn-mfd-delete', text: 'Copy', click: addToClipboard, }, buttonCommon)), $(' ', $.extend({ text: 'Select All', click: selectAll, }, buttonCommon)) ];       $(' ')            .append(buttons) .css({               position: 'fixed',                'z-index': 9999,                bottom: 30,                left: 0            }) .appendTo('body'); }

function start { /*jshint -W040 */ // allow old school jquery this if (!started) {           addCkeckboxes; addButtons; started = true; }   }

function addMyToolsOption { var link = $('') .append($('').attr({href: '#'}).text('Clipboard Copy')) .click(start); $('#my-tools-menu').prepend(link); }

$(addMyToolsOption);

}(jQuery));