dmwyatt / gh_repo_download

download and view the contents of a GitHub repository or a ZIP file as a single text file

Home Page:https://gh-repo-dl.cottonash.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add "copy file to clipboard" feature

dmwyatt opened this issue · comments

Currently, on the results page, the user can download the text file or copy its contents to the clipboard.

Another useful function would be to copy the text file as a file to the clipboard. When the user pastes this into Claude, ChatGPT, Gemini, and probably others it ends up as an attachment rather than inserted inline.

A completely untested idea of how to do this:

function dataUriToFile(dataUri, filename) {
    const byteString = atob(dataUri.split(',')[1]);
    const mimeString = dataUri.split(',')[0].split(':')[1].split(';')[0];
    const ab = new ArrayBuffer(byteString.length);
    const ia = new Uint8Array(ab);
    for (let i = 0; i < byteString.length; i++) {
        ia[i] = byteString.charCodeAt(i);
    }
    const blob = new Blob([ab], {type: mimeString});
    return new File([blob], filename, {type: mimeString});
}

function copyFileToClipboard(dataUri, filename) {
    const file = dataUriToFile(dataUri, filename);
    navigator.clipboard.write([new ClipboardItem({'application/octet-stream': file})])
        .then(() => console.log("File copied to clipboard!"))
        .catch(err => console.error("Error copying file to clipboard:", err));
}

// Example usage with a data URI and filename
const dataUri = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA...';
const filename = 'example.png';  // Provide a meaningful filename here
copyFileToClipboard(dataUri, filename);