mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-08-08 19:21:32 -07:00
Add inline character portraits and dictionary search workflow (#83)
This commit is contained in:
@@ -788,6 +788,30 @@ test('stripSubtitleAnnotationMetadata keeps known hover data while clearing non-
|
||||
});
|
||||
});
|
||||
|
||||
test('stripSubtitleAnnotationMetadata clears character image metadata from excluded name matches', () => {
|
||||
const token = makeToken({
|
||||
surface: 'は',
|
||||
headword: 'は',
|
||||
reading: 'ハ',
|
||||
partOfSpeech: PartOfSpeech.particle,
|
||||
pos1: '助詞',
|
||||
isNameMatch: true,
|
||||
});
|
||||
token.characterImage = {
|
||||
src: 'data:image/png;base64,AAAA',
|
||||
alt: 'は',
|
||||
};
|
||||
|
||||
assert.deepEqual(stripSubtitleAnnotationMetadata(token), {
|
||||
...token,
|
||||
isNPlusOneTarget: false,
|
||||
isNameMatch: false,
|
||||
characterImage: undefined,
|
||||
jlptLevel: undefined,
|
||||
frequencyRank: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
test('stripSubtitleAnnotationMetadata leaves content tokens unchanged', () => {
|
||||
const token = makeToken({
|
||||
surface: '猫',
|
||||
|
||||
@@ -508,11 +508,17 @@ export function stripSubtitleAnnotationMetadata(
|
||||
return token;
|
||||
}
|
||||
|
||||
return {
|
||||
const strippedToken = {
|
||||
...token,
|
||||
isNPlusOneTarget: false,
|
||||
isNameMatch: false,
|
||||
jlptLevel: undefined,
|
||||
frequencyRank: undefined,
|
||||
};
|
||||
|
||||
if ('characterImage' in strippedToken) {
|
||||
strippedToken.characterImage = undefined;
|
||||
}
|
||||
|
||||
return strippedToken;
|
||||
}
|
||||
|
||||
@@ -1577,18 +1577,24 @@ test('dictionary settings helpers upsert and remove dictionary entries without r
|
||||
assert.match(upsertScript ?? '', /"enabled":true/);
|
||||
});
|
||||
|
||||
test('importYomitanDictionaryFromZip uses settings automation bridge instead of custom backend action', async () => {
|
||||
test('importYomitanDictionaryFromZip imports via localhost URL instead of embedding archive bytes in script', async () => {
|
||||
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'subminer-yomitan-import-'));
|
||||
const zipPath = path.join(tempDir, 'dict.zip');
|
||||
fs.writeFileSync(zipPath, Buffer.from('zip-bytes'));
|
||||
|
||||
const scripts: string[] = [];
|
||||
const servedArchives: string[] = [];
|
||||
const settingsWindow = {
|
||||
isDestroyed: () => false,
|
||||
destroy: () => undefined,
|
||||
webContents: {
|
||||
executeJavaScript: async (script: string) => {
|
||||
scripts.push(script);
|
||||
const urlMatch = script.match(/importDictionaryArchiveUrl\(\s*"([^"]+)"/);
|
||||
if (urlMatch) {
|
||||
const response = await fetch(JSON.parse(`"${urlMatch[1]}"`) as string);
|
||||
servedArchives.push(await response.text());
|
||||
}
|
||||
return true;
|
||||
},
|
||||
},
|
||||
@@ -1611,15 +1617,103 @@ test('importYomitanDictionaryFromZip uses settings automation bridge instead of
|
||||
true,
|
||||
);
|
||||
assert.equal(
|
||||
scripts.some((script) => script.includes('importDictionaryArchiveBase64')),
|
||||
scripts.some((script) => script.includes('importDictionaryArchiveUrl')),
|
||||
true,
|
||||
);
|
||||
assert.deepEqual(servedArchives, ['zip-bytes']);
|
||||
assert.equal(
|
||||
scripts.some((script) => script.includes('emlwLWJ5dGVz')),
|
||||
false,
|
||||
);
|
||||
assert.equal(
|
||||
scripts.some((script) => script.includes('subminerImportDictionary')),
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
test('importYomitanDictionaryFromZip falls back to base64 import for older Yomitan bridge', async () => {
|
||||
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'subminer-yomitan-import-'));
|
||||
const zipPath = path.join(tempDir, 'dict.zip');
|
||||
fs.writeFileSync(zipPath, Buffer.from('zip-bytes'));
|
||||
|
||||
const scripts: string[] = [];
|
||||
const settingsWindow = {
|
||||
isDestroyed: () => false,
|
||||
destroy: () => undefined,
|
||||
webContents: {
|
||||
executeJavaScript: async (script: string) => {
|
||||
scripts.push(script);
|
||||
if (
|
||||
script.includes(
|
||||
'typeof globalThis.__subminerYomitanSettingsAutomation.importDictionaryArchiveUrl',
|
||||
)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const deps = createDeps(async () => true, {
|
||||
createYomitanExtensionWindow: async (pageName: string) => {
|
||||
assert.equal(pageName, 'settings.html');
|
||||
return settingsWindow;
|
||||
},
|
||||
});
|
||||
|
||||
const imported = await importYomitanDictionaryFromZip(zipPath, deps, {
|
||||
error: () => undefined,
|
||||
});
|
||||
|
||||
assert.equal(imported, true);
|
||||
assert.equal(
|
||||
scripts.some((script) => script.includes('importDictionaryArchiveBase64')),
|
||||
true,
|
||||
);
|
||||
assert.equal(
|
||||
scripts.some((script) => script.includes('importDictionaryArchiveUrl(')),
|
||||
false,
|
||||
);
|
||||
assert.equal(
|
||||
scripts.some((script) => script.includes('emlwLWJ5dGVz')),
|
||||
true,
|
||||
);
|
||||
});
|
||||
|
||||
test('importYomitanDictionaryFromZip returns false when served archive cannot be read', async () => {
|
||||
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'subminer-yomitan-import-'));
|
||||
const zipPath = path.join(tempDir, 'dict.zip');
|
||||
fs.writeFileSync(zipPath, Buffer.from('zip-bytes'));
|
||||
|
||||
const settingsWindow = {
|
||||
isDestroyed: () => false,
|
||||
destroy: () => undefined,
|
||||
webContents: {
|
||||
executeJavaScript: async (script: string) => {
|
||||
const urlMatch = script.match(/importDictionaryArchiveUrl\(\s*"([^"]+)"/);
|
||||
if (!urlMatch) return true;
|
||||
fs.unlinkSync(zipPath);
|
||||
const response = await fetch(JSON.parse(`"${urlMatch[1]}"`) as string);
|
||||
return response.ok;
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const deps = createDeps(async () => true, {
|
||||
createYomitanExtensionWindow: async (pageName: string) => {
|
||||
assert.equal(pageName, 'settings.html');
|
||||
return settingsWindow;
|
||||
},
|
||||
});
|
||||
|
||||
const imported = await importYomitanDictionaryFromZip(zipPath, deps, {
|
||||
error: () => undefined,
|
||||
});
|
||||
|
||||
assert.equal(imported, false);
|
||||
});
|
||||
|
||||
test('deleteYomitanDictionaryByTitle uses settings automation bridge instead of custom backend action', async () => {
|
||||
const scripts: string[] = [];
|
||||
const settingsWindow = {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { BrowserWindow, Extension, Session } from 'electron';
|
||||
import * as fs from 'fs';
|
||||
import * as http from 'http';
|
||||
import * as path from 'path';
|
||||
import { selectYomitanParseTokens } from './parser-selection-stage';
|
||||
|
||||
@@ -705,6 +706,70 @@ async function invokeYomitanSettingsAutomation<T>(
|
||||
}
|
||||
}
|
||||
|
||||
async function serveDictionaryZipOnce<T>(
|
||||
zipPath: string,
|
||||
callback: (url: string) => Promise<T>,
|
||||
): Promise<T> {
|
||||
const fileName = path.basename(zipPath);
|
||||
const token = `${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}`;
|
||||
const requestPath = `/${token}/${encodeURIComponent(fileName)}`;
|
||||
let served = false;
|
||||
const server = http.createServer((request, response) => {
|
||||
if (request.method === 'OPTIONS') {
|
||||
response.writeHead(204, {
|
||||
'access-control-allow-origin': '*',
|
||||
'access-control-allow-methods': 'GET, OPTIONS',
|
||||
});
|
||||
response.end();
|
||||
return;
|
||||
}
|
||||
if (request.method !== 'GET' || request.url !== requestPath || served) {
|
||||
response.writeHead(404, { 'access-control-allow-origin': '*' });
|
||||
response.end();
|
||||
return;
|
||||
}
|
||||
served = true;
|
||||
let size = 0;
|
||||
try {
|
||||
size = fs.statSync(zipPath).size;
|
||||
} catch {
|
||||
response.writeHead(500, { 'access-control-allow-origin': '*' });
|
||||
response.end();
|
||||
return;
|
||||
}
|
||||
response.writeHead(200, {
|
||||
'access-control-allow-origin': '*',
|
||||
'content-length': String(size),
|
||||
'content-type': 'application/zip',
|
||||
});
|
||||
const stream = fs.createReadStream(zipPath);
|
||||
stream.on('error', () => {
|
||||
if (!response.headersSent) {
|
||||
response.writeHead(500, { 'access-control-allow-origin': '*' });
|
||||
response.end();
|
||||
return;
|
||||
}
|
||||
response.destroy();
|
||||
});
|
||||
stream.pipe(response);
|
||||
});
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
server.once('error', reject);
|
||||
server.listen(0, '127.0.0.1', () => resolve());
|
||||
});
|
||||
|
||||
try {
|
||||
const address = server.address();
|
||||
if (!address || typeof address === 'string') {
|
||||
throw new Error('Dictionary import server did not bind to a TCP port.');
|
||||
}
|
||||
return await callback(`http://127.0.0.1:${address.port}${requestPath}`);
|
||||
} finally {
|
||||
await new Promise<void>((resolve) => server.close(() => resolve()));
|
||||
}
|
||||
}
|
||||
|
||||
const YOMITAN_SCANNING_HELPERS = String.raw`
|
||||
const HIRAGANA_CONVERSION_RANGE = [0x3041, 0x3096];
|
||||
const KATAKANA_CONVERSION_RANGE = [0x30a1, 0x30f6];
|
||||
@@ -1863,17 +1928,43 @@ export async function importYomitanDictionaryFromZip(
|
||||
return false;
|
||||
}
|
||||
|
||||
const archiveBase64 = fs.readFileSync(normalizedZipPath).toString('base64');
|
||||
const script = `
|
||||
(async () => {
|
||||
await globalThis.__subminerYomitanSettingsAutomation.importDictionaryArchiveBase64(
|
||||
${JSON.stringify(archiveBase64)},
|
||||
${JSON.stringify(path.basename(normalizedZipPath))}
|
||||
);
|
||||
return true;
|
||||
})();
|
||||
`;
|
||||
const result = await invokeYomitanSettingsAutomation<boolean>(script, deps, logger);
|
||||
const supportsUrlImport = await invokeYomitanSettingsAutomation<boolean>(
|
||||
`
|
||||
(() => typeof globalThis.__subminerYomitanSettingsAutomation.importDictionaryArchiveUrl === "function")();
|
||||
`,
|
||||
deps,
|
||||
logger,
|
||||
);
|
||||
|
||||
const result =
|
||||
supportsUrlImport === true
|
||||
? await serveDictionaryZipOnce(normalizedZipPath, async (archiveUrl) =>
|
||||
invokeYomitanSettingsAutomation<boolean>(
|
||||
`
|
||||
(async () => {
|
||||
await globalThis.__subminerYomitanSettingsAutomation.importDictionaryArchiveUrl(
|
||||
${JSON.stringify(archiveUrl)}
|
||||
);
|
||||
return true;
|
||||
})();
|
||||
`,
|
||||
deps,
|
||||
logger,
|
||||
),
|
||||
)
|
||||
: await invokeYomitanSettingsAutomation<boolean>(
|
||||
`
|
||||
(async () => {
|
||||
await globalThis.__subminerYomitanSettingsAutomation.importDictionaryArchiveBase64(
|
||||
${JSON.stringify(fs.readFileSync(normalizedZipPath).toString('base64'))},
|
||||
${JSON.stringify(path.basename(normalizedZipPath))}
|
||||
);
|
||||
return true;
|
||||
})();
|
||||
`,
|
||||
deps,
|
||||
logger,
|
||||
);
|
||||
return result === true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user