前端实现页面文本复制/代码复制/mardown复制的几种方式

1.复制文本【安全环境下-localhost或者https】

 <button class="btn" onclick="copy1()">复制文本</button>
 function copy1() {
            navigator.clipboard.writeText('普通')
 }

2.复制文本【所有环境都可用】

 function  copy(val){
            const textarea = document.createElement('textarea');
            textarea.value = val;
            document.body.appendChild(textarea);
            textarea.select();
            try {
                const successful = document.execCommand('copy');
 				console.log("复制成功")
            } catch (err) {
                console.error('复制失败:', err);
            } finally {
                document.body.removeChild(textarea);
            }
        },

以下为复制markdown代码或者图片功能,我自己写的ChatGPT网站也用了以下功能,
演示地址为:演示地址

3.复制markdown语法

        copyWord(val) {
            const el = document.createElement('div'); 
            el.innerHTML = marked(val);// 'marked'是一个Markdown到HTML的转换库
            el.contentEditable = 'true'; 
            //q:为什么要设置contentEditable为true?
            //a:因为如果不设置为true,那么在Firefox中,el.innerHTML = '<table><tr><td>1</td></tr></table>'会变成'<table><tbody><tr><td>1</td></tr></tbody></table>',这会导致Word无法识别
        
            // 清除或调整样式以避免在Word中出现边框
            this.removeBorderStyles(el);
        
            document.body.appendChild(el);
            const range = document.createRange();
            range.selectNodeContents(el);
            const selection = window.getSelection();
            selection.removeAllRanges();
            selection.addRange(range);
            document.execCommand('copy');
            selection.removeAllRanges();
            document.body.removeChild(el);
            this.$Message.success('复制成功!');
        },
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
前端加密有以下几种方式: 1. 对称加密:使用相同的密钥进行加密和解密,常见的算法有DES、3DES、AES等。实现方式如下: ```javascript // 加密 function encryptByAES(message, secretKey) { const key = CryptoJS.enc.Utf8.parse(secretKey); const encrypted = CryptoJS.AES.encrypt(message, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }); return encrypted.toString(); } // 解密 function decryptByAES(ciphertext, secretKey) { const key = CryptoJS.enc.Utf8.parse(secretKey); const decrypted = CryptoJS.AES.decrypt(ciphertext, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }); return decrypted.toString(CryptoJS.enc.Utf8); } ``` 2. 非对称加密:使用公钥和私钥进行加密和解密,常见的算法有RSA、DSA等。实现方式如下: ```javascript // 生成公钥和私钥 const keyPair = window.crypto.subtle.generateKey( { name: "RSA-OAEP", modulusLength: 2048, publicExponent: new Uint8Array([1, 0, 1]), // 65537 hash: "SHA-256" }, true, ["encrypt", "decrypt"] ); // 加密 async function encryptByRSA(message, publicKey) { const encodedMessage = new TextEncoder().encode(message); const encrypted = await window.crypto.subtle.encrypt( { name: "RSA-OAEP" }, publicKey, encodedMessage ); return window.btoa(String.fromCharCode(...new Uint8Array(encrypted))); } // 解密 async function decryptByRSA(ciphertext, privateKey) { const decodedCiphertext = Uint8Array.from( atob(ciphertext), c => c.charCodeAt(0) ); const decrypted = await window.crypto.subtle.decrypt( { name: "RSA-OAEP" }, privateKey, decodedCiphertext ); return new TextDecoder().decode(decrypted); } ``` 3. 散列加密:将数据转化为固定长度的散列值,常见的算法有MD5、SHA-1、SHA-256等。实现方式如下: ```javascript // 计算MD5散列值 function hashByMD5(message) { return CryptoJS.MD5(message).toString(); } // 计算SHA-256散列值 function hashBySHA256(message) { return CryptoJS.SHA256(message).toString(); } ``` 4. 混淆加密:通过混淆代码或者加入噪音的方式来增强安全性,常见的方式代码混淆、字符替换等。实现方式如下: ```javascript // 字符串替换 function replaceChars(str) { return str.replace(/a/g, "@").replace(/e/g, "3").replace(/i/g, "1"); } // 代码混淆 function obfuscateCode(code) { // 实现方式可以使用自己的加密算法,这里只是示例 return code.split("").reverse().join(""); } ``` 需要注意的是,以上示例代码只是参考实现,实际情况需要根据具体需求进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吉吉安

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值