Convert LF to CRLF

Automatically convert line feed (LF) to carriage return + line feed (CRLF) when copying text from web pages. 可以让复制的greasy fork代码支持在via直接新建,也可以让系统菜单复制的通义千问移动端网页的代码在便签APP正常换行。UserScript完全由通义千问生成。

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(У мене вже є менеджер скриптів, дайте мені встановити його!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Convert LF to CRLF
// @namespace    http://tampermonkey.net/
// @version      0.1.2
// @description  Automatically convert line feed (LF) to carriage return + line feed (CRLF) when copying text from web pages. 可以让复制的greasy fork代码支持在via直接新建,也可以让系统菜单复制的通义千问移动端网页的代码在便签APP正常换行。UserScript完全由通义千问生成。
// @author       幸福的赢得
// @match        *://greasyfork.org/*/scripts/*/code*
// @match        *://update.greasyfork.org/*
// @match        *://tongyi.aliyun.com/*
// @match        *://www.doubao.com/chat/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    document.addEventListener('copy', function(event) {
        var selection = window.getSelection().toString();
        if (!selection.includes('\n')) return;

        event.preventDefault();

        // Replace LF with CRLF
        var modifiedSelection = selection.replace(/\n/g, '\r\n');


//进一步优化通义千问移动版网页
if (/:\/\/tongyi/.test (location.href) ) {
    // Remove the leading whitespace + newline
    modifiedSelection = modifiedSelection.replace(/^\r\n/, '');

    // Find and remove the pattern "space newline space newline" and everything following it
    var patternMatch = modifiedSelection.match(/\r\n0\/1000\r\n(.*)/);
    if (patternMatch) {
        modifiedSelection = modifiedSelection.slice(0, -patternMatch[0].length -2);
    }
}


        // Put the modified text on the clipboard
        event.clipboardData.setData('text/plain', modifiedSelection);
    });
})();