-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
167 lines (159 loc) · 8.02 KB
/
index.html
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> JSON to URL | URL to JSON </title>
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="bg-gray-800 text-white h-screen" onload="document.getElementById('urlInput').focus();">
<div id="app">
<div class="max-w-2xl mx-auto mt-10 p-5 bg-gray-700 shadow-md rounded">
<h2 class="text-xl font-semibold mb-5">URL to JSON</h2>
<div class="mb-4">
<input type="text" id="urlInput" v-model="urlInput" placeholder="https://example.com?param1=value1¶m2=value2"
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-300 bg-gray-800 border-gray-600 leading-tight focus:outline-none focus:shadow-outline">
</div>
<div class="mb-6">
<label for="jsonOutput" class="block text-gray-300 text-sm font-bold mb-2">JSON:</label>
<textarea id="jsonOutput" v-model="jsonOutput" rows="18" readonly
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-300 bg-gray-800 border-gray-600 leading-tight focus:outline-none focus:shadow-outline bg-gray-100"></textarea>
</div>
<button @click="copyJsonToClipboard"
class="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">
Copy JSON to Clipboard {{ copyJsonCheck }}
</button>
<button @click="clearURLtoJSON"
class="bg-gray-500 hover:bg-gray-600 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">
Clear
</button>
</div>
<div class="max-w-2xl mx-auto mt-10 p-5 bg-gray-700 shadow-md rounded">
<h2 class="text-xl font-semibold mb-5">JSON to URL</h2>
<div class="mb-4">
<label for="baseURL" class="block text-gray-300 text-sm font-bold mb-2">Base URL:</label>
<input type="text" id="baseURL" v-model="baseURL" placeholder="https://example.com"
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-300 bg-gray-800 border-gray-600 leading-tight focus:outline-none focus:shadow-outline">
</div>
<div class="mb-2">
<label for="jsonInput" class="block text-gray-300 text-sm font-bold mb-2">Parameters:</label>
<textarea id="jsonInput" v-model="jsonInput" rows="18" placeholder='{"key1":"value1", "key2":"value2"}'
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-300 bg-gray-800 border-gray-600 leading-tight focus:outline-none focus:shadow-outline"></textarea>
<p v-if="jsonInput != '' && !isValidJSON" class="text-red-500 text-xs italic">Invalid JSON input.</p>
</div>
<p class="mb-4 break-words"><a v-if="baseURL != '' && isValidJSON" :href="generatedURL" target="_blank">{{ generatedURL }}</a></p>
<a v-if="baseURL != '' && isValidJSON" :href="generatedURL" target="_blank"
class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline mr-2" title="Open generated URL in new tab">
Open URL
</a>
<button @click="copyUrlToClipboard"
class="bg-green-500 hover:bg-green-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline" title="Copy generated URL to clipboard">
Copy URL to Clipboard {{ copyUrlCheck }}
</button>
<button @click="clearJSONtoURL"
class="bg-gray-500 hover:bg-gray-600 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">
Clear
</button>
</div>
</div>
<script>
new Vue({
el: '#app',
data: {
//inputs
urlInput: '',
baseURL: '',
jsonInput: '',
//outputs
jsonOutput: '',
generatedURL: '',
copyJsonCheck: '',
copyUrlCheck: '',
},
watch: {
urlInput: function(newVal) {
this.parseURL(newVal);
}
},
computed: {
isValidJSON: function() {
if(this.jsonInput === '' || this.jsonInput.length < 2) return false;
try {
this.generateURL();
return true;
} catch (e) {
return false;
}
}
},
methods: {
generateURL: function() {
//Add double quotes to the JSON keys if they don't have them.
const fixedInput = (this.jsonInput.trim().replace(/{\s*/, "{").length >= 2 && !(/[a-zA-Z]/.test(this.jsonInput.trim().replace(/{\s*/, "{").charAt(1))))
? this.jsonInput.trim().replace(/{\s*/, "{")
// This regex looks for unquoted keys and adds double quotes around them
: this.jsonInput.replace(/([{,]\s*)([^\s:{},]+)\s*:/g, '$1"$2":')
// Additionally, ensure single-quoted strings are replaced with double quotes
.replace(/:\s*'([^']+)'/g, ':"$1"')
// Handle special case where single quotes are used inside the strings
.replace(/'([^']+)'(?=[,}])/g, '"$1"');
// Parse the adjusted string as JSON and convert to query parameters
try {
const paramsObj = JSON.parse(fixedInput);
const queryParams = new URLSearchParams(paramsObj).toString();
this.generatedURL = `${this.baseURL}?${queryParams}`;
console.log('is a valid JSON');
console.log(`URL: ${this.generatedURL}`);
} catch (e) {
console.error("Failed to generate URL: ", e);
throw Error("Failed to generate URL: ", e);
}
},
parseURL: function(url) {
const parser = document.createElement('a');
parser.href = url;
const query = parser.search.substring(1);
const vars = query.split('&');
if(vars[0] === "") return;
const result = {};
for (let varPair of vars) {
const pair = varPair.split('=');
const key = decodeURIComponent(pair[0]);
const value = decodeURIComponent(pair[1] || '');
result[key] = value;
}
this.jsonOutput = JSON.stringify(result, null, 2);
},
copyUrlToClipboard: function() {
if(!this.isValidJSON) return false;
const tempInput = document.createElement('input');
document.body.appendChild(tempInput);
tempInput.value = this.generatedURL;
tempInput.select();
document.execCommand('copy');
document.body.removeChild(tempInput);
this.copyUrlCheck = '✔';
},
copyJsonToClipboard: function() {
if(this.jsonOutput == '') return false;
const jsonOutput = document.getElementById('jsonOutput');
jsonOutput.select();
document.execCommand('copy');
this.copyJsonCheck = '✔';
},
clearURLtoJSON: function() {
this.urlInput = '';
this.jsonOutput = '';
this.copyJsonCheck = '';
},
clearJSONtoURL: function() {
this.baseURL = '';
this.jsonInput = '';
this.copyUrlCheck = '';
},
}
});
</script>
</body>
</html>