From 2b3b69b73e3518e07fed296e0b99cf43d3921229 Mon Sep 17 00:00:00 2001 From: ciricc Date: Wed, 11 Dec 2024 14:11:13 +0300 Subject: [PATCH] fix: support utf8 bodies --- src/entries/Background/plugins/utils.ts | 11 ++++++----- src/utils/utf8.ts | 7 +++++++ 2 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 src/utils/utf8.ts diff --git a/src/entries/Background/plugins/utils.ts b/src/entries/Background/plugins/utils.ts index bcd78f76..34b1be14 100644 --- a/src/entries/Background/plugins/utils.ts +++ b/src/entries/Background/plugins/utils.ts @@ -1,11 +1,12 @@ import { addPlugin, addPluginConfig, addPluginMetadata } from '../db'; import { getPluginConfig } from '../../../utils/misc'; +import { bytesSize, indexOfString } from '../../../utils/utf8'; export async function installPlugin( urlOrBuffer: ArrayBuffer | string, origin = '', filePath = '', - metadata: {[key: string]: string} = {}, + metadata: { [key: string]: string } = {}, ) { let arrayBuffer; @@ -31,11 +32,11 @@ export async function installPlugin( export function mapSecretsToRange(secrets: string[], text: string) { return secrets .map((secret: string) => { - const index = text.indexOf(secret); - return index > -1 + const byteIdx = indexOfString(text, secret); + return byteIdx > -1 ? { - start: index, - end: index + secret.length, + start: byteIdx, + end: byteIdx + bytesSize(secret) } : null; }) diff --git a/src/utils/utf8.ts b/src/utils/utf8.ts new file mode 100644 index 00000000..1703c78b --- /dev/null +++ b/src/utils/utf8.ts @@ -0,0 +1,7 @@ +export function indexOfString(str: string, substr: string): number { + return Buffer.from(str).indexOf(Buffer.from(substr)); +} + +export function bytesSize(str: string): number { + return Buffer.from(str).byteLength; +} \ No newline at end of file