forked from dodying/UserJs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprettyJSON.user.js
105 lines (94 loc) · 3.63 KB
/
prettyJSON.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* eslint-env browser */
// ==UserScript==
// @name []prettyJSON
// @description prettyJSON
// @include *
// @version 1.1.9
// @modified 2020/6/8 14:01:31
// @author dodying
// @namespace https://github.com/dodying/UserJs
// @supportURL https://github.com/dodying/UserJs/issues
// @icon https://gitee.com/dodying/userJs/raw/master/Logo.png
// @run-at document-end
// @grant none
// @noframes
// ==/UserScript==
(function () {
try {
let text = document.body.textContent;
let jsonp = null;
if (text.match(/^([a-z0-9$_]+)\((.*)\)$/i)) [, jsonp, text] = text.match(/^([a-z0-9$_]+)\((.*)\)$/i);
/* eslint-disable no-eval */
const json = jsonp ? window.eval(text) : JSON.parse(text);
/* eslint-enable no-eval */
if (typeof json !== 'object') return;
console.log(json);
window.$json = json;
if (JSON.stringify(json) === '{}') return;
text = JSON.stringify(json, null, 2);
if (jsonp) text = `${jsonp}(${text})`;
const arr = text.split(/\n/);
const pretty = document.createElement('div');
pretty.classList.add('pretty');
for (let i = 0; i < arr.length; i++) {
const elem = document.createElement('p');
elem.setAttribute('order', i + 1);
const textThis = arr[i];
const arrThis = textThis.split(/(\s)/);
for (let j = 0; j < arrThis.length; j++) {
if (arrThis[j].match(/^(.*?)(https?:\/\/.*?)(["'].*?)$/)) {
const matched = arrThis[j].match(/^(.*?)(https?:\/\/.*?)(["'].*?)$/);
elem.appendChild(document.createTextNode(matched[1]));
const a = document.createElement('a');
a.setAttribute('target', '_blank');
a.setAttribute('href', matched[2]);
a.textContent = matched[2];
elem.appendChild(a);
elem.appendChild(document.createTextNode(matched[3]));
// } else if (arrThis[j].match(/\s/)) {
// const span = document.createElement('span');
// span.classList.add('white');
// elem.appendChild(span);
} else {
elem.appendChild(document.createTextNode(arrThis[j]));
}
}
pretty.appendChild(elem);
}
// const raw = document.createElement('div');
// raw.classList.add('raw');
// raw.textContent = text;
const style = document.createElement('style');
style.textContent = [
'body{}',
'body>div{white-space:pre-wrap;word-break:break-word;font-family:Consolas,Monaco,monospace;}',
'body>div.pretty>p{margin:0;}',
`body>div.pretty>p::before{content:attr(order);width:${arr.length.toString().length * 12}px;display:inline-block;border-right:solid #000 1px;margin-right:4px;}`,
'body>div.pretty>p .white::before{content:"·";}',
'body>div.raw{display:none;}',
'body{background-color:#ffffff;color:#000000;}',
'body>div.pretty>p::before{color:#237893;}',
'body>div.pretty>p .white::before{color: #3333;}',
'@media (prefers-color-scheme:dark) {',
' body{background-color:#1e1e1e;color:#d4d4d4;}',
' body>div.pretty>p::before{color:#858585;}',
' body>div.pretty>p .white::before{color:#e3e4e229;}',
'}',
].join('');
document.head.appendChild(style);
document.body.innerHTML = '';
document.body.appendChild(pretty);
// document.body.appendChild(raw);
// document.body.addEventListener('dblclick', e => {
// if (e.target === raw) {
// raw.style.display = 'none';
// pretty.style.display = 'block';
// } else {
// pretty.style.display = 'none';
// raw.style.display = 'block';
// }
// });
} catch (error) {
// console.log(error);
}
}());