sonygod commited on
Commit
4b11939
·
1 Parent(s): a86c660

修复序列

Browse files
Files changed (1) hide show
  1. youtube_sub.js +50 -31
youtube_sub.js CHANGED
@@ -1190,20 +1190,33 @@ GM_addStyle(`
1190
 
1191
  async translateInChunks(subtitleData) {
1192
 
 
1193
  // Get video URL as cache key
1194
  const videoUrl = window.location.href;
1195
  const cacheKey = `translations_${videoUrl}`;
1196
  const CACHE_EXPIRY = 365 * 24 * 60 * 60 * 1000; // 7 days
1197
-
1198
-
1199
- // Try to get from cache
 
 
 
 
1200
  try {
1201
  const cached = localStorage.getItem(cacheKey);
1202
  if (cached) {
1203
- const { translations, timestamp } = JSON.parse(cached);
1204
  if (Date.now() - timestamp < CACHE_EXPIRY) {
1205
- debug.log('Using cached translations');
1206
- return translations;
 
 
 
 
 
 
 
 
1207
  } else {
1208
  localStorage.removeItem(cacheKey);
1209
  }
@@ -1211,19 +1224,18 @@ GM_addStyle(`
1211
  } catch (e) {
1212
  debug.error('Cache read error:', e);
1213
  }
1214
- const CHUNK_SIZE = 100;
1215
- const chunks = [];
1216
- const translations = [];
1217
- const DELAY_BETWEEN_CHUNKS = 1000; // 1 second delay
1218
 
1219
- // Split into chunks
1220
- for (let i = 0; i < subtitleData.length; i += CHUNK_SIZE) {
1221
  chunks.push(subtitleData.slice(i, i + CHUNK_SIZE));
1222
  }
1223
 
1224
  // Process each chunk
1225
  for (let i = 0; i < chunks.length; i++) {
1226
- this.updateTabText('chinese', `翻译中...(${i + 1}/${chunks.length})`);
 
1227
 
1228
  if (i > 0) {
1229
  await sleep(DELAY_BETWEEN_CHUNKS);
@@ -1282,13 +1294,15 @@ GM_addStyle(`
1282
  try {
1283
  localStorage.setItem(cacheKey, JSON.stringify({
1284
  translations,
1285
- timestamp: Date.now()
 
 
1286
  }));
1287
- debug.log('Translations cached');
1288
  } catch (e) {
1289
  debug.error('Cache write error:', e);
1290
  }
1291
-
1292
 
1293
  } catch (error) {
1294
  debug.error(`Chunk ${i + 1} translation failed:`, error);
@@ -1341,28 +1355,33 @@ GM_addStyle(`
1341
 
1342
  const videoUrl = window.location.href;
1343
  const cacheKey = `translations_${videoUrl}`;
1344
-
1345
  // Try cache first
1346
  try {
1347
  const cached = localStorage.getItem(cacheKey);
1348
  if (cached) {
1349
- const { translations, timestamp } = JSON.parse(cached);
1350
  const CACHE_EXPIRY = 365 * 24 * 60 * 60 * 1000; // 365 days
1351
-
1352
  if (Date.now() - timestamp < CACHE_EXPIRY) {
1353
- debug.log('Using cached translations');
1354
- this.subtitles.chinese = this.subtitles.english.map((sub, index) => ({
1355
- startTime: sub.startTime,
1356
- endTime: sub.endTime,
1357
- words: [{
1358
- text: translations[index]?.text || '',
1359
  startTime: sub.startTime,
1360
- endTime: sub.endTime
1361
- }]
1362
- }));
1363
- this.updateTabText('chinese', '中文');
1364
- return;
1365
- } else {
 
 
 
 
 
 
 
 
1366
  localStorage.removeItem(cacheKey);
1367
  }
1368
  }
 
1190
 
1191
  async translateInChunks(subtitleData) {
1192
 
1193
+ debugger;
1194
  // Get video URL as cache key
1195
  const videoUrl = window.location.href;
1196
  const cacheKey = `translations_${videoUrl}`;
1197
  const CACHE_EXPIRY = 365 * 24 * 60 * 60 * 1000; // 7 days
1198
+ const CHUNK_SIZE = 100;
1199
+ const chunks = [];
1200
+ let translations = [];
1201
+ const DELAY_BETWEEN_CHUNKS = 1000; // 1 second delay
1202
+ let lastChunkIndex = 0;
1203
+ const totalChunks = Math.ceil(subtitleData.length / CHUNK_SIZE);
1204
+ // Try to get cached progress
1205
  try {
1206
  const cached = localStorage.getItem(cacheKey);
1207
  if (cached) {
1208
+ const { translations: cachedTranslations, timestamp, lastProcessedChunk } = JSON.parse(cached);
1209
  if (Date.now() - timestamp < CACHE_EXPIRY) {
1210
+ if (lastProcessedChunk === totalChunks - 1) {
1211
+ // Translation was complete
1212
+ debug.log('Using complete cached translations');
1213
+ return cachedTranslations;
1214
+ } else {
1215
+ // Resume from last successful chunk
1216
+ debug.log(`Resuming from chunk ${lastProcessedChunk + 1}`);
1217
+ translations = [...cachedTranslations];
1218
+ lastChunkIndex = lastProcessedChunk + 1;
1219
+ }
1220
  } else {
1221
  localStorage.removeItem(cacheKey);
1222
  }
 
1224
  } catch (e) {
1225
  debug.error('Cache read error:', e);
1226
  }
1227
+
1228
+
1229
+ // Split remaining data into chunks
 
1230
 
1231
+ for (let i = lastChunkIndex * CHUNK_SIZE; i < subtitleData.length; i += CHUNK_SIZE) {
 
1232
  chunks.push(subtitleData.slice(i, i + CHUNK_SIZE));
1233
  }
1234
 
1235
  // Process each chunk
1236
  for (let i = 0; i < chunks.length; i++) {
1237
+ const currentChunkIndex = lastChunkIndex + i;
1238
+ this.updateTabText('chinese', `翻译中...(${currentChunkIndex + 1}/${totalChunks})`);
1239
 
1240
  if (i > 0) {
1241
  await sleep(DELAY_BETWEEN_CHUNKS);
 
1294
  try {
1295
  localStorage.setItem(cacheKey, JSON.stringify({
1296
  translations,
1297
+ timestamp: Date.now(),
1298
+ lastProcessedChunk: currentChunkIndex, // Save current chunk index
1299
+ total: totalChunks
1300
  }));
1301
+ debug.log(`Saved progress: ${currentChunkIndex + 1}/${totalChunks}`);
1302
  } catch (e) {
1303
  debug.error('Cache write error:', e);
1304
  }
1305
+
1306
 
1307
  } catch (error) {
1308
  debug.error(`Chunk ${i + 1} translation failed:`, error);
 
1355
 
1356
  const videoUrl = window.location.href;
1357
  const cacheKey = `translations_${videoUrl}`;
1358
+
1359
  // Try cache first
1360
  try {
1361
  const cached = localStorage.getItem(cacheKey);
1362
  if (cached) {
1363
+ const { translations, timestamp, lastProcessedChunk, total } = JSON.parse(cached);
1364
  const CACHE_EXPIRY = 365 * 24 * 60 * 60 * 1000; // 365 days
1365
+
1366
  if (Date.now() - timestamp < CACHE_EXPIRY) {
1367
+ if (lastProcessedChunk === total - 1) {
1368
+ debug.log('Using cached translations');
1369
+ this.subtitles.chinese = this.subtitles.english.map((sub, index) => ({
 
 
 
1370
  startTime: sub.startTime,
1371
+ endTime: sub.endTime,
1372
+ words: [{
1373
+ text: translations[index]?.text || '',
1374
+ startTime: sub.startTime,
1375
+ endTime: sub.endTime
1376
+ }]
1377
+ }));
1378
+ this.updateTabText('chinese', '中文');
1379
+ return;
1380
+ } else {
1381
+ debug.log(`Cached translation incomplete (${lastProcessedChunk + 1}/${total}), resuming...`);
1382
+ }
1383
+ }
1384
+ else {
1385
  localStorage.removeItem(cacheKey);
1386
  }
1387
  }