Taobao Sales Filter

根据销量过滤淘宝搜索页面的商品

目前为 2019-10-30 提交的版本,查看 最新版本

// ==UserScript==
// @name         Taobao Sales Filter
// @namespace    zzway.space
// @version      0.3
// @description  根据销量过滤淘宝搜索页面的商品
// @author       Zzway
// @match        https://s.taobao.com/*
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==

(function () {
    'use strict'

    start()

    var mutationObserver = new MutationObserver(function (mutations) {
        mutations.forEach(function (mutation) {
            if (!mutation.target.classList.contains('nprogress-busy')) {
                start()
            }
        })
    })
    mutationObserver.observe(document.documentElement, {
        attributes: true
    })

    var myInterval
    
    function start() {
        myInterval = setInterval(detect, 500)
        function detect() {
            let test = document.querySelector('.tb-side > ul')
            console.info(test)
            if (test) { main() }
        }
    }

    function main() {
        clearInterval(myInterval)

        let li = document.createElement('li')
        li.title = '鼠标点击空白处或按回车 即可生效'
        let input = document.createElement('input')
        input.type = 'number'
        input.min = 0
        input.style.width = '100%'
        input.value = GM_getValue('limit', 1)
        let span = document.createElement('span')
        span.innerText = '销量过滤'
        span.style.backgroundColor = '#f40'
        span.style.color = 'white'
        span.style['font-size'] = 'x-large'

        li.appendChild(input)
        li.appendChild(span)
        let root = document.querySelector('.tb-side > ul')
        root.appendChild(li)

        let itemList = document.querySelectorAll('.items > div.item')
        let cntList = document.querySelectorAll('div.deal-cnt')

        filter()
        input.addEventListener('focusout', filter)
        input.addEventListener('keydown', e => {
            if (e.code == 'Enter') { filter() }
        })
        function filter() {
            cntList.forEach((element, key) => {
                let number = Number(element.innerText.replace('人付款', '').replace('人收货', ''))
                console.info(number)
                console.info(number < input.value)
                if (number < input.value) {
                    itemList[key].hidden = true
                    if (itemList[key].className.search('item-ad') > -1) {
                        itemList[key].style.setProperty('display', 'none', 'important')
                    }
                } else {
                    itemList[key].hidden = false
                    if (itemList[key].className.search('item-ad') > -1) {
                        itemList[key].style.setProperty('display', 'initial')
                    }
                }
            })
            GM_setValue('limit', input.value)
        }
    }

})()