-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbackground.js
More file actions
216 lines (189 loc) · 6.63 KB
/
background.js
File metadata and controls
216 lines (189 loc) · 6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
const detect_server = "http://8.138.222.184:8201/upload"
const result_map = new Map(); // 存储后端返回结果
const wasm_map = new Map(); // 存储注入脚本发送的wasm
const host_set = new Set(); // 检测有多少个源
let vue_ready = false;
// Background Script
// // host: 发送这个文件的url
// function wasmFound(data, host) {
// // 用Web Crypto API 生成哈希作为filename
// crypto.subtle.digest('SHA-256', new TextEncoder().encode(data)).then(async (hashBuffer) => {
// const hashArray = Array.from(new Uint8Array(hashBuffer));
// const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
// if (!wasm_map.has(host)) {
// wasm_map.set(host, []);
// }
// if (!host_set.has(host)) {
// host_set.add(host)
// }
// let arr = wasm_map.get(host);
// let wasm_obj = {
// filename: hashHex,
// content: data
// }
// arr.push(wasm_obj);
// });
// }
function wasmFound(data, host, originalFilename) {
crypto.subtle.digest('SHA-256', new TextEncoder().encode(data)).then(async (hashBuffer) => {
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
if (!wasm_map.has(host)) {
wasm_map.set(host, []);
}
if (!host_set.has(host)) {
host_set.add(host)
}
let arr = wasm_map.get(host);
let wasm_obj = {
filename: hashHex,
originalFilename: originalFilename, // 保存原始文件名
content: data
}
arr.push(wasm_obj);
});
}
function detect_wasm(host) {
let data_send = {
files: wasm_map.get(host),
host: host
}
console.log(data_send)
fetch(detect_server, {
method: "POST",
headers: {
"Content-Type": "application/json"
// 如果有需要的话,可以添加其他的请求头,比如认证令牌
// "Authorization": "Bearer YOUR_TOKEN_HERE"
},
body: JSON.stringify(data_send) // 将JavaScript对象转换为JSON字符串
})
.then(response => {
if (!response.ok) {
// 网络响应状态码不为2xx的情况
throw new Error(response);
}
return response.json(); // 解析JSON格式的响应数据
})
.then(data => {
console.log("数据成功发送并接收到响应:", data);
let temp = [];
data.predictions.forEach((val) => {
const key = Object.keys(val)[0];
let obj = {
filename: key,
res: val[key],
note: val['note'],
host: host
}
temp.push(obj);
});
result_map.set(host, temp.slice());
console.log(result_map)
//send_detect_res(host)
host_set.size === result_map.size && send_detect_res()
// 在这里处理服务器返回的数据
})
.catch(error => {
console.error("发送数据过程中出现问题:", error.message);
})
.finally(() => {
wasm_map.set(host, []);
})
;
}
function send_detect_res() {
console.log("向vue页面发送数据")
let data_arr = [];
result_map.forEach((val, key) => {
data_arr.push(...val);
})
const data_send = {
data: data_arr,
type: "data_to_show"
}
//chrome.runtime.sendMessage(data_send);
chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
var activeTab = tabs[0];
chrome.tabs.sendMessage(activeTab.id, data_send);
});
}
function download_wasm(base64Data, filename) {
// 创建本地url
const dataUrl = 'data:application/wasm;base64,' + base64Data;
chrome.downloads.download({
url: dataUrl,
filename: filename,
saveAs: true
}, function (downloadId) {
if (chrome.runtime.lastError) {
console.error('下载失败:', chrome.runtime.lastError);
} else {
console.log('文件开始下载, 下载ID:', downloadId);
}
});
}
// 注意:这里假设数据已经是base64编码的字符串
// 监听注入的消息
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (changeInfo.status === 'complete' && tab.target) {
// 适用于您想要的特定URLs
console.log("注入成功")
}
});
chrome.runtime.onMessage.addListener(async function (message, sender, sendResponse) {
console.log("Message received:", message);
// 通信测试
if (message.greeting === "hello") {
// 发送响应到content script
sendResponse({farewell: "goodbye"});
}
if (message.type === "FROM_INJECTED_SCRIPT") {
console.log("Message from injected script:", message.text);
// 根据需要处理消息
}
// if (message.type === "WASM_FOUND") {
// const data = message.data; // Base64编码的WASM数据
// const host = message.host;
// // 在这里实现wasmFound的逻辑
// try {
// await wasmFound(data, host)
// // 这里应该发生个信息回去
// console.log('存储成功!')
// } catch {
// console.log('失败')
// }
// }
if (message.type === "WASM_FOUND") {
const data = message.data; // Base64编码的WASM数据
const host = message.host;
const originalFilename = message.originalFilename; // 假设消息中包含原始文件名
try {
await wasmFound(data, host, originalFilename);
console.log('存储成功!')
} catch {
console.log('失败')
}
}
if (message.type === "WASM_COMPLETE") {
// 所有wasm发送完毕
console.log(`${message.host}总共有${message.count}个wasm`)
console.log(wasm_map)
detect_wasm(message.host)
//console.log(result_map)
}
if (message.type === "vue_page_mounted") {
console.log("vue页面已挂载")
//wasm_map.size && send_detect_res(wasm_map)
vue_ready = true;
}
if (message.action === 'closeTab') {
console.log("快关闭网页!")
chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
chrome.tabs.remove(tabs[0].id);
});
}
// 如果你需要异步响应,返回true
// 这会保持消息通道开放,直到sendResponse被调用
return true;
});