Taobao Sales Filter

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

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

(function () {
    'use strict'

    var flag = true

    start()

    var mutationObserver = new MutationObserver(function (mutations) {
        mutations.forEach(function (mutation) {
            console.info(mutation.target)
            if (!mutation.target.classList.contains('nprogress-busy')) {//翻页后再次start()
                start()
            }else{
                flag = true
                console.log('set flag true')
            }
        })
    })


    var myInterval

    function start() {
        console.info('start' + flag)
        if(flag){
            myInterval = setInterval(detect, 500)
            function detect() {
                console.info('detect')
                // console.info(myInterval)
                if (document.querySelector('.tb-side > ul')) {
                    clearInterval(myInterval)
                    main()
                    mutationObserver.observe(document.documentElement, {
                        attributes: true
                    })
                }
            }
            flag = false
        }
    }


    function main() {
        console.info('main')

        let li = document.createElement('li')
        // li.id='SalesFilter'
        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')

        input.addEventListener('focusout', filter)
        input.addEventListener('keydown', e => {
            if (e.code == 'Enter') { filter() }
        })

        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)
        }
    }

})()