forked from drphe/CORS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
301 lines (274 loc) · 9.07 KB
/
background.js
File metadata and controls
301 lines (274 loc) · 9.07 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
var pref = {
'shortcutToggle': false,
'cssToggle': true,
'allowCopy':false,
'version': chrome.runtime.getManifest().version
};
class Extension {
// Biến để lưu trữ ID của tab chụp ảnh màn hình
screenshotTabId = null;
constructor() {
this.initMessageListeners();
}
// Khởi tạo các listeners để nhận message từ các script khác
initMessageListeners() {
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
switch (request.method) {
case "take_screen_shot":
this.screenShot(sendResponse);
break;
case "get_pixel_color":
this.getPixelColor(request.point, sendResponse);
break;
case "save_data":
this.saveData(request.config);
break;
case "get_data":
this.getData(sendResponse);
break;
case "open_options":
chrome.runtime.openOptionsPage();
break;
}
return true; // Để callback không bị garbage collected
});
}
// Lấy màu pixel tại một điểm cụ thể trên trang
async getPixelColor(point, sendResponse) {
try {
const imageDataUrl = await chrome.tabs.captureVisibleTab();
const canvas = new OffscreenCanvas(1, 1);
const ctx = canvas.getContext("2d");
const img = new Image();
img.onload = () => {
canvas.width = img.naturalWidth;
canvas.height = img.naturalHeight;
ctx.drawImage(img, 0, 0);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const pixelIndex = 4 * (point.y * imageData.width + point.x);
const color = {
r: imageData.data[pixelIndex],
g: imageData.data[pixelIndex + 1],
b: imageData.data[pixelIndex + 2],
a: imageData.data[pixelIndex + 3],
};
sendResponse(color);
};
img.src = imageDataUrl;
} catch (error) {
console.error("Lỗi khi lấy màu pixel:", error);
sendResponse(null);
}
}
// Lưu dữ liệu vào local storage
async saveData(config) {
await chrome.storage.local.set({
config
});
}
// Lấy dữ liệu từ local storage
async getData(sendResponse) {
const data = await chrome.storage.local.get("config");
sendResponse(data.config || null);
}
// Chèn CSS và JavaScript vào tab hiện tại
async inject() {
const [activeTab] = await chrome.tabs.query({
active: true,
currentWindow: true,
});
if (activeTab) {
try {
await chrome.scripting.insertCSS({
target: {
tabId: activeTab.id
},
files: ["drawtool/panelTools.css"],
});
await chrome.scripting.executeScript({
target: {
tabId: activeTab.id
},
files: ["drawtool/panelTools.js"],
});
} catch (error) {
console.table("Lỗi khi chèn script:", error);
}
}
}
// Chụp ảnh màn hình
async screenShot(sendResponse) {
const screenshotDataUrl = await chrome.tabs.captureVisibleTab();
let screenshotTab = null;
if (this.screenshotTabId) {
screenshotTab = await chrome.tabs
.get(this.screenshotTabId)
.catch(() => null);
}
if (screenshotTab) {
await chrome.tabs.update(screenshotTab.id, {
active: true
});
this.updateScreenshot(screenshotDataUrl, sendResponse, 0, screenshotTab);
} else {
const newTab = await chrome.tabs.create({
url: "drawtool/editor.html"
});
this.screenshotTabId = newTab.id;
this.updateScreenshot(screenshotDataUrl, sendResponse, 0, newTab);
}
}
// Gửi URL ảnh màn hình đến tab chỉnh sửa
updateScreenshot(
screenshotDataUrl,
sendResponse,
retries = 0,
editorTab
) {
if (retries > 10) return;
chrome.runtime.sendMessage({
method: "update_url",
url: screenshotDataUrl
},
(response) => {
if (response && response.success) {
sendResponse({
success: true
});
} else {
setTimeout(() => {
this.updateScreenshot(
screenshotDataUrl,
sendResponse,
retries + 1,
editorTab
);
}, 300);
}
}
);
}
// Cập nhật pop-up của extension
async setWarning(popupUrl) {
await chrome.action.setPopup({
popup: popupUrl
});
}
}
// Khởi tạo class ngay khi extension được tải
const drawtool = new Extension();
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.local.get(Object.keys(pref)).then(toggle => {
for (let key in toggle) {
if (key in pref) pref[key] = toggle[key];
}
chrome.storage.local.set(pref);
});
// menu mở Wichart
chrome.contextMenus.create({
id: "wichart",
title: "Mở Wichart",
contexts: ["page", "selection", "link", "image"],
documentUrlPatterns: ["*://*.drphe.github.io/BM/*"]
});
// menu mở Wichart
chrome.contextMenus.create({
id: "repo",
title: "Tải repo editor",
contexts: ["page", "selection", "link", "image"],
documentUrlPatterns: ["*://*.drphe.github.io/KhoIPA/*"]
});
// Đăng nhập tài khoản Vieon VIP
chrome.contextMenus.create({
id: "loadVieonAccounts",
title: "Nạp tài khoản Vieon",
contexts: ["page"],
documentUrlPatterns: ["*://*.vieon.vn/*"]
});
// vẽ trên trang
chrome.contextMenus.create({
id: "draw",
title: "🎨 Công cụ vẽ",
contexts: ["action"]
});
// bảng gõ tắt
chrome.contextMenus.create({
id: "banggotat",
title: "✏️ Soạn bảng gõ tắt...",
contexts: ["action"]
});
// css
chrome.contextMenus.create({
id: "css",
title: "✨ Giao diện tùy chỉnh CSS...",
contexts: ["action"]
});
chrome.contextMenus.create({
title: pref.allowCopy ? "✅ Đã bật SupperCopy" : "❌ Không dùng SupperCopy",
id: 'allowCopy',
contexts: ["action"]
})
// Hướng dẫn sử dụng
chrome.contextMenus.create({
id: "open-guide",
title: "📄 Hướng dẫn sử dụng",
contexts: ["action"] // Hiển thị khi nhấp chuột phải vào biểu tượng extension
});
});
// Truy cập trang web tài chính
chrome.action.onClicked.addListener(function(tab) {
const url = "https://drphe.github.io/BM/vnindex";
chrome.tabs.create({
url: url
});
});
// Lắng nghe sự kiện click vào nút menu context
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
if (info.menuItemId === "loadVieonAccounts") {
// Mở popup.html trong một cửa sổ mới
chrome.windows.create({
url: 'vieon_helper/popup.html',
type: 'popup',
width: 500,
height: 650
});
}
if (info.menuItemId === 'allowCopy') {
pref.allowCopy = !pref.allowCopy
await chrome.storage.local.set(pref);
await chrome.contextMenus.update("allowCopy", {
title: pref.allowCopy ? "✅ Đã bật SupperCopy" : "❌ Không dùng SupperCopy",
});
}
if (info.menuItemId === "wichart") {
const wichartUrl = "https://wichart.vn";
// Mở một tab mới với URL đã chỉ định.
chrome.tabs.create({
url: wichartUrl
});
}
if (info.menuItemId === "repo") {
const khoipatUrl = "khoipa/index.html";
chrome.tabs.create({
url: khoipatUrl
});
}
if (info.menuItemId === "open-guide") {
chrome.tabs.create({
url: "https://drphe.github.io/BM/hdsd.html"
});
}
if (info.menuItemId === "banggotat") {
chrome.tabs.create({
url: "shortcut/dashboard.html"
});
}
if (info.menuItemId === "css") {
chrome.tabs.create({
url: "morecss/dashboard.html"
});
}
if (info.menuItemId === "draw") {
drawtool.inject();
}
});