卡世界数据导出器

卡世界辅助脚本,产品管理列表数据导出

// ==UserScript==
// @name         卡世界数据导出器
// @namespace    http://tampermonkey.net/
// @version      0.3
// @license      MIT
// @description  卡世界辅助脚本,产品管理列表数据导出
// @author       byhgz
// @match        https://www.ksjhaoka.com/
// @icon         https://www.google.com/s2/favicons?sz=64&domain=ksjhaoka.com
// @require      https://greasyfork.org/scripts/462234-message/code/Message.js?version=1170653
// @grant        GM_registerMenuCommand
// ==/UserScript==


const Util = {
    /**
     *注册一个菜单并返回菜单id,可在插件中点击油猴时看到对应脚本的菜单
     * @param {string}text 显示文本
     * @param {function}func 事件
     * @param {string}shortcutKey 快捷键
     * @return menu 菜单id
     */
    addGMMenu(text, func, shortcutKey = null) {
        return GM_registerMenuCommand(text, func, shortcutKey);
    },
    /**
     * 内容导出为文件
     * @param {String}content 内容
     * @param {String}fileName 文件名
     */
    fileDownload(content, fileName) {
        // 获取导出文件内容
        // 创建隐藏的下载文件链接
        const element = document.createElement('a');
        element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(content));
        element.setAttribute('download', fileName);
        element.style.display = 'none';
        document.body.appendChild(element);
        // 手动触发下载
        element.click();
        // 清理dom
        document.body.removeChild(element);
    }
}


function getData() {
    const dataList = [];
    document.querySelectorAll(".app-main .el-row>div").forEach(value => {
        const data = {};
        data["运营商"] = value.querySelector(".goods-header-title").textContent.match(/运营商:(.+)/)[1];
        data["上架时间"] = value.querySelector(".goods-header-time").textContent.match(/上架时间:(.+)/)[1];
        const tempTitle = value.querySelector(".overflow-ellipsis").textContent;
        data["title"] = tempTitle.substring(2, tempTitle.indexOf("卡") + 1);
        const tempTitleInfo = tempTitle.substring(tempTitle.indexOf("卡") + 1);
        data["titleInfo"] = tempTitleInfo;
        const monthlyRentIndexOf = tempTitleInfo.indexOf("元");
        let temp_monthlyRent;
        if (monthlyRentIndexOf !== -1) {
            temp_monthlyRent = tempTitleInfo.substring(0, monthlyRentIndexOf);
        } else {
            temp_monthlyRent = "未描述";
        }
        data["月租"] = temp_monthlyRent;
        const tagsEl = value.querySelectorAll(".goods-tab>span");
        const placeOfBelonging = tagsEl[0].textContent;
        data["归属地"] = placeOfBelonging.includes("归属地") ? placeOfBelonging.match(/(.+)归属地/)[1] : placeOfBelonging;
        data["快递"] = tagsEl[1].textContent;
        data["合约"] = tagsEl[2].textContent;
        data["年龄要求"] = tagsEl[3].textContent;
        let tempActivationPlan;
        if (tagsEl.length === 5) {
            tempActivationPlan = tagsEl[4].textContent;
        } else {
            tempActivationPlan = "未描述"
        }
        data["激活方案"] = tempActivationPlan;
        data["佣金"] = value.querySelector(".goods-brokerage div").textContent;
        value.querySelectorAll(".goods-info-ul>.goods-info-li").forEach(li => {
            const strings = li.querySelector(".goods-info-value").textContent.split(":");
            data[strings[0]] = strings[1];
        });
        data["original_title"] = tempTitle;
        data["img"] = value.querySelector(".goods-img").src;
        for (const key in data) {
            data[key] = data[key].trim();
        }
        dataList.push(data);
    })
    return dataList;
}


(function () {
    'use strict';
    Util.addGMMenu("获取当前产品页面列表", () => {
        const url = document.documentURI;
        if (!url.endsWith("/#/goods/sale")) {
            //提示
            alert("当前页面不是产品管理");
            return;
        }
        const data = getData();
        console.log("===========当前页面产品列表===========")
        console.log(data)
        console.log("===========当前页面产品列表===========")
        alert("已打印在控制台上")
    })
})();