/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/** * Application actions/commands. This list implements all commands * available for the JSON viewer.
*/
input.actions = {
onCopyJson() { const text = input.prettified ? input.jsonPretty : input.jsonText;
copyString(text.textContent);
},
/** * Helper for copying a string to the clipboard. * * @param {String} string The text to be copied.
*/ function copyString(string) {
document.addEventListener( "copy",
event => {
event.clipboardData.setData("text/plain", string);
event.preventDefault();
},
{ once: true }
);
document.execCommand("copy", false, null);
}
/** * Helper for dispatching an event. It's handled in chrome scope. * * @param {String} type Event detail type * @param {Object} value Event detail value
*/ function dispatchEvent(type, value) { const data = {
detail: {
type,
value,
},
};
const contentMessageEvent = new CustomEvent("contentMessage", data);
window.dispatchEvent(contentMessageEvent);
}
/** * Render the main application component. It's the main tab bar displayed * at the top of the window. This component also represents ReacJS root.
*/ const content = document.getElementById("content"); const promise = (async function parseJSON() { if (document.readyState == "loading") { // If the JSON has not been loaded yet, render the Raw Data tab first.
input.json = {};
input.activeTab = TABS.RAW_DATA; returnnew Promise(resolve => {
document.addEventListener("DOMContentLoaded", resolve, { once: true });
})
.then(parseJSON)
.then(async () => { // Now update the state and switch to the JSON tab.
await appIsReady;
theApp.setState({
activeTab: TABS.JSON,
json: input.json,
expandedNodes: input.expandedNodes,
});
});
}
// If the JSON has been loaded, parse it immediately before loading the app. const jsonString = input.jsonText.textContent; try {
input.json = JSON.parse(jsonString, (key, parsedValue, { source }) => { // Parsed numbers might be different than the source, for example // JSON.parse("1516340399466235648") returns 1516340399466235600. if ( typeof parsedValue === "number" &&
source !== parsedValue.toString() && // `(-0).toString()` returns "0", but the value is correctly parsed as -0, so we // shouldn't render JSON_NUMBER here.
source !== "-0"
) { // In such case, we want to show the actual source, as well as an indication // to the user that the parsed value might be different. return {
source,
type: JSON_NUMBER,
parsedValue,
};
}
return parsedValue;
});
} catch (err) {
input.json = err; // Display the raw data tab for invalid json
input.activeTab = TABS.RAW_DATA;
}
// Expand the document by default if its size isn't bigger than 100KB. if (
!(input.json instanceof Error) &&
jsonString.length <= AUTO_EXPAND_MAX_SIZE
) {
input.expandedNodes = TreeViewClass.getExpandedNodes(input.json, {
maxLevel: AUTO_EXPAND_MAX_LEVEL,
});
} return undefined;
})();
const appIsReady = new Promise(resolve => {
render(MainTabbedArea(input), content, function () {
theApp = this;
resolve();
// Send readyState change notification event to the window. Can be useful for // tests as well as extensions.
JSONView.readyState = "interactive";
window.dispatchEvent(new CustomEvent("AppReadyStateChange"));
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.