<!DOCTYPE html>
<html>
<head>
<title>TxtFilter文件处理器</title>
<meta charset="UTF-8">
<link rel="stylesheet" href="txtFilter.css">
</head>
<body>
<div class="txtFilter">
<h2>文本过滤器</h2>
<p>使用方式</p>
<p>1、选择一个TXT格式的记事本文档</p>
<p>2、内容一行一个</p>
<p>3、设置您所需要的过滤条件</p>
<p>4、点击一键处理即可</p>
<p>生成后的文档将按开头相似性排序</p>
<div class="input">
<span>选择文件</span>
<label for="txt">
<em id="labeltxt">请选择文件</em>
<input type="file" hidden id="txt" placeholder="请选择文件">
</label>
</div>
<div class="input">
<span>必须包含</span>
<input type="text" id="reqkeyword" placeholder="请输入,多个关键词请用|隔开">
</div>
<div class="input">
<span>不能包含</span>
<input type="text" id="unReqkeyword" placeholder="请输入,多个关键词请用|隔开">
</div>
<div class="input">
<span>最低字数</span>
<input type="number" id="minlingth" placeholder="请输入">
</div>
<div class="input">
<span>最多字数</span>
<input type="number" id="maxlength" placeholder="请输入">
</div>
<div class="input2">
<button id="handle">一键处理</button>
</div>
</div>
<script src="popup.js"></script>
</body>
</html>
function displayFileName() {
const fileInput = document.getElementById('txt');
const selectedFileName = document.getElementById('labeltxt');
if (fileInput.files.length > 0) {
selectedFileName.textContent = fileInput.files[0].name;
} else {
selectedFileName.textContent = '请选择文件';
}
}
const txt = document.getElementById('txt');
txt.addEventListener('change', displayFileName);
function handleFilter() {
const fileInput = document.getElementById('txt');
const reqKeywordsInput = document.getElementById('reqkeyword');
const unReqKeywordsInput = document.getElementById('unReqkeyword');
const minLengthInput = document.getElementById('minlingth');
const maxLengthInput = document.getElementById('maxlength');
const file = fileInput.files[0];
const reqKeywords = reqKeywordsInput.value.trim().split('|').filter(keyword => keyword !== '');
const unReqKeywords = unReqKeywordsInput.value.trim().split('|').filter(keyword => keyword !== '');
const minLength = parseInt(minLengthInput.value);
const maxLength = parseInt(maxLengthInput.value);
if (!file) {
alert('请选择文件');
return;
}
const reader = new FileReader();
reader.onload = function (e) {
const contents = e.target.result;
const lines = contents.split('\n');
const uniqueLines = new Set(lines);
const filteredLines = Array.from(uniqueLines).filter(line => {
const trimmedLine = line.trim();
const lineLength = trimmedLine.length;
const hasReqKeywords = reqKeywords.length === 0 || reqKeywords.some(keyword => trimmedLine.includes(keyword));
const hasUnReqKeywords = unReqKeywords.length === 0 || unReqKeywords.every(keyword => !trimmedLine.includes(keyword));
const meetsMinLength = isNaN(minLength) || lineLength >= minLength;
const meetsMaxLength = isNaN(maxLength) || lineLength <= maxLength;
return (reqKeywords.length === 0 || hasReqKeywords) && hasUnReqKeywords && meetsMinLength && meetsMaxLength;
});
const sortedLines = filteredLines.sort((a, b) => a.localeCompare(b));
const output = sortedLines.join('\n');
downloadTxtFile(output);
};
reader.readAsText(file);
}
function downloadTxtFile(content) {
const element = document.createElement('a');
const file = new Blob([content], { type: 'text/plain' });
element.href = URL.createObjectURL(file);
element.download = 'filtered.txt';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
alert('处理完成,已为您下载完成');
}
const handleButton = document.getElementById('handle');
handleButton.addEventListener('click', handleFilter);
已经封装成谷歌浏览器插件了