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.

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

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!)

  1. // ==UserScript==
  2. // @name Convert LF to CRLF
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1.2
  5. // @description Automatically convert line feed (LF) to carriage return + line feed (CRLF) when copying text from web pages. 可以让复制的greasy fork代码支持在via直接新建,也可以让系统菜单复制的通义千问移动端网页的代码在便签APP正常换行。UserScript完全由通义千问生成。
  6. // @author 幸福的赢得
  7. // @match *://greatest.deepsurf.us/*/scripts/*/code*
  8. // @match *://update.greatest.deepsurf.us/*
  9. // @match *://tongyi.aliyun.com/*
  10. // @match *://www.doubao.com/chat/*
  11. // @grant none
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. document.addEventListener('copy', function(event) {
  18. var selection = window.getSelection().toString();
  19. if (!selection.includes('\n')) return;
  20.  
  21. event.preventDefault();
  22.  
  23. // Replace LF with CRLF
  24. var modifiedSelection = selection.replace(/\n/g, '\r\n');
  25.  
  26.  
  27. //进一步优化通义千问移动版网页
  28. if (/:\/\/tongyi/.test (location.href) ) {
  29. // Remove the leading whitespace + newline
  30. modifiedSelection = modifiedSelection.replace(/^\r\n/, '');
  31.  
  32. // Find and remove the pattern "space newline space newline" and everything following it
  33. var patternMatch = modifiedSelection.match(/\r\n0\/1000\r\n(.*)/);
  34. if (patternMatch) {
  35. modifiedSelection = modifiedSelection.slice(0, -patternMatch[0].length -2);
  36. }
  37. }
  38.  
  39.  
  40. // Put the modified text on the clipboard
  41. event.clipboardData.setData('text/plain', modifiedSelection);
  42. });
  43. })();