Enhanced reading experience for local txt files
当前为
// ==UserScript==
// @name Local TXT Reader
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Enhanced reading experience for local txt files
// @author JiuYou2020
// @license MIT
// @match file:///*
// @grant none
// ==/UserScript==
(function () {
'use strict';
// Ensure the script runs only on .txt files
if (window.location.pathname.endsWith('.txt')) {
// Set up styles
const style = document.createElement('style');
style.innerHTML = `
body {
background-color: #e7e3d8;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
overflow: hidden;
}
.outer-container {
width: 100%;
height: 100%;
overflow-y: auto;
display: flex;
justify-content: center;
align-items: flex-start;
padding: 20px 0;
}
.content-container {
width: 55%;
background-color: #f4f1e9;
padding: 20px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
color: #333333;
font-size: 19px;
line-height: 1.5;
font-family: inherit;
}
.content-container p {
text-indent: 2em;
margin-top: 1em;
}
`;
document.head.appendChild(style);
// Get the text content
const text = document.body.textContent.trim();
// Split the text into paragraphs by newlines
const paragraphs = text.split(/\n+/).map(paragraph => paragraph.trim());
// Clear the body content
document.body.innerHTML = '';
// Create the outer container
const outerContainer = document.createElement('div');
outerContainer.className = 'outer-container';
document.body.appendChild(outerContainer);
// Create the content container
const contentContainer = document.createElement('div');
contentContainer.className = 'content-container';
outerContainer.appendChild(contentContainer);
// Add paragraphs to the content container
paragraphs.forEach(paragraph => {
if (paragraph) { // Only add non-empty paragraphs
const p = document.createElement('p');
p.textContent = paragraph;
contentContainer.appendChild(p);
}
});
}
})();