Skip to content

Commit 0b2468e

Browse files
committed
Enhance message handling and priority management in content scripts
- Updated the message structure in index.ts to include additional properties: nodeName, priority, and tick. - Introduced a new utility function getPriority to determine the priority of HTML elements based on their node names. - Modified handleNode to utilize the new priority system and increment tick for each processed node. - Added a new file getPriority.ts to manage priority mappings for various HTML elements.
1 parent ff0a66a commit 0b2468e

File tree

6 files changed

+54
-14
lines changed

6 files changed

+54
-14
lines changed

chrome-extension/src/background/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const listenMessageForUI = (
5656

5757
switch (func) {
5858
case 'QueryRequest': {
59-
const { source, logic, url } = message.body;
59+
const { source, logic, url, nodeName, priority, tick } = message.body;
6060
const tabId = sender.tab?.id;
6161
const v = waitingQuery[source];
6262
if (v === undefined) {
@@ -68,7 +68,8 @@ const listenMessageForUI = (
6868
v.resolves.push(sendResponse);
6969
}
7070
}
71-
ws?.send(JSON.stringify({ source, logic, url, tabId }));
71+
const data = { source, logic, url, tabId, nodeName, priority, tick };
72+
ws?.send(JSON.stringify(data));
7273
return true;
7374
}
7475
case 'GetState': {
@@ -88,6 +89,15 @@ const listenMessageForUI = (
8889
syncStateToContent();
8990
return false;
9091
}
92+
case 'QueryResponse': {
93+
break;
94+
}
95+
case 'OnStateChanged': {
96+
break;
97+
}
98+
case 'GetStateResponse': {
99+
break;
100+
}
91101
}
92102
return false;
93103
} catch (e) {

docs/TODO.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
- [ ] Bundle chrome extension files
88
- [ ] 使用教程
99
- [ ] 演示模式
10-
- [ ] 坑爹的隐藏 Form, 给老子滚
1110
- [x] 确保转圈会消失
1211
- [x] 如果没有运行, 就移除所有转圈
1312
- [x] 移除红框
@@ -17,7 +16,6 @@
1716

1817
### 中优先级
1918

20-
- [ ] 优先翻译 main 标签, See: [通常有哪些情况表明某些内容是网页的主要元素?](https://chatgpt.com/share/68740231-3100-8004-973e-b850038a27b7)
2119
- [ ] Store things in frontend
2220
- [ ] 使用最新权重
2321
- [ ] 固定不翻译一些内容
@@ -26,7 +24,9 @@
2624
- [ ] 不翻译已经关闭的标签
2725
- [ ] 渲染 frontend 在线状态
2826
- [ ] 根据 backend 在线状态决定是否启用翻译
29-
- [x] 不在某些 URL 上执行:
27+
- [x] 不在某些 URL 上执行
28+
- [x] 优先翻译 main 标签, See: [通常有哪些情况表明某些内容是网页的主要元素?](https://chatgpt.com/share/68740231-3100-8004-973e-b850038a27b7)
29+
- [x] 坑爹的隐藏 Form, 给老子滚
3030

3131
### 低优先级
3232

packages/shared/lib/utils/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ export interface QueryRequest {
1818
source: string;
1919
logic: 'translate' | 'loop';
2020
url: string;
21+
nodeName: string;
22+
priority: number;
23+
tick: number;
2124
};
2225
}
2326

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const priorityMap: { [key: string]: number } = {
2+
'turbo-frame': 20,
3+
article: 40,
4+
main: 30,
5+
section: 10,
6+
p: 10,
7+
h1: 10,
8+
h2: 10,
9+
header: -10,
10+
footer: -10,
11+
comment: -20,
12+
nav: -20,
13+
sidebar: -20,
14+
menu: -20,
15+
options: -20,
16+
cite: -11,
17+
};
18+
19+
export const getPriority = (node: HTMLElement) => {
20+
const nodeName = node.nodeName;
21+
return priorityMap[nodeName] ?? priorityMap[nodeName.toLowerCase()] ?? 0;
22+
};

pages/content/src/matches/all/handleNode.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { checkBreakLineHappened } from '.';
2+
import { getPriority } from './getPriority';
23
import { state } from './state';
34
import { ignoreHref, isChinese, isUrl, queryWS, formatTranslation } from '@extension/shared';
45

56
const ignoreTypeLower = ['path', 'script', 'style', 'svg', 'noscript', 'head', 'pre', 'code', 'math', 'textarea'];
67
const ignoreTypeUpper = ignoreTypeLower.map(item => item.toUpperCase());
78
const ignoreTypes = ignoreTypeLower.concat(ignoreTypeUpper);
8-
// const checkingTypeLower = ['turbo-frame', 'article'];
9-
const checkingTypeLower: string[] = [];
9+
const checkingTypeLower = ['turbo-frame', 'article', 'main'];
10+
// const checkingTypeLower: string[] = [];
1011
const checkingTypeUpper = checkingTypeLower.map(item => item.toUpperCase());
1112
const checkingTypes = checkingTypeLower.concat(checkingTypeUpper);
13+
let tick = 0;
1214

1315
export const handleNode = (_node: Node): boolean => {
1416
const currentUrl = window.location.href;
@@ -17,13 +19,16 @@ export const handleNode = (_node: Node): boolean => {
1719
if (startWith) return false;
1820
}
1921

20-
if (state.running) return false;
21-
2222
const node = _node as HTMLElement;
2323
const nodeName = node.nodeName;
2424

2525
const checking = checkingTypes.includes(nodeName);
2626

27+
if (!state.running) {
28+
// if (checking) console.log({ nodeName, running: state.running });
29+
return false;
30+
}
31+
2732
// if (checking) console.log({ nodeName, step: 0 });
2833

2934
const parentNode = node.parentElement;
@@ -160,7 +165,9 @@ export const handleNode = (_node: Node): boolean => {
160165
const nodeToBeAdded = document.createElement(nodeNameToBeAdded);
161166
nodeToBeAdded.classList.add('rwkv_offline_translation_result');
162167
if (state.inspecting) nodeToBeAdded.classList.add('rwkv_inspecting');
163-
queryWS({ source: textContent, logic: 'translate', url: currentUrl })
168+
const priority = getPriority(node);
169+
tick++;
170+
queryWS({ source: textContent, logic: 'translate', url: currentUrl, nodeName, priority, tick })
164171
.then(json => {
165172
if (node.classList.contains('rwkv_offline_translation_done')) return;
166173

pages/content/src/matches/all/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { injectCss } from './injectcss';
33
import { state } from './state';
44
import { ignoreHref } from '@extension/shared';
55
import { sampleFunction } from '@src/sample-function';
6-
import { run } from 'node:test';
76

87
injectCss();
98

@@ -58,6 +57,8 @@ const handleStateChanged = (event: CustomEvent) => {
5857
document.body.querySelectorAll('.rwkv_loading_spinner').forEach(node => {
5958
node.remove();
6059
});
60+
} else {
61+
document.body.querySelectorAll('*').forEach(handleNode);
6162
}
6263
}
6364
};
@@ -211,6 +212,3 @@ observer.observe(document.body, {
211212
childList: true,
212213
subtree: true,
213214
});
214-
215-
// 初始遍历已有 DOM
216-
document.body.querySelectorAll('*').forEach(handleNode);

0 commit comments

Comments
 (0)