4

I'm having trouble working cross-platform with Windows 10 and Linux (Ubuntu), after SSHing into a remote Ubuntu terminal with the SSH client Putty.

What I'm basically trying to do:

In my personal GitHub account, I store and edit some code files (instead storing and editing them on my PC). Some of these files are Bourne shell files (.sh files).

I desire to copy some Bash code from inside a file I have there in my personal GitHub account, then paste it into a Bash terminal, for direct execution:

  1. Copy code from one of the files in GitHub (non-raw version).

  2. Paste that code in terminal.

  3. Execute that code by hitting Enter in terminal.

The relevant Bash code is the following code (originally taken from here) which contains a Bash script declaration, 2 variable exports, and a Bash function with a cat here-document, indented with leading tab indentations, along with the function's call:

#!/bin/bash
export user="benqzq"
export repo="ulcwe"

preparations() {
    cat <<-EOF >> "$HOME"/.bashrc
        export s_a="/etc/nginx/sites-available"
        export s_e="/etc/nginx/sites-enabled"
        export drt="/var/www/html"
        source "$HOME"/"$repo"/software_internal.sh
    EOF
    source "$HOME"/.bashrc 2>/dev/null
}
preparations

Current state and problem

The code is copied with Windows characters like the Carriage Return (CR) line delimiter common in Windows, or leading whitespaces that might break execution (depends on the particular code I copy, and in the case of an here-document it sure is in the sense that leading tab indentations turn into a single dot or non-tab spaces, etc, and that's forbidden when working with here-documents).

Desired state

I desire that the code I copy from GitHub into Windows 10 clipboard will be copied without leading spaces and CRs.

Things I've tried:

  1. I failed to find a postprocessing solution in the Linux side (which is basically piping all Bash code in utilities that will filter out Windows characters (like dos2unix or sed "s/^[ \t]//g").

  2. I did found a solution with the AHk TrimClipboard() function that trimmed undesired characters in between copying and sending content to Windows clipboard.

A possible JavaScript approach

Given I failed postprocessing the code from Linux (Bash) side, and given that I don't want to be dependent in AHk, I desire a pure JavaScript vanilla way to copy the GitHub content into Windows clipboard, when it is free of all leading whitespaces and Windows characters (CR in particular).

Is there a way to do such "filtered copying" in vanilla JavaScript? I thought of a Greasemoneky script that will contain a script to do so, aimed to work only inside github.com.

Notes

  1. I don't use Git at all, only GitHub.
  2. I indent with tabs and not regular spaces because that's what needed for heredocuments, and it's also more comfortable for me.
  3. The end result in Windows clipboard should be the same code, but without any leading spaces / whitespaces / tabs and also without CRs. A raw, CR-less version of the code / pure code.
  4. The problem I encounter isn't unique to GitHub, it could effect other browser-programs, hence I seek a general and radical approach with JavaScript. Indeed, in this case I'll match only GitHub with Greasemonkey, but I could always match another website if I copy text from there as well, and encounter that problem.
Osi
  • 1
  • 3
  • 9
  • 30

1 Answers1

1

If you are able to run a Greasemonkey script then you can try below code snippet. This executes on user benqzq on github which you can change to expand its working domain. I never have made a Greasemonkey script before actually, I'm not a JS guru, so it may need some tests before any serious run. After importing this script you have to select areas of code you need then modified version will be copied to your system clipboard:

// ==UserScript==
// @name     CRLFRemover
// @version  1
// @match https://github.com/benqzq/*
// ==/UserScript==

// @https://stackoverflow.com/a/5379408/1020526
function getSelectionText() {
    var text = "";
    if (window.getSelection) {
        text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
        text = document.selection.createRange().text;
    }
    return text;
}

document.onmouseup = function() {
    var text = getSelectionText();
    // It should be of type `textarea` otherwise format will mess up
    var input = document.createElement('textarea');
    document.body.appendChild(input)
    // Here we remove CRs
    input.value = text.replace(/\r/g, '');
    input.select();
    document.execCommand('Copy', false);
    input.remove();
}
revo
  • 47,783
  • 14
  • 74
  • 117