Skip to content

Commit 2173bdd

Browse files
committed
删去不使用的 listenPageMessages, 修正 CAT_fetchDocument
1 parent a18b6d1 commit 2173bdd

File tree

3 files changed

+8
-24
lines changed

3 files changed

+8
-24
lines changed

src/app/service/content/gm_api/gm_api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ export default class GMApi extends GM_Base {
481481

482482
@GMContext.API()
483483
public async CAT_fetchDocument(url: string): Promise<Document | undefined> {
484-
return urlToDocumentInContentPage(this, url);
484+
return urlToDocumentInContentPage(this, url, isContent);
485485
}
486486

487487
static _GM_cookie(

src/app/service/content/gm_api/gm_xhr.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ export const convObjectToURL = async (object: string | URL | Blob | File | undef
9494
return url;
9595
};
9696

97-
export const urlToDocumentInContentPage = async (a: GMApi, url: string) => {
97+
export const urlToDocumentInContentPage = async (a: GMApi, url: string, isContent: boolean) => {
9898
// url (e.g. blob url) -> XMLHttpRequest (CONTENT) -> Document (CONTENT)
99-
const nodeId = await a.sendMessage("CAT_fetchDocument", [url]);
99+
const nodeId = await a.sendMessage("CAT_fetchDocument", [url, isContent]);
100100
return (<CustomEventMessage>a.message).getAndDelRelatedTarget(nodeId) as Document;
101101
};
102102

src/scripting.ts

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,6 @@ const setupDeliveryChannel = () => {
6565
messageDeliveryToPage.setup(`evt_${token}_deliveryMessage`);
6666
};
6767

68-
// ================================
69-
// 页面消息监听(调试用)
70-
// ================================
71-
72-
// 页面消息监听(TBC)
73-
const listenPageMessages = () => {
74-
const token = requireScriptingToken();
75-
pageAddEventListener(`evt_${token}_listen_page`, (ev) => {
76-
if (!(ev instanceof CustomEvent)) return;
77-
const { tag, value, from } = ev.detail;
78-
// 仅打印 (TBC)
79-
console.log(tag, value, from);
80-
});
81-
};
82-
8368
// ================================
8469
// Server 构建与 service_worker 转发
8570
// ================================
@@ -102,13 +87,15 @@ const handleRuntimeGmApi = (
10287
return fetch(data.params[0]).then((res) => res.blob());
10388
}
10489
case "CAT_fetchDocument": {
90+
const [url, isContent] = data.params;
10591
return new Promise((resolve) => {
10692
const xhr = new XMLHttpRequest();
10793
xhr.responseType = "document";
108-
xhr.open("GET", data.params[0]);
94+
xhr.open("GET", url);
10995
xhr.onload = () => {
110-
// TBC
111-
const nodeId = (<CustomEventMessage>senderToInject).sendRelatedTarget(xhr.response);
96+
// 根据来源选择不同的消息桥(content / inject)
97+
const msg = isContent ? senderToContent : senderToInject;
98+
const nodeId = msg.sendRelatedTarget(xhr.response);
11299
resolve(nodeId);
113100
};
114101
xhr.send();
@@ -221,9 +208,6 @@ const onMessageFlagReceived = (MessageFlag: string) => {
221208
const server = new Server("scripting", [scriptExecutorMsgTxIT, scriptExecutorMsgTxCT]);
222209
prepareServer(server, senderToExt, scriptExecutorMsgTxIT, scriptExecutorMsgTxCT);
223210

224-
// 页面消息监听(TBC)
225-
listenPageMessages();
226-
227211
// 建立向页面投递消息的 delivery 通道
228212
setupDeliveryChannel();
229213
}

0 commit comments

Comments
 (0)