// ==UserScript== // @name 随机写入100个不同字符到剪切板 // @namespace https://viayoo.com/6a4j69 // @version 1.0 // @description 纯粹自用脚本,MIUI剪切板写入字符超过2w+后,底栏的无法显示剪切板内容,只有不断写入,超过限定剪切板数量,才能显示新的剪切板。 // @author Via // @match *://*/* // @grant GM_setClipboard // @grant GM_registerMenuCommand // @grant GM_getValue // @grant GM_setValue // @run-at document-idle // ==/UserScript== (() => { 'use strict'; const POOL = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789~!@#$%^&*_-+=<>?"; const TIMES = GM_getValue('copyTimes', 100); const sec = (TIMES * 60 / 1000).toFixed(1); let i = 0, bar = null; let generatedStrings = []; let currentIndex = 0; let collisionCount = 0; let performanceLog = []; const generateAllUniqueStrings = (count) => { console.log(`开始预生成 ${count} 个唯一字符串...`); const startTime = performance.now(); const strings = new Set(); const baseChars = POOL.split(''); while (strings.size < count) { const rnd = Math.random(); let arr = [...baseChars]; for (let x = arr.length - 1; x > 0; x--) { const j = Math.floor(Math.random() * (x + 1)); [arr[x], arr[j]] = [arr[j], arr[x]]; } let str; if (rnd < 0.7) { const len = 10 + Math.floor(Math.random() * 141); str = arr.slice(0, len).join(''); } else if (rnd < 0.9) { const p1 = arr.slice(0, 3 + Math.random() * 10 | 0).join(''); const p2 = arr.slice(15, 18 + Math.random() * 12 | 0).join(''); const p3 = arr.slice(30, 32 + Math.random() * 8 | 0).join(''); str = p1 + p2 + p3; } else { let result = ''; const segs = 3 + Math.floor(Math.random() * 5); let start = 0; for (let s = 0; s < segs; s++) { const len = 1 + Math.floor(Math.random() * 20); result += arr.slice(start, start + len).join(''); start += len + 5; if (start >= arr.length) start = 0; } const chaos = Math.random() < 0.4 ? ' \n ' : Math.random() < 0.3 ? '\t' : ' '; str = result.split('').join(Math.random() < 0.3 ? chaos : ''); } if (strings.has(str)) { collisionCount++; const uniqueSuffix = '_' + Date.now() + '_' + performance.now().toString(36).replace('.', '') + Math.random().toString(36).substr(2, 4); str = str.substring(0, Math.max(10, str.length - 10)) + uniqueSuffix; } strings.add(str); if (strings.size % 100 === 0 || strings.size === count) { console.log(`已生成 ${strings.size}/${count} 个字符串,碰撞 ${collisionCount} 次`); } } const endTime = performance.now(); console.log(`预生成完成!耗时 ${(endTime - startTime).toFixed(2)}ms`); console.log(`总碰撞次数: ${collisionCount}`); console.log(`唯一性验证: ${strings.size === count ? '✓ 100% 不重复' : '✗ 存在重复'}`); return Array.from(strings); }; const copy = t => { const copyStart = performance.now(); let method = ''; if (typeof GM_setClipboard === 'function') { GM_setClipboard(t); method = 'GM_setClipboard'; } else if (navigator.clipboard?.writeText) { navigator.clipboard.writeText(t); method = 'navigator.clipboard'; } else { const e = document.createElement('textarea'); e.value = t; e.style.cssText = 'position:fixed;opacity:0'; document.body.appendChild(e); e.select(); document.execCommand('copy'); e.remove(); method = 'execCommand'; } const copyTime = performance.now() - copyStart; performanceLog.push({index: i, time: copyTime, method}); if (i % 10 === 0) { console.log(`第 ${i} 次复制,方法: ${method},耗时: ${copyTime.toFixed(2)}ms`); } }; const createBar = () => { bar = document.createElement('div'); bar.innerHTML = `
0 / ${TIMES}
`; bar.style.cssText = 'position:fixed;top:50%;left:50%;transform:translate(-50%,-50%) scale(.92);min-width:300px;max-width:88vw;padding:24px 32px;background:rgba(255,255,255,.72);backdrop-filter:blur(20px) saturate(180%);-webkit-backdrop-filter:blur(20px) saturate(180%);border-radius:26px;border:1px solid rgba(255,255,255,.4);box-shadow:0 12px 32px rgba(0,0,0,.18),0 4px 12px rgba(0,0,0,.12);z-index:2147483647;pointer-events:none;text-align:center;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif;opacity:0;transition:all .65s cubic-bezier(.22,1,.36,1)'; document.body.appendChild(bar); requestAnimationFrame(() => { bar.style.opacity = '1'; bar.style.transform = 'translate(-50%,-50%) scale(1)'; }); }; const progress = () => { if (!bar) createBar(); bar.firstElementChild.textContent = i + ' / ' + TIMES; bar.querySelector('#p').style.width = (i / TIMES * 100) + '%'; if (i >= TIMES) { const p = bar.querySelector('#p'); p.style.background = 'linear-gradient(90deg,#34C759,#32D74B)'; p.style.boxShadow = '0 0 24px rgba(52,199,89,.8)'; setTimeout(() => { bar.style.opacity = '0'; bar.style.transform = 'translate(-50%,-50%) scale(.92)'; setTimeout(() => bar?.remove(), 700); const totalTime = performanceLog.reduce((sum, log) => sum + log.time, 0); console.log(`====== 执行完成统计 ======`); console.log(`总计: ${TIMES} 次复制`); console.log(`总耗时: ${totalTime.toFixed(2)}ms`); console.log(`平均每次: ${(totalTime / TIMES).toFixed(2)}ms`); console.log(`预生成碰撞: ${collisionCount} 次`); console.log(`实际执行碰撞: 0 次 (100% 不重复)`); const methodStats = {}; performanceLog.forEach(log => { methodStats[log.method] = (methodStats[log.method] || 0) + 1; }); console.log('复制方法统计:', methodStats); }, 900); } }; const run = () => { if (currentIndex < generatedStrings.length) { copy(generatedStrings[currentIndex]); currentIndex++; i = currentIndex; progress(); } }; const start = mode => { if (i > 0) return alert('请刷新页面后再操作'); console.log(`====== 开始执行 ======`); console.log(`模式: ${mode === 'lightning' ? '光速复制' : '快速复制'}`); console.log(`目标次数: ${TIMES}`); i = 0; currentIndex = 0; collisionCount = 0; performanceLog = []; if (bar) bar.remove(); bar = null; generatedStrings = generateAllUniqueStrings(TIMES); progress(); if (mode === 'lightning') { console.log('使用异步分批处理避免阻塞...'); const batchSize = 20; const processBatch = async () => { for (let batch = 0; batch < TIMES; batch += batchSize) { const end = Math.min(batch + batchSize, TIMES); for (let j = batch; j < end; j++) { if (currentIndex >= TIMES) return; run(); } await new Promise(resolve => setTimeout(resolve, 30)); } }; processBatch(); } else { console.log(`使用间隔复制,间隔: 80ms`); const id = setInterval(() => { if (currentIndex >= TIMES) { clearInterval(id); return; } run(); }, 80); } }; GM_registerMenuCommand('光速复制 ' + TIMES + ' 次(瞬间完成)', () => start('lightning')); GM_registerMenuCommand('快速复制 ' + TIMES + ' 次(约' + sec + '秒)', () => start()); GM_registerMenuCommand('设置复制次数(当前 ' + TIMES + ' 次)', () => { const n = prompt('请输入要连续复制的次数(1~50000)', TIMES); if (n && /^\d+$/.test(n) && +n >= 1 && +n <= 50000) { GM_setValue('copyTimes', +n); location.reload(); } }); console.log('脚本已加载,准备就绪'); console.log(`当前设置: ${TIMES} 次复制`); console.log(`字符池大小: ${POOL.length} 个字符`); })();