Greasy Fork

Greasy Fork is available in English.

嘉立创开源广场辅助工具

嘉立创开源广场BOM列表一键搜索淘宝,一键搜索优信,支持配置自定义店铺

当前为 2024-05-08 提交的版本,查看 最新版本

// ==UserScript==
// @name         嘉立创开源广场辅助工具
// @namespace    http://tampermonkey.net/
// @version      1.0.4
// @description  嘉立创开源广场BOM列表一键搜索淘宝,一键搜索优信,支持配置自定义店铺
// @author       Lx
// @match        https://oshwhub.com/**
// @icon         https://www.google.com/s2/favicons?sz=64&domain=szlcsc.com
// @require      https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js
// @grant        GM_openInTab
// @license      MIT
// ==/UserScript==

(async function () {
    'use strict';

    /**
     * 用户自定义配置项
     * @returns
     */
    const getConfig = () => {
        return {
            // ================  大的优先级 valueList > columnNameList  ============================
            // ================  小的优先级 每个集合中从上到下有优先级  ==============================

            // 按钮组要追加所在的列名,从上到下有优先级
            targetAppend: [
                'Device',
                'Name',
            ],
            // 如果valueList--Value没值,会从这几个值里拿,从上到下有优先级
            columnNameList: [
                'Manufacturer Part',
                'Device',
                'Name',
            ],
            // 封装列,从上到下有优先级
            footprintList: [
                'Footprint',
            ],
            // 具体的值,从上到下有优先级
            valueList: [
                'Value',
            ],
            // 淘宝全局搜索,指定店铺简称(支持配置其他店铺)
            //  例如:
            //  storeNameList: [
            //     '优信',
            //     'xxxx', 新增加一行这个,把店铺名简称填在这里。末尾的逗号不能省略
            // ]
            storeNameList: [
                '优信',
            ],
            // 该功能是进入到店铺内进行搜索,需要手动设置店铺url
            // https://shopsearch.taobao.com/search?q=【这里写店铺名称来获取店铺url】
            storeNameDetailList: {
                '优信(主)': 'https://youxin-electronic.taobao.com/',  // 信用值最高
                '优信(备)': 'https://shop35338630.taobao.com/',       // 信用值还行
                '优信-集芯电子': 'https://shop107953716.taobao.com/',   // 深圳市集芯电子 优信分店
            }
        }
    }

    /**
     * 等待
     * @param {*} timeout
     * @returns
     */
    const setAwait = (timeout) => {
        return new Promise((resolve, reject) => {
            setTimeout(resolve, timeout);
        })
    }

    /**
     * 获取索引
     * 从多个指定的列名中,查找最先出现的索引位置
     */
    const getColumnIndex = (columnNames, i = 0, columnIndex = -1) => {
        const $eles = $(`div.table-box .table tr:contains("${columnNames[i]}") th`);
        if ($eles.length === 0) {
            return getColumnIndex(columnNames, ++i);
        }
        [...$eles].some(a => {
            columnIndex++
            return $(a).text() === `${columnNames[i]}`
        });
        if (columnIndex > -1) {
            return columnIndex
        }
    }

    const start = () => {
        // 查询用于跳转淘宝的列索引
        const { targetAppend, columnNameList, footprintList, valueList } = getConfig();

        const targetAppendIndex = getColumnIndex(targetAppend)
        const searchTbIndex = getColumnIndex(columnNameList)
        const footprintIndex = getColumnIndex(footprintList)
        const valueIndex = getColumnIndex(valueList)

        // 没找到的话,等待查找索引成功
        if (searchTbIndex === -1) {
            return;
        }

        // 添加一键搜索BOM的按钮
        $(`div.table-box .table tr`).find(`th:eq(${targetAppendIndex})`).append(`
            <p class="oneKey-search-tb" style='padding: 0px 8px; background-color: #00bfffd1;cursor: pointer;border-radius: 4px; margin-left: 20px;width:min-content;'>
            淘宝一键搜索BOM
            </br>一次性会打开很多页面,慎用!
            </br>同时会被淘宝限流
            </p>
        `)

        // const $tdEles = $(`div.table-box .table tr`).find(`td:eq(${targetAppendIndex})`).css({
        //     "display": "flex",
        //     "justify-content": "space-between"
        // })

        const $tdEles = $(`div.table-box .table tr`).find(`td:eq(${targetAppendIndex})`);
        // 页面渲染按钮组
        // 遍历每一行
        $tdEles.each(function () {
            const $parents = $(this).parents('tr')
            const keyword = $(this).text().trim()
            const $targetAppendTarget = $parents.find(`td:eq(${targetAppendIndex})`)
            const searchTbText = $parents.find(`td:eq(${searchTbIndex})`).text().trim()
            const footprintText = $parents.find(`td:eq(${footprintIndex})`).text().trim()
            const valueText = $parents.find(`td:eq(${valueIndex})`).text().trim()

            // 最后得到的关键字
            const kwd = valueText ? valueText : (searchTbText ? searchTbText : keyword)

            const forHtml = getConfig().storeNameList.map(storeName => {
                return `<p class="search-tb-" data-query="https://s.taobao.com/search?q=${storeName}/${kwd}/${footprintText}"
                style='padding: 0px 8px; background-color: #f4a4608f;cursor: pointer;border-radius: 4px; margin-left: 7px;'>
                搜${storeName}
                </p>`
            }).join('')

            // 店铺中精确搜索
            let forDetailHtml = '';
            for (let [prefixName, storeIndexUrl] of new Map(Object.entries(getConfig().storeNameDetailList))) {
                forDetailHtml += `<p class="search-tb-" data-query="${storeIndexUrl}/search.htm?keyword=${kwd}/${footprintText}"
                style='padding: 0px 8px; background-color: #c0c4cc;cursor: pointer;border-radius: 4px; margin-left: 7px;'>
                搜${prefixName}
                </p>`
            }

            $targetAppendTarget.append(`
            <div style="display: flex;">
                <p class="search-tb" data-query="https://s.taobao.com/search?q=${kwd}/${footprintText}"
                style='padding: 0px 8px; background-color: #00bfff7a;cursor: pointer;border-radius: 4px; margin-left: 7px;'>
                搜淘宝
                </p>
                ${forHtml}
                ${forDetailHtml}
            <div>
            `)
        })

        // 搜索按钮的击事件
        $(`.search-tb-,.search-tb`).click(function () {
            GM_openInTab($(this).data('query'), {})
        })

        $(`.oneKey-search-tb`).click(function () {
            $(`.search-tb`).each(function () {
                GM_openInTab($(this).data('query'), {})
            })
        })
    }

    const timerFunc = () => {
        let timer = setInterval(() => {
            const tableNotEmpty = $('div.table-box .table tr th').length > 0
            const notEmpty = $('.search-tb').length > 0

            console.log(`等待BOM列表加载...`);

            if (!notEmpty && tableNotEmpty) {
                start()
            }
        }, 4000);
    }
    timerFunc()
})()