2015-09-28 14:33:20 +03:00
|
|
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
|
|
|
|
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
|
2016-03-18 08:26:53 +03:00
|
|
|
"use strict";
|
2015-09-28 14:33:20 +03:00
|
|
|
|
2016-04-27 05:32:42 +03:00
|
|
|
define(function (require, exports, module) {
|
2016-03-21 15:45:06 +03:00
|
|
|
const { render } = require("devtools/client/shared/vendor/react-dom");
|
2017-04-12 09:33:37 +03:00
|
|
|
const { createFactories } = require("devtools/client/shared/react-utils");
|
2017-10-01 21:14:40 +03:00
|
|
|
const { MainTabbedArea } = createFactories(require("./components/MainTabbedArea"));
|
2017-10-24 04:55:07 +03:00
|
|
|
const TreeViewClass = require("devtools/client/shared/components/tree/TreeView");
|
2016-03-18 08:26:53 +03:00
|
|
|
|
2017-10-24 04:55:07 +03:00
|
|
|
const AUTO_EXPAND_MAX_SIZE = 100 * 1024;
|
|
|
|
const AUTO_EXPAND_MAX_LEVEL = 7;
|
2016-03-18 08:26:53 +03:00
|
|
|
|
2017-05-10 10:48:00 +03:00
|
|
|
let prettyURL;
|
2016-03-18 08:26:53 +03:00
|
|
|
|
|
|
|
// Application state object.
|
|
|
|
let input = {
|
2017-11-28 18:28:21 +03:00
|
|
|
jsonText: JSONView.json,
|
2016-03-18 08:26:53 +03:00
|
|
|
jsonPretty: null,
|
2017-06-03 21:58:00 +03:00
|
|
|
headers: JSONView.headers,
|
2016-07-15 10:59:21 +03:00
|
|
|
tabActive: 0,
|
2016-03-18 08:26:53 +03:00
|
|
|
prettified: false
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Application actions/commands. This list implements all commands
|
|
|
|
* available for the JSON viewer.
|
|
|
|
*/
|
|
|
|
input.actions = {
|
2017-01-12 01:21:56 +03:00
|
|
|
onCopyJson: function () {
|
2017-11-28 18:28:21 +03:00
|
|
|
let text = input.prettified ? input.jsonPretty : input.jsonText;
|
|
|
|
copyString(text.textContent);
|
2016-03-18 08:26:53 +03:00
|
|
|
},
|
|
|
|
|
2017-01-12 01:21:56 +03:00
|
|
|
onSaveJson: function () {
|
2017-05-10 10:48:00 +03:00
|
|
|
if (input.prettified && !prettyURL) {
|
2017-11-28 18:28:21 +03:00
|
|
|
prettyURL = URL.createObjectURL(new window.Blob([input.jsonPretty.textContent]));
|
2017-05-10 10:48:00 +03:00
|
|
|
}
|
|
|
|
dispatchEvent("save", input.prettified ? prettyURL : null);
|
2016-03-18 08:26:53 +03:00
|
|
|
},
|
|
|
|
|
2017-01-12 01:21:56 +03:00
|
|
|
onCopyHeaders: function () {
|
2017-11-24 19:50:22 +03:00
|
|
|
let value = "";
|
|
|
|
let isWinNT = document.documentElement.getAttribute("platform") === "win";
|
|
|
|
let eol = isWinNT ? "\r\n" : "\n";
|
|
|
|
|
|
|
|
let responseHeaders = input.headers.response;
|
|
|
|
for (let i = 0; i < responseHeaders.length; i++) {
|
|
|
|
let header = responseHeaders[i];
|
|
|
|
value += header.name + ": " + header.value + eol;
|
|
|
|
}
|
|
|
|
|
|
|
|
value += eol;
|
|
|
|
|
|
|
|
let requestHeaders = input.headers.request;
|
|
|
|
for (let i = 0; i < requestHeaders.length; i++) {
|
|
|
|
let header = requestHeaders[i];
|
|
|
|
value += header.name + ": " + header.value + eol;
|
|
|
|
}
|
|
|
|
|
|
|
|
copyString(value);
|
2016-03-18 08:26:53 +03:00
|
|
|
},
|
|
|
|
|
2017-01-12 01:21:56 +03:00
|
|
|
onSearch: function (value) {
|
2016-03-18 08:26:53 +03:00
|
|
|
theApp.setState({searchFilter: value});
|
|
|
|
},
|
|
|
|
|
2017-01-12 01:21:56 +03:00
|
|
|
onPrettify: function (data) {
|
2017-09-12 13:15:00 +03:00
|
|
|
if (input.json instanceof Error) {
|
2017-07-13 21:47:00 +03:00
|
|
|
// Cannot prettify invalid JSON
|
|
|
|
return;
|
|
|
|
}
|
2016-03-18 08:26:53 +03:00
|
|
|
if (input.prettified) {
|
|
|
|
theApp.setState({jsonText: input.jsonText});
|
|
|
|
} else {
|
|
|
|
if (!input.jsonPretty) {
|
2017-11-28 18:28:21 +03:00
|
|
|
input.jsonPretty = new Text(JSON.stringify(input.json, null, " "));
|
2016-03-18 08:26:53 +03:00
|
|
|
}
|
|
|
|
theApp.setState({jsonText: input.jsonPretty});
|
2015-09-28 14:33:20 +03:00
|
|
|
}
|
2016-03-18 08:26:53 +03:00
|
|
|
|
|
|
|
input.prettified = !input.prettified;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2017-11-24 19:50:22 +03:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2016-11-30 15:55:51 +03:00
|
|
|
/**
|
|
|
|
* 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) {
|
|
|
|
let data = {
|
|
|
|
detail: {
|
|
|
|
type,
|
|
|
|
value,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let contentMessageEvent = new CustomEvent("contentMessage", data);
|
|
|
|
window.dispatchEvent(contentMessageEvent);
|
|
|
|
}
|
|
|
|
|
2016-03-18 08:26:53 +03:00
|
|
|
/**
|
|
|
|
* Render the main application component. It's the main tab bar displayed
|
|
|
|
* at the top of the window. This component also represents ReacJS root.
|
|
|
|
*/
|
|
|
|
let content = document.getElementById("content");
|
2017-11-28 18:28:21 +03:00
|
|
|
let 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.expandedNodes = new Set();
|
|
|
|
input.tabActive = 1;
|
|
|
|
return new Promise(resolve => {
|
|
|
|
document.addEventListener("DOMContentLoaded", resolve, {once: true});
|
|
|
|
}).then(parseJSON).then(() => {
|
|
|
|
// Now update the state and switch to the JSON tab.
|
|
|
|
theApp.setState({
|
|
|
|
tabActive: 0,
|
|
|
|
json: input.json,
|
|
|
|
expandedNodes: input.expandedNodes,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the JSON has been loaded, parse it immediately before loading the app.
|
|
|
|
let jsonString = input.jsonText.textContent;
|
|
|
|
try {
|
|
|
|
input.json = JSON.parse(jsonString);
|
|
|
|
} catch (err) {
|
|
|
|
input.json = err;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
})();
|
|
|
|
|
2016-03-21 15:45:06 +03:00
|
|
|
let theApp = render(MainTabbedArea(input), content);
|
2016-03-18 08:26:53 +03:00
|
|
|
|
2017-11-28 18:28:21 +03:00
|
|
|
// Send readyState change notification event to the window. Can be useful for
|
2016-03-18 08:26:53 +03:00
|
|
|
// tests as well as extensions.
|
2017-11-28 18:28:21 +03:00
|
|
|
JSONView.readyState = "interactive";
|
|
|
|
window.dispatchEvent(new CustomEvent("AppReadyStateChange"));
|
|
|
|
|
|
|
|
promise.then(() => {
|
|
|
|
// Another readyState change notification event.
|
|
|
|
JSONView.readyState = "complete";
|
|
|
|
window.dispatchEvent(new CustomEvent("AppReadyStateChange"));
|
|
|
|
});
|
2015-09-28 14:33:20 +03:00
|
|
|
});
|