98手机网页版搜索助手

98手机网页版搜索助手..

// ==UserScript==
// @name         98手机网页版搜索助手
// @namespace    http://tampermonkey.net/
// @version      1.6
// @description  98手机网页版搜索助手..
// @author       bbbyqq
// @match        https://*sehuatang.org/home.php*
// @match        https://*sehuatang.org/search.php*
// @match        https://*sehuatang.net/home.php*
// @match        https://*sehuatang.net/search.php*
// @match        *://*/*
// @grant        GM_addStyle
// @license      bbbyqq
// @require      https://code.jquery.com/jquery-3.7.1.js
// ==/UserScript==

(function () {
  'use strict'

  const excludes = {
    description: '排除关键词',
    keywords: [
      '求',
      '约定',
      'SHA1'
    ]
  }

  let excludesWrapper = `
      <div class="excludes-wrapper">
          <span>${excludes.description}:</span>
      </div>
    `
  $('.threadlist').prepend(excludesWrapper)

  // 从浏览器缓存中获取勾选状态
  let checkedList = JSON.parse(localStorage.getItem('checkedList')) || []
  removeSearchResult()

  excludes.keywords.forEach(item => {
    $('.excludes-wrapper').append(`<label class="excludes-item"><input type="checkbox" value="${item}" ${checkedList.some(val => item === val) ? 'checked' : ''}/>${item}</label>`)
  })

  let css = `
      .excludes-wrapper {
        font-size: 20px;
        display: flex;
        align-items: center;
        font-weight: 700;
      }
      .excludes-item {
        margin-right: 10px;
      }
      .excludes-item input {
        margin-right: 5px;
      }
    `
  GM_addStyle(css)

  removeNodes()

  // 删除重复元素
  function removeNodes() {
    const wrapperNodeList = document.querySelectorAll('.excludes-wrapper');
    if (wrapperNodeList.length > 1) {
      for (let i = 1; i < wrapperNodeList.length; i++) {
        wrapperNodeList[i].parentNode.removeChild(wrapperNodeList[i])
      }
    }
  }

  // 监听勾选状态
  document.querySelectorAll('.excludes-item input[type="checkbox"]').forEach((checkbox) => {
    checkbox.addEventListener('change', (e) => {
      const isChecked = e.target.checked
      const checkedValue = e.target.value
      if (isChecked) {
        checkedList.push(checkedValue)
      } else {
        checkedList = checkedList.filter(val => val !== checkedValue)
      }
      // 数组去重
      checkedList = Array.from(new Set(checkedList))
      // 浏览器缓存保存勾选状态
      localStorage.setItem('checkedList', JSON.stringify(checkedList))
      removeSearchResult()
    })
  })

  // 隐藏拥有关键词的元素
  function removeSearchResult() {
    const searchList = document.querySelectorAll('.threadlist ul li')
    searchList.forEach(item => {
      if (checkedList.some(val => item.innerHTML.toLowerCase().includes(val.toLowerCase()))) {
        item.style.display = 'none'
      } else {
        item.style.removeProperty('display')
      }
    })
  }
})()