JGY
「创建博客」 「编辑」 「本文源码」

新文章


// ==UserScript==
// @name         本地TXT网页查看器
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  选择本地TXT,弹窗网页形式展示,支持字体脚本修改
// @author       You
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // 唤起文件选择按钮
    const openBtn = document.createElement('button');
    openBtn.innerText = '打开本地TXT';
    openBtn.style = `
        position: fixed;
        top: 20px;
        right: 20px;
        z-index: 9999999;
        padding: 8px 16px;
        background: #2b88d8;
        color: #fff;
        border: none;
        border-radius: 4px;
        cursor: pointer;
        font-size: 14px;
    `;
    document.body.appendChild(openBtn);

    // 隐藏文件选择器
    const fileInput = document.createElement('input');
    fileInput.type = 'file';
    fileInput.accept = '.txt';
    fileInput.style.display = 'none';
    document.body.appendChild(fileInput);

    // 外层遮罩容器
    let mask = null;
    // 文本展示主体
    let txtBox = null;

    // 关闭弹窗
    function closeView() {
        if(mask) mask.remove();
        mask = null;
        txtBox = null;
    }

    // 创建网页式弹窗
    function createViewer(fileName, content) {
        // 遮罩层
        mask = document.createElement('div');
        mask.style = `
            position: fixed;
            top: 0;
            left: 0;
            width: 100vw;
            height: 100vh;
            background: rgba(0,0,0,0.7);
            z-index: 99999999;
            display: flex;
            justify-content: center;
            align-items: center;
        `;
        mask.onclick = (e) => {
            if(e.target === mask) closeView();
        };

        // 内容容器(模拟网页页面)
        const wrap = document.createElement('div');
        wrap.style = `
            width: 85vw;
            height: 85vh;
            background: #fff;
            border-radius: 8px;
            display: flex;
            flex-direction: column;
            overflow: hidden;
        `;

        // 标题栏
        const titleBar = document.createElement('div');
        titleBar.style = `
            padding: 12px 16px;
            background: #f5f5f5;
            border-bottom: 1px solid #eee;
            display: flex;
            justify-content: space-between;
            align-items: center;
        `;
        titleBar.innerHTML = `<span>${fileName}</span>`;

        // 关闭按钮
        const closeBtn = document.createElement('button');
        closeBtn.innerText = '关闭';
        closeBtn.style = `
            padding: 4px 12px;
            border: 1px solid #ccc;
            background: #fff;
            border-radius: 4px;
            cursor: pointer;
        `;
        closeBtn.onclick = closeView;
        titleBar.appendChild(closeBtn);

        // 文本显示区域(核心,字体脚本可正常作用于此)
        txtBox = document.createElement('div');
        txtBox.className = 'txt-content';
        txtBox.style = `
            flex: 1;
            padding: 20px;
            overflow: auto;
            white-space: pre-wrap;
            word-break: break-all;
            line-height: 1.6;
            font-size: 16px;
        `;
        txtBox.textContent = content;

        // 组装结构
        wrap.appendChild(titleBar);
        wrap.appendChild(txtBox);
        mask.appendChild(wrap);
        document.body.appendChild(mask);
    }

    // 绑定点击事件
    openBtn.onclick = () => fileInput.click();

    // 读取文件并渲染
    fileInput.onchange = function(e) {
        const file = e.target.files[0];
        if(!file) return;

        const reader = new FileReader();
        reader.onload = function(ev) {
            createViewer(file.name, ev.target.result);
        };
        // 读取文本,兼容大部分编码
        reader.readAsText(file);
        fileInput.value = '';
    };
})();

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

¥ 打赏博主

上一篇 替换脚本

下一篇 软键盘

发布评论

Content
smoothScroll.init({ speed: 500, // Integer. How fast to complete the scroll in milliseconds easing: 'easeInOutCubic', // Easing pattern to use offset: 20, // Integer. How far to offset the scrolling anchor location in pixels });