NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript== // @name Google Drive copy image filenames // @namespace https://openuserjs.org/users/floodmeadows // @description Copies the list of image filenames from a Google Drive folder view to the clipboard // @copyright 2023, floodmeadows (https://openuserjs.org/users/floodmeadows) // @license MIT // @version 0.1.6 // @author andy@floodmeadows.com // @match https://drive.google.com/drive* // @icon https://www.google.com/s2/favicons?sz=64&domain=google.com // @updateURL https://openuserjs.org/meta/floodmeadows/Google_Drive_copy_image_filenames.meta.js // @downloadURL https://openuserjs.org/src/scripts/floodmeadows/Google_Drive_copy_image_filenames.user.js // @grant none // ==/UserScript== /* jshint esversion: 8 */ const inputListSelector = 'div.KL4NAf'; const correctPrefix = 'cym_'; (function() { 'use strict'; addButton(); })(); function addButton() { const newElement = document.createElement("button"); newElement.setAttribute("style", "position:absolute; top:22px; right:370px;"); newElement.addEventListener("click", copyListOfFilenames); const text = document.createTextNode("Copy image filenames"); newElement.appendChild(text); const target = document.querySelector("#gb").children[1].children[1].children[1]; target.appendChild(newElement); } function copyListOfFilenames() { console.log("copyListOfFilenames called."); console.log("inputListSelector = " + inputListSelector); var inputList = document.querySelectorAll(inputListSelector); console.log("inputList = " + inputList); console.log("inputList length = " + inputList.length); var arrFilenames = []; for(var i = 0; i < inputList.length; i++) { var filename = inputList[i].innerHTML; console.log(filename); if(!filenameStartsWithCorrectPrefix(filename)) { continue; } if(isImageFile(filename)) { filename = removePrefix(filename); arrFilenames.push(filename); } } var txtFilenames = arrFilenames.sort().join("\r\n"); console.log(txtFilenames); addTextToClipboard(txtFilenames); } function filenameStartsWithCorrectPrefix(filename) { console.log(filename.substring(0,correctPrefix.length)); if(filename.substring(0,correctPrefix.length) == correctPrefix) { console.log("filename " + filename + " has correct prefix."); return true; } else { console.log("filename " + filename + " does not have correct prefix."); return false; } } function isImageFile(filename) { if( (filename.substring(filename.length-4,filename.length) == '.png') || (filename.substring(filename.length-4,filename.length) == '.jpg') || (filename.substring(filename.length-5,filename.length) == '.jpeg')) { console.log("filename " + filename + " is an image."); return true; } else { console.log("filename " + filename + " is not an image."); return false; } } function removePrefix(filename) { console.log("Removing prefix... "); var filenameWithoutPrefix = filename.substring(correctPrefix.length,filename.length); console.log("Filename is now " + filenameWithoutPrefix); return filenameWithoutPrefix; } async function addTextToClipboard(text) { console.log("Adding text to clipboard..."); try { await navigator.clipboard.writeText(text); console.log('Content added to clipboard'); } catch (err) { console.error('Failed to add text to clipboard with error: ', err); } }