XML to JSON Converter and Downloader

Upload an XML file and convert it to JSON format, then provide a download link for the converted file.

目前為 2023-04-05 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         XML to JSON Converter and Downloader
// @namespace    none
// @version      1
// @license MIT
// @description  Upload an XML file and convert it to JSON format, then provide a download link for the converted file.
// @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;

            // create a download link for the converted file
            var downloadLink = document.createElement("a");
            downloadLink.href = "data:text/json;charset=utf-8," + encodeURIComponent(xhr.responseText);
            downloadLink.download = "converted.json";
            downloadLink.style.display = "block";
            downloadLink.style.marginTop = "20px";
            downloadLink.innerHTML = "Download JSON file";
            document.body.appendChild(downloadLink);
        };
        xhr.send(formData);
    });
})();