txt文件去重,可自动排序,筛选导出新txt文件

<!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);

已经封装成谷歌浏览器插件了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值