// ==UserScript== // @name 高效文本文件查看器 // @namespace http://tampermonkey.net/ // @version 2.0 // @description 优化文本文件查看体验,支持快速复制、下载及长文本处理 // @author niweizhuan // @match *://*/* // @grant GM_addStyle // @grant GM_setClipboard // @license MIT // @downloadURL https://update.greasyfork.icu/scripts/524721/%E9%AB%98%E6%95%88%E6%96%87%E6%9C%AC%E6%96%87%E4%BB%B6%E6%9F%A5%E7%9C%8B%E5%99%A8.user.js // @updateURL https://update.greasyfork.icu/scripts/524721/%E9%AB%98%E6%95%88%E6%96%87%E6%9C%AC%E6%96%87%E4%BB%B6%E6%9F%A5%E7%9C%8B%E5%99%A8.meta.js // ==/UserScript== (function() { 'use strict'; // 获取当前页面的URL const currentPageUrl = window.location.href; // 检查是否为txt文件 function isTxtFile() { // 获取当前页面的Content-Type var contentType = document.contentType; // 检查Content-Type是否为纯文本 return contentType === 'text/plain'; } // 异步加载文本内容(流式读取) function loadTextContent(url) { return fetch(url).then(function(response) { if (!response.ok) { throw new Error('请检查互联网连接'); } const reader = response.body.getReader(); const decoder = new TextDecoder('utf-8'); let content = []; return new Promise(function(resolve, reject) { function read() { reader.read().then(function({ done, value }) { if (done) { resolve(content.join('')); return; } content.push(decoder.decode(value, { stream: true })); read(); }).catch(reject); } read(); }); }).catch(function(error) { alert('加载失败:', error); return null; }); } // 分段显示内容 function displayContent(content) { const contentElement = document.getElementById('content'); const loadingElement = document.getElementById('loading'); const errorElement = document.getElementById('error'); loadingElement.style.display = 'block'; errorElement.style.display = 'none'; if (!content) { errorElement.style.display = 'block'; loadingElement.style.display = 'none'; return; } const chunkSize = 10000; let offset = 0; window.textContentInterval = setInterval(function() { const chunk = content.slice(offset, offset + chunkSize); const chunkElement = document.createElement('div'); chunkElement.textContent = chunk; contentElement.appendChild(chunkElement); offset += chunkSize; if (offset >= content.length) { clearInterval(window.textContentInterval); loadingElement.style.display = 'none'; } }, 100); } // 初始化页面 function initializePage() { const newPageContent = `