Fix parsed json values not previewing properly

This commit is contained in:
Kuba Orlik 2021-11-22 13:28:31 +01:00
parent edfde535d2
commit 492332802f
2 changed files with 24 additions and 7 deletions

View File

@ -77,6 +77,9 @@ export class StolenDataEntry extends EventEmitter {
} }
static parseValue(value: unknown): string | Record<string, unknown> { static parseValue(value: unknown): string | Record<string, unknown> {
if (value === undefined) {
return "";
}
if (isJSONObject(value)) { if (isJSONObject(value)) {
const object = parseToObject(value); const object = parseToObject(value);
return object; return object;
@ -101,7 +104,7 @@ export class StolenDataEntry extends EventEmitter {
} }
} }
const object = { const object = {
[Symbol.for("originalURL")]: value, // so it doesn't appear raw in the table but can be easily retrieved later [Symbol.for("originalString")]: value, // so it doesn't appear raw in the table but can be easily retrieved later
host: url.host, host: url.host,
path: url.pathname, path: url.pathname,
...Object.fromEntries( ...Object.fromEntries(
@ -181,14 +184,21 @@ export class StolenDataEntry extends EventEmitter {
} }
getValuePreview(key = ""): string { getValuePreview(key = ""): string {
console.log("getValuePreview", key, this.getParsedValue(key));
const value = this.getParsedValue(key); const value = this.getParsedValue(key);
const str = value.toString(); const str =
typeof value === "object" && value[Symbol.for("originalString")]
? (value[Symbol.for("originalString")] as string)
: value.toString();
if (typeof value !== "object" && this.classification == "id") { if (typeof value !== "object" && this.classification == "id") {
return ( return (
str.slice(0, Math.min(str.length / 3, ID_PREVIEW_MAX_LENGTH)) + "(...)" str.slice(0, Math.min(str.length / 3, ID_PREVIEW_MAX_LENGTH)) + "(...)"
); );
} else if (typeof value === "object" && value[Symbol.for("originalURL")]) { } else if (
return value[Symbol.for("originalURL")] as string; typeof value === "object" &&
value[Symbol.for("originalString")]
) {
return value[Symbol.for("originalString")] as string;
} else { } else {
return str; return str;
} }

13
util.ts
View File

@ -74,12 +74,19 @@ export async function getTabByID(id: number) {
return tabs.find((tab) => tab.id == id); return tabs.find((tab) => tab.id == id);
} }
export function parseToObject(str: unknown): Record<string, unknown> { export function parseToObject(str: unknown): Record<string | symbol, unknown> {
let result: Record<string | symbol, unknown>;
let original_string: string;
if (typeof str === "string") { if (typeof str === "string") {
return JSON.parse(str); original_string = str;
result = JSON.parse(str);
} else if (typeof str == "object") { } else if (typeof str == "object") {
return str as Record<string, unknown>; result = str as Record<string | symbol, unknown>;
original_string =
(result[Symbol.for("originalString")] as string) || JSON.stringify(str);
} }
result[Symbol.for("originalString")] = original_string;
return result;
} }
export function isJSONObject( export function isJSONObject(