Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
270 lines (234 loc) · 7.81 KB
/
Copy pathscript.js
File metadata and controls
270 lines (234 loc) · 7.81 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
const controllers = [
'AccountOrderController',
'AccountTracker',
'AccountsController',
'AddressBookController',
'AlertController',
'AnnouncementController',
'AppMetadataController',
'AppStateController',
'ApprovalController',
'AuthenticationController',
'BridgeController',
'BridgeStatusController',
'CronjobController',
'CurrencyController',
'DecryptMessageController',
'EncryptionPublicKeyController',
'EnsController',
'GasFeeController',
'KeyringController',
'LoggingController',
'MetaMetricsController',
'MetaMetricsDataDeletionController',
'MultichainBalancesController',
'MultichainRatesController',
'NameController',
'NetworkController',
'NetworkOrderController',
'NftController',
'NotificationServicesController',
'NotificationServicesPushController',
'OnboardingController',
'PPOMController',
'PermissionController',
'PermissionLogController',
'PhishingController',
'PreferencesController',
'SelectedNetworkController',
'SignatureController',
'SmartTransactionsController',
'SnapController',
'SnapInsightsController',
'SnapInterfaceController',
'SnapsRegistry',
'SubjectMetadataController',
'SwapsController',
'TokenBalancesController',
'TokenListController',
'TokenRatesController',
'TokensController',
'TransactionController',
'UserOperationController',
'UserStorageController'
];
function showDiff() {
const input1 = document.getElementById('input1').value;
const input2 = document.getElementById('input2').value;
let stateA, stateB;
try {
stateA = JSON.parse(input1);
stateB = JSON.parse(input2);
} catch (e) {
alert('Invalid JSON input');
return;
}
const diffOutput1 = document.getElementById('diffOutput1');
const diffOutput2 = document.getElementById('diffOutput2');
diffOutput1.innerHTML = '';
diffOutput2.innerHTML = '';
displayControllers(stateA, stateB, diffOutput1, 'A');
displayControllers(stateB, stateA, diffOutput2, 'B');
}
function displayControllers(state, otherState, outputElement, column) {
if (!state.data) {
outputElement.textContent = 'No data available';
return;
}
controllers.forEach(controller => {
const controllerDiv = document.createElement('div');
const title = document.createElement('h4');
title.textContent = controller;
title.classList.add('controller-title');
title.dataset.controller = controller;
title.dataset.column = column;
title.addEventListener('click', () => toggleController(controller));
const icon = document.createElement('span');
icon.textContent = '+';
icon.classList.add('toggle-icon');
title.prepend(icon);
const content = document.createElement('pre');
// Always use the current state's data for display
const controllerData = state.data[controller];
const otherControllerData = otherState.data ? otherState.data[controller] : undefined;
// Pass the states in the correct order for comparison
content.innerHTML = highlightDifferences(
controllerData, // current state's data
otherControllerData, // other state's data for comparison
column
);
if (JSON.stringify(controllerData) !== JSON.stringify(otherControllerData)) {
title.classList.add('updated');
}
content.classList.add('controller-content', 'hidden');
content.dataset.controller = controller;
controllerDiv.appendChild(title);
controllerDiv.appendChild(content);
outputElement.appendChild(controllerDiv);
});
}
function toggleController(controller) {
const titles = document.querySelectorAll(`.controller-title[data-controller="${controller}"]`);
const contents = document.querySelectorAll(`.controller-content[data-controller="${controller}"]`);
contents.forEach(content => {
content.classList.toggle('hidden');
});
titles.forEach(title => {
const icon = title.querySelector('.toggle-icon');
const content = document.querySelector(`.controller-content[data-controller="${controller}"]`);
icon.textContent = content.classList.contains('hidden') ? '+' : '-';
});
}
function highlightDifferences(obj1, obj2, column) {
if (!obj1 || !obj2) {
return JSON.stringify(column === 'B' ? obj2 : obj1, null, 2);
}
const diff = compareObjects(obj1, obj2, column);
return formatDiff(diff);
}
function compareObjects(obj1, obj2, column) {
const result = {};
const allKeys = new Set([...Object.keys(obj1 || {}), ...Object.keys(obj2 || {})]);
for (const key of allKeys) {
const val1 = obj1?.[key];
const val2 = obj2?.[key];
// Always use the first parameter's value (obj1)
const currentValue = val1;
// Key exists in both objects
if (key in (obj1 || {}) && key in (obj2 || {})) {
if (typeof val1 === 'object' && val1 !== null && typeof val2 === 'object' && val2 !== null) {
// For arrays, handle them directly
if (Array.isArray(val1) || Array.isArray(val2)) {
if (JSON.stringify(val1) !== JSON.stringify(val2)) {
result[key] = {
value: currentValue,
type: 'updated'
};
} else {
result[key] = {
value: currentValue,
type: 'equal'
};
}
} else {
// For objects, recurse
const nestedDiff = compareObjects(val1, val2, column);
if (Object.keys(nestedDiff).length > 0) {
result[key] = { value: nestedDiff, type: 'nested' };
} else {
result[key] = { value: currentValue, type: 'equal' };
}
}
} else if (JSON.stringify(val1) !== JSON.stringify(val2)) {
result[key] = {
value: currentValue,
type: 'updated'
};
} else {
result[key] = {
value: currentValue,
type: 'equal'
};
}
}
// Key only in current object
else if (key in (obj1 || {})) {
result[key] = { value: val1, type: 'updated' };
}
}
return result;
}
function formatDiff(diff) {
const formatted = [];
for (const key in diff) {
const { value, type, oldValue } = diff[key];
let formattedValue;
if (value === null) {
formattedValue = 'null';
} else if (typeof value === 'object' && type === 'nested') {
formattedValue = formatDiff(value);
} else if (typeof value === 'object') {
if (Array.isArray(value)) {
formattedValue = `[${value.map(v => JSON.stringify(v)).join(', ')}]`;
} else if (Object.keys(value).length === 0) {
formattedValue = '{}';
} else {
formattedValue = JSON.stringify(value, null, 2);
}
} else {
formattedValue = JSON.stringify(value);
}
const className = type === 'updated' ? 'updated' : type === 'equal' ? 'equal' : '';
formatted.push(`<div class="${className}">${key}: ${formattedValue}</div>`);
}
return formatted.join('');
}
function copyCommand(event) {
const command = document.getElementById('exportCommand').textContent;
navigator.clipboard.writeText(command).then(() => {
showTooltip(event);
}).catch(err => {
console.error('Failed to copy command: ', err);
});
}
function showTooltip(event) {
const tooltip = document.createElement('div');
tooltip.className = 'tooltip';
tooltip.textContent = 'Copied';
document.body.appendChild(tooltip);
const rect = event.target.getBoundingClientRect();
tooltip.style.left = `${rect.left + window.scrollX + rect.width / 2}px`;
tooltip.style.top = `${rect.top + window.scrollY - rect.height}px`;
setTimeout(() => {
tooltip.remove();
}, 1000);
}
function clearInput(inputId) {
document.getElementById(inputId).value = '';
}
function clearAll() {
document.getElementById('input1').value = '';
document.getElementById('input2').value = '';
document.getElementById('diffOutput1').innerHTML = '';
document.getElementById('diffOutput2').innerHTML = '';
}