JavaScript:从网络复制时, trim 所有前导空格以及所有 CR

标签 javascript regex linux bash clipboard

在使用 SSH 客户端 SSH 连接到远程 Ubuntu 终端后,我无法跨平台使用 Windows 10 和 Linux (Ubuntu) Putty .

我基本上想做的是:

在我的个人 GitHub 帐户中,我存储和编辑了一些代码文件(而不是在我的 PC 上存储和编辑它们)。其中一些文件是 Bourne shell 文件(.sh 文件)。

我想从我个人 GitHub 帐户中的文件中复制一些 Bash 代码,然后将其粘贴到 Bash 终端以直接执行:

  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.

相关的 Bash 代码是以下代码(最初取自 here ),其中包含 Bash 脚本声明、2 个变量导出和带有 cat here-document 的 Bash 函数, 缩进前导制表符缩进,以及函数的调用:

#!/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

现状与问题

代码是用 Windows 字符复制的,例如 Windows 中常见的回车 (CR) 行定界符,或可能会中断执行的前导空格(取决于我复制的特定代码,在这种情况下here-document 的意思是前导制表符缩进变成单个点或非制表符空格等,这在使用 here-documents 时是被禁止的)。

期望的状态

我希望我从 GitHub 复制到 Windows 10 剪贴板的代码被复制时没有前导空格和 CR。

我尝试过的事情:

  1. 我找不到 postprocessing solution in the Linux side (这基本上是将所有 Bash 代码输送到实用程序中,过滤掉 Windows 字符(如 dos2unixsed "s/^[\t]//g")。

  2. 我确实找到了 AHk 的解决方案trimmed undesired characters in between copying and sending content to Windows clipboard 的 TrimClipboard() 函数.

一种可能的 JavaScript 方法

鉴于我无法从 Linux (Bash) 端对代码进行后处理,并且鉴于我不想依赖于 AHk,我希望在免费时使用纯 JavaScript vanilla 方式将 GitHub 内容复制到 Windows 剪贴板中所有前导空格和 Windows 字符(尤其是 CR)。

有没有办法在 vanilla JavaScript 中进行这种“过滤复制”?我想到了一个 Greasemoneky 脚本,它将包含一个脚本来执行此操作,旨在仅在 github.com 中工作。

注意事项

  1. 我根本不使用 Git,只使用 GitHub。
  2. 我使用制表符而不是常规空格缩进,因为这是文档所需要的,而且对我来说也更舒服。
  3. Windows 剪贴板中的最终结果应该是相同的代码,但没有任何前导空格/空格/制表符,也没有 CR。原始的、无 CR 版本的代码/纯代码。
  4. 我遇到的问题不是 GitHub 独有的,它可能会影响其他浏览器程序,因此我寻求一种通用的和激进的 JavaScript 方法。事实上,在这种情况下,我只会将 GitHub 与 Greasemonkey 匹配,但如果我也从那里复制文本,我总是可以匹配另一个网站,并遇到那个问题。

最佳答案

如果您能够运行 Greasemonkey 脚本,那么您可以尝试以下代码片段。这在 github 上的用户 benqzq 上执行,您可以更改它以扩展其工作域。事实上,我之前从未制作过 Greasemonkey 脚本,我不是 JS 专家,因此在正式运行之前可能需要进行一些测试。导入此脚本后,您必须选择所需的代码区域,然后修改后的版本将被复制到您的系统剪贴板:

// ==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();
}

关于JavaScript:从网络复制时, trim 所有前导空格以及所有 CR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48966685/

相关文章:

ruby - RVM在linux上有什么优势?

javascript - 多维数组在 Javascript 中返回 'undefined' 值

javascript - ASP :RadioButton and javascript function $find()

regex - 在 bash 中使用正则表达式查找文件

regex - Jmeter正则表达式提取器没有值(value)

linux - 用于在 Linux 上监视磁盘 i/o 速率的脚本

javascript - 数组对象之间的映射关系

javascript - 如何为 session 存储正确设置 redis

php - Laravel 规则和正则表达式 (OR) 运算符的问题

mysql - 如何更多地了解 MySQL、Apache 和 .htaccess?