Discussions » Greasy Fork Feedback

Failed to upload script?

§
Posted: 2019-01-03
Edited: 2019-01-03

Failed to upload script?

很抱歉,您提交的内容有点问题…

Exception 402014

// ==UserScript==
// @name         【Lutra】Java代码转换器
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  将Lutra界面转换成Java代码
// @author       李少杰
// @match        http://lutra.sankuai.com/*
// @grant        none
// ==/UserScript==

(function() {

    const javaPrimTypes = {
        'string': 'String'
    };

    function toCamelCase(name) {
        const segs = name.split('_');
        if (segs.length === 1) return name;
        return segs.reduce(((previousValue, currentValue) => previousValue + currentValue.substr(0, 1).toUpperCase() + currentValue.substr(1)));
    }

    function generateJavaField(field, modelDict, pendingClass) {
        let result = '';
        if (field.desc) {
            result +=
                `    /**
     * ${field.desc}
     */
`;
        }
        let typeName = field.type;
        if (modelDict[typeName]) {
            typeName = modelDict[typeName].name;
            pendingClass.push(field.type);
        } else if (javaPrimTypes[typeName]) {
            typeName = javaPrimTypes[typeName];
        }
        result +=
            `    @SerializedName("${field.name}")
    public ${typeName} ${toCamelCase(field.name)};

`;
        return result;
    }

    function generateJavaClass(typeName, modelDict) {
        const typeModel = modelDict[typeName];
        let pendingClass = [];
        let result =
            `class ${typeModel.name} {
`;
        for (const f in typeModel.fields) {
            if (typeModel.fields.hasOwnProperty(f)) {
                result += generateJavaField(typeModel.fields[f], modelDict, pendingClass);
            }
        }
        result +=
            `}
`;
        pendingClass.forEach(c => {
            result += generateJavaClass(c, modelDict);
        });
        return result;
    }

    function findApi(data, path) {
        for (const key in data.api) {
            if (data.api.hasOwnProperty(key)) {
                const apiModel = data.api[key];
                if (apiModel.path instanceof Array && apiModel.path.find(p => path.indexOf(p) === 0)) {
                    for (const apiKey in apiModel.apis) {
                        if (apiModel.apis.hasOwnProperty(apiKey)) {
                            if (path.endsWith(apiKey)) {
                                return apiModel.apis[apiKey];
                            }
                        }
                    }
                }
            }
        }
    }

    function findReturnBlock() {
        document.querySelectorAll('.dataBlock').forEach(el => {
            if (el.title == '返回') {
                if (!el.isJava) {
                    const btn = document.createElement('button');
                    el.isJava = true;
                    btn.textContent = '转换Java Model';
                    btn.addEventListener('click', async () => {
                        const query = new URLSearchParams(location.search);
                        let path;
                        if (query.get('branchId') == 'all') {
                            path = "/" + window.pid + "/result";
                        } else {
                            path = "/" + window.pid + "/api/branchData/" + query.get('branchId');
                        }
                        let data = await (await fetch(path)).json();
                        const current = query.get('anchor');
                        let api = findApi(data, current);
                        if (api) {
                            el.innerHTML = '<pre>' + generateJavaClass(api.returnType.wrapperData.data.type, data.model) + '</pre>';
                        } else {
                            console.warn('api not found');
                        }
                    });
                    el.insertBefore(btn, el.firstChild);
                }
            }
        });
    }

    setInterval(findReturnBlock, 1000);
    findReturnBlock();
})();
§
Posted: 2019-01-04

Try again, should work now.

Post reply

Sign in to post a reply.