淘口令解析

将淘宝生成的淘口令转换为链接

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name            淘口令解析
// @name:en         Taobao Command Parse
// @namespace       net.coolkk.taobaocommandparse
// @description     将淘宝生成的淘口令转换为链接
// @description:en  Taobao Command Parse conversion into the link
// @version         1.6.4
// @author          Coolkk
// @icon            https://img.alicdn.com/tps/i3/T1OjaVFl4dXXa.JOZB-114-114.png
// @homepage        https://github.com/Coolkkmeat/TaobaoCommandParse
// @supportURL      https://github.com/Coolkkmeat/TaobaoCommandParse/issues
// @contributionURL https://coolkk.net/
// @license         Apache License 2.0
// @charset		    UTF-8
// @include         http*://*taobao.com/*
// @grant           GM_setValue
// @grant           GM_getValue
// @grant           GM_registerMenuCommand
// @grant           GM_xmlhttpRequest
// @connect         chaozhi.hk
// @connect         taodaxiang.com
// @run-at          document-idle
// ==/UserScript==

(function () {
    /**
     * 严格模式
     */
    "use strict";

    /**
     * 入口
     */
    //设置
    const config = { "data_source_list": ["taobaoke", "taodaxiang"], "data_source_now": GM_getValue("data_source_now", "none") }
    if (config["data_source_now"] == "none") {
        control_dataSource();
    }
    GM_registerMenuCommand("设置数据源", function () {
        control_dataSource();
    });
    //获取元素
    getElement("other");

    /**
     * 控制-设置数据源
     */
    function control_dataSource() {
        let configNew = prompt("请选择解析功能的接口:" + config["data_source_list"].join(" 或 "), config["data_source_now"]);
        if (configNew && configNew !== config["data_source_now"] && config["data_source_list"].indexOf(configNew) > -1) {
            GM_setValue("data_source_now", configNew);
            window.location.reload();
        }
    }

    /**
     * 获取元素
     * @param {string} type 站点类型
     */
    function getElement(type) {
        let element;
        switch (type) {
            case "other"://其它站
                element = document.getElementById("q");
                listenInput(element);
                break;
        }
    }

    /**
     * 监听输入
     * @param {element} element 元素
     */
    function listenInput(element) {
        if (element == null) return;
        element.addEventListener("input", function (e) {
            work(e.target.value, element)
        });
    }

    /**
     * 处理
     * @param {string} text 文本
     * @param {element} element 元素
     */
    function work(text, element) {
        let symbols = ["\\$", "¥", "€", "₤", "₳", "¢", "¤", "฿", "₵", "₡", "₫", "ƒ", "₲", "₭", "£", "₥", "₦", "₱", "〒", "₮", "₩", "₴", "₪", "៛", "﷼", "₢", "M", "₰", "₯", "₠", "₣", "₧", "ƒ", "¥", "\/", "\\(", "\\)"];
        let regExpParamPrepare = symbols.join("|");
        let regExpParam = `(${regExpParamPrepare})([a-zA-Z0-9]*)(${regExpParamPrepare})`;
        let regExpObject = new RegExp(regExpParam);
        let code = text.match(regExpObject);
        code = code == undefined ? false : code[2];
        if (code) {
            element.value = code;
            request(code);
        }
    }

    /**
     * 请求
     * @param {string} code 淘口令
     */
    function request(code) {
        switch (config["data_source_now"]) {
            case "taobaoke":
                GM_xmlhttpRequest({
                    url: "//api.chaozhi.hk/tool/webLogin",
                    method: "POST",
                    responseType: "json",
                    timeout: 10000,
                    headers: { "Content-Type": "application/json", "Host": "api.chaozhi.hk" },
                    data: '{ "type": 2, "name": "TestAccount", "pass": "TestAccount" }',
                    onload: function (res) {
                        res = JSON.parse(res.responseText);
                        GM_xmlhttpRequest({
                            url: "//api.chaozhi.hk/tb/tklParse",
                            method: "POST",
                            responseType: "json",
                            timeout: 10000,
                            headers: { "Content-Type": "application/json", "Host": "api.chaozhi.hk" },
                            data: `{ "tkl": "${code}", "token": "${res.data.token}" }`,
                            onload: function (res) {
                                res = JSON.parse(res.responseText);
                                if (res.msgCode == 0) {
                                    window.location.href = res.data.url;
                                }
                            }
                        });
                    }
                });
                break;
            case "taodaxiang":
                GM_xmlhttpRequest({
                    url: "//taodaxiang.com/taopass/parse/get",
                    method: "POST",
                    responseType: "json",
                    timeout: 10000,
                    headers: { "Content-Type": "application/x-www-form-urlencoded" },
                    data: `content=${code}`,
                    onload: function (res) {
                        res = JSON.parse(res.responseText);
                        if (res.code == 0) {
                            window.location.href = res.data.url;
                        }
                    }
                });
                break;
            default:
                control_dataSource();
                break;
        }
    }
})();