XML to JSON Converter

Upload an XML file and convert it to JSON format, then display the result on the page

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         XML to JSON Converter
// @namespace    none
// @version      1.0.1
// @license MIT
// @description  Upload an XML file and convert it to JSON format, then display the result on the page
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // create a form for uploading files
    var form = document.createElement("form");
    form.style.margin = "20px";

    // create an input element for the file
    var fileInput = document.createElement("input");
    fileInput.type = "file";
    fileInput.name = "file";
    fileInput.accept = "application/xml";
    form.appendChild(fileInput);

    // create a submit button
    var submitButton = document.createElement("input");
    submitButton.type = "submit";
    submitButton.value = "Convert to JSON";
    form.appendChild(submitButton);

    // append the form to the body
    document.body.appendChild(form);

    // create a div for displaying the result
    var resultDiv = document.createElement("div");
    resultDiv.style.margin = "20px";
    document.body.appendChild(resultDiv);

    // add an event listener for form submission
    form.addEventListener("submit", function(event) {
        event.preventDefault();

        // create a new FormData object and add the file to it
        var formData = new FormData();
        formData.append("file", fileInput.files[0]);

        // send a POST request to the server to convert the file
        var xhr = new XMLHttpRequest();
        xhr.open("POST", "/convert", true);
        xhr.onload = function() {
            // display the result
            resultDiv.innerHTML = xhr.responseText;
        };
        xhr.send(formData);
    });
})();