vg-coder-cli 2.0.32 → 2.0.34
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/.vgignore +0 -10
- package/ARCHITECTURE.md +0 -255
- package/change.sh +0 -0
- package/gulpfile.js +0 -111
- package/vetgo-auto/README.md +0 -3
- package/vetgo-auto/chrome/CSP_IMPROVEMENTS.md +0 -147
- package/vetgo-auto/chrome/MANIFEST_V3_MIGRATION.md +0 -123
- package/vetgo-auto/chrome/assets/icon128.png +0 -0
- package/vetgo-auto/chrome/assets/icon16.png +0 -0
- package/vetgo-auto/chrome/assets/icon48.png +0 -0
- package/vetgo-auto/chrome/environments/environment.ts +0 -13
- package/vetgo-auto/chrome/manifest.json +0 -66
- package/vetgo-auto/chrome/rules.json +0 -23
- package/vetgo-auto/chrome/src/background.ts +0 -200
- package/vetgo-auto/chrome/src/controller.ts +0 -172
- package/vetgo-auto/chrome/src/controllers/common.firebase.ts +0 -31
- package/vetgo-auto/chrome/src/controllers/firebase-crud.ts +0 -147
- package/vetgo-auto/chrome/src/controllers/load-common-fuc.controller.ts +0 -24
- package/vetgo-auto/chrome/src/controllers/load-script.controller.ts +0 -23
- package/vetgo-auto/chrome/src/script-injector.ts +0 -305
- package/vetgo-auto/chrome/src/sidepanel.css +0 -166
- package/vetgo-auto/chrome/src/sidepanel.html +0 -48
- package/vetgo-auto/chrome/src/sidepanel.ts +0 -127
- package/vetgo-auto/chrome/src/utils/ai-domains.ts +0 -33
- package/vetgo-auto/chrome/src/utils/db-utils.ts +0 -2
- package/vetgo-auto/chrome/src/utils/environment-storage.service.ts +0 -85
- package/vetgo-auto/chrome/src/utils/injector-script.ts +0 -47
- package/vetgo-auto/chrome/webpack.config.js +0 -53
- package/vetgo-auto/chrome/webpack.config.prod.js +0 -54
- package/vetgo-auto/package.json +0 -30
- package/vetgo-auto/tsconfig.json +0 -27
- package/vetgo-auto/vg-coder.zip +0 -0
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"manifest_version": 3,
|
|
3
|
-
"name": "VetGo Pro",
|
|
4
|
-
"short_name": "VetGoPro",
|
|
5
|
-
"version": "1.0.0",
|
|
6
|
-
"description": "VetGo Copyright VetGo ",
|
|
7
|
-
"permissions": [
|
|
8
|
-
"sidePanel",
|
|
9
|
-
"management",
|
|
10
|
-
"webNavigation",
|
|
11
|
-
"notifications",
|
|
12
|
-
"cookies",
|
|
13
|
-
"storage",
|
|
14
|
-
"tabs",
|
|
15
|
-
"nativeMessaging",
|
|
16
|
-
"downloads",
|
|
17
|
-
"scripting",
|
|
18
|
-
"declarativeNetRequest"
|
|
19
|
-
],
|
|
20
|
-
"host_permissions": [
|
|
21
|
-
"<all_urls>"
|
|
22
|
-
],
|
|
23
|
-
"icons": {
|
|
24
|
-
"128": "assets/icon128.png",
|
|
25
|
-
"48": "assets/icon48.png",
|
|
26
|
-
"16": "assets/icon16.png"
|
|
27
|
-
},
|
|
28
|
-
"action": {
|
|
29
|
-
"default_title": "VetGo Pro - Cấu hình",
|
|
30
|
-
"default_icon": {
|
|
31
|
-
"128": "assets/icon128.png",
|
|
32
|
-
"48": "assets/icon48.png",
|
|
33
|
-
"16": "assets/icon16.png"
|
|
34
|
-
}
|
|
35
|
-
},
|
|
36
|
-
"content_scripts": [
|
|
37
|
-
{
|
|
38
|
-
"matches": [
|
|
39
|
-
"<all_urls>"
|
|
40
|
-
],
|
|
41
|
-
"js": [
|
|
42
|
-
"controller.js"
|
|
43
|
-
],
|
|
44
|
-
"all_frames": true,
|
|
45
|
-
"run_at": "document_start"
|
|
46
|
-
}
|
|
47
|
-
],
|
|
48
|
-
"background": {
|
|
49
|
-
"service_worker": "background.js"
|
|
50
|
-
},
|
|
51
|
-
"content_security_policy": {
|
|
52
|
-
"extension_pages": "script-src 'self'; object-src 'self'"
|
|
53
|
-
},
|
|
54
|
-
"side_panel": {
|
|
55
|
-
"default_path": "sidepanel.html"
|
|
56
|
-
},
|
|
57
|
-
"declarative_net_request": {
|
|
58
|
-
"rule_resources": [
|
|
59
|
-
{
|
|
60
|
-
"id": "ruleset_1",
|
|
61
|
-
"enabled": true,
|
|
62
|
-
"path": "rules.json"
|
|
63
|
-
}
|
|
64
|
-
]
|
|
65
|
-
}
|
|
66
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
[
|
|
2
|
-
{
|
|
3
|
-
"id": 1,
|
|
4
|
-
"priority": 1,
|
|
5
|
-
"action": {
|
|
6
|
-
"type": "modifyHeaders",
|
|
7
|
-
"responseHeaders": [
|
|
8
|
-
{
|
|
9
|
-
"header": "content-security-policy",
|
|
10
|
-
"operation": "remove"
|
|
11
|
-
},
|
|
12
|
-
{
|
|
13
|
-
"header": "x-frame-options",
|
|
14
|
-
"operation": "remove"
|
|
15
|
-
}
|
|
16
|
-
]
|
|
17
|
-
},
|
|
18
|
-
"condition": {
|
|
19
|
-
"urlFilter": "*",
|
|
20
|
-
"resourceTypes": ["main_frame", "sub_frame"]
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
]
|
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
import { LoadScriptController } from "./controllers/load-script.controller";
|
|
2
|
-
import { Observable } from "rxjs";
|
|
3
|
-
import { CommonFunc } from "./controllers/load-common-fuc.controller";
|
|
4
|
-
import { uuid } from "./utils/db-utils";
|
|
5
|
-
|
|
6
|
-
// Type declaration for chrome.sidePanel API (not yet in @types/chrome)
|
|
7
|
-
declare global {
|
|
8
|
-
namespace chrome {
|
|
9
|
-
namespace sidePanel {
|
|
10
|
-
function open(options: { tabId?: number; windowId?: number }): Promise<void>;
|
|
11
|
-
function setOptions(options: { tabId?: number; path?: string; enabled?: boolean }): Promise<void>;
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
// Handle extension icon click to open side panel
|
|
17
|
-
chrome.action.onClicked.addListener((tab) => {
|
|
18
|
-
if (tab.id) {
|
|
19
|
-
(chrome as any).sidePanel.open({ tabId: tab.id }).catch((err: Error) => {
|
|
20
|
-
console.error('Failed to open side panel:', err);
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
// Service Worker không hỗ trợ BehaviorSubject như trong background page
|
|
27
|
-
// Sử dụng chrome.storage để lưu trữ state thay thế
|
|
28
|
-
// const $eventOpenZalo = new BehaviorSubject<boolean>(null);
|
|
29
|
-
|
|
30
|
-
// Manifest V3 không còn hỗ trợ webRequest blocking
|
|
31
|
-
// Chuyển sang sử dụng declarativeNetRequest thông qua rules.json
|
|
32
|
-
// Headers sẽ được xử lý bởi declarative rules thay vì code
|
|
33
|
-
|
|
34
|
-
// Manifest V3: Thay thế webRequest.onBeforeRequest bằng webNavigation
|
|
35
|
-
// Vì không thể chặn request, ta sẽ lắng nghe navigation events
|
|
36
|
-
chrome.webNavigation.onCompleted.addListener(async (details) => {
|
|
37
|
-
if (details.frameId === 0) { // Chỉ xử lý main frame
|
|
38
|
-
try {
|
|
39
|
-
const url = new URL(details.url);
|
|
40
|
-
const params = new URLSearchParams(url.search);
|
|
41
|
-
const sheetID = params.get('sheetID');
|
|
42
|
-
const profile = params.get('profile');
|
|
43
|
-
|
|
44
|
-
if (sheetID) {
|
|
45
|
-
console.log('Found navigation with sheetID:', details.url);
|
|
46
|
-
// Manifest V3: Sử dụng chrome.scripting thay vì chrome.tabs.executeScript
|
|
47
|
-
await chrome.scripting.executeScript({
|
|
48
|
-
target: { tabId: details.tabId },
|
|
49
|
-
func: (sheetID) => {
|
|
50
|
-
localStorage.setItem("sheetID", sheetID);
|
|
51
|
-
},
|
|
52
|
-
args: [sheetID]
|
|
53
|
-
});
|
|
54
|
-
console.log('sheetID saved to local storage:', sheetID);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if (profile) {
|
|
58
|
-
console.log('Found navigation with profile:', details.url);
|
|
59
|
-
await chrome.scripting.executeScript({
|
|
60
|
-
target: { tabId: details.tabId },
|
|
61
|
-
func: (profile) => {
|
|
62
|
-
localStorage.setItem("phone", profile);
|
|
63
|
-
},
|
|
64
|
-
args: [profile]
|
|
65
|
-
});
|
|
66
|
-
console.log('profile saved to local storage:', profile);
|
|
67
|
-
}
|
|
68
|
-
} catch (error) {
|
|
69
|
-
console.error('Error processing navigation:', error);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
chrome.webNavigation.onBeforeNavigate.addListener((details) => {
|
|
75
|
-
// Log the URL of the navigation
|
|
76
|
-
console.log('Navigating to:', details.url);
|
|
77
|
-
chrome.tabs.query({ currentWindow: true }, (tabs) => {
|
|
78
|
-
tabs.filter(tab => tab.url && tab.url.includes('id.zalo.me')).forEach((tab) => {
|
|
79
|
-
if(tab.id && tab.id !== details.tabId) {
|
|
80
|
-
chrome.tabs.remove(tab.id);
|
|
81
|
-
}
|
|
82
|
-
})
|
|
83
|
-
})
|
|
84
|
-
}, { url: [{ hostEquals: 'id.zalo.me' }] });
|
|
85
|
-
const getChromeId = ():Observable<string> =>{
|
|
86
|
-
return new Observable((ob) => {
|
|
87
|
-
chrome.storage.sync.get(({id}) => {
|
|
88
|
-
if(!id) {
|
|
89
|
-
const chromeId = uuid();
|
|
90
|
-
chrome.storage.sync.set({id: chromeId});
|
|
91
|
-
ob.next(chromeId);
|
|
92
|
-
} else {
|
|
93
|
-
ob.next(id);
|
|
94
|
-
}
|
|
95
|
-
})
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
chrome.runtime.onInstalled.addListener(() => {
|
|
99
|
-
getChromeId().subscribe((id) => {
|
|
100
|
-
console.log("id",id);
|
|
101
|
-
});
|
|
102
|
-
// chrome.storage.sync.clear(() => console.log("Clear store......"));
|
|
103
|
-
// chrome.cookies.getAll({}, function(cookies) {
|
|
104
|
-
// var json = JSON.stringify(cookies);
|
|
105
|
-
// var blob = new Blob([json], {type: "application/json"});
|
|
106
|
-
// var url = URL.createObjectURL(blob);
|
|
107
|
-
//
|
|
108
|
-
// chrome.downloads.download({
|
|
109
|
-
// url: url,
|
|
110
|
-
// filename: "cookies.json"
|
|
111
|
-
// });
|
|
112
|
-
// });
|
|
113
|
-
});
|
|
114
|
-
// function zaloTab() {
|
|
115
|
-
// chrome.tabs.query({ currentWindow: true }, (tabs) => {
|
|
116
|
-
// const isNewTab = tabs.map(tab => tab.url).some( url => url.includes('chat.zalo.me') || url.includes('id.zalo.me'));
|
|
117
|
-
// if(!isNewTab) {
|
|
118
|
-
// chrome.tabs.create({ url: 'https://chat.zalo.me', active: false });
|
|
119
|
-
// }
|
|
120
|
-
// });
|
|
121
|
-
// }
|
|
122
|
-
// Listen for tab removal
|
|
123
|
-
// chrome.tabs.onActivated.addListener((activeInfo) => {
|
|
124
|
-
// $eventOpenZalo.next(true);
|
|
125
|
-
// });
|
|
126
|
-
// chrome.tabs.onRemoved.addListener((tabId, removeInfo) => {
|
|
127
|
-
// $eventOpenZalo.next(true);
|
|
128
|
-
// });
|
|
129
|
-
chrome.tabs.onCreated.addListener(() => {
|
|
130
|
-
// console.log(JSON.stringify(tab));
|
|
131
|
-
// alert(JSON.stringify(tab))
|
|
132
|
-
});
|
|
133
|
-
chrome.cookies.onChanged.addListener(() => {
|
|
134
|
-
// console.log(JSON.stringify(changeInfo.cookie))
|
|
135
|
-
});
|
|
136
|
-
chrome.runtime.onMessage.addListener((request, sender, respond) => {
|
|
137
|
-
console.log(sender.tab ?
|
|
138
|
-
"from a content script:" + sender.tab.url :
|
|
139
|
-
"from the extension");
|
|
140
|
-
const handler = new Promise((resolve, reject) => {
|
|
141
|
-
if ( request.action === 'REMOVE_EXTENSION' ) {
|
|
142
|
-
// ham nay de tu xoa extension
|
|
143
|
-
chrome.management.uninstallSelf();
|
|
144
|
-
resolve(request);
|
|
145
|
-
return;
|
|
146
|
-
}
|
|
147
|
-
if(request.action === 'SHEET') {
|
|
148
|
-
resolve(request);
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
if (request.action === 'INJECT_SCRIPT') {
|
|
152
|
-
// Inject script using chrome.scripting API to bypass CSP
|
|
153
|
-
if (sender.tab && sender.tab.id) {
|
|
154
|
-
chrome.scripting.executeScript({
|
|
155
|
-
target: { tabId: sender.tab.id },
|
|
156
|
-
world: 'MAIN', // Execute in main world to access page's window object
|
|
157
|
-
func: (scriptCode) => {
|
|
158
|
-
try {
|
|
159
|
-
// Execute script in page context
|
|
160
|
-
const func = new Function(scriptCode);
|
|
161
|
-
func();
|
|
162
|
-
} catch (error) {
|
|
163
|
-
console.error('Script execution error:', error);
|
|
164
|
-
}
|
|
165
|
-
},
|
|
166
|
-
args: [request.script]
|
|
167
|
-
}).then(() => {
|
|
168
|
-
resolve({ success: true });
|
|
169
|
-
}).catch((error) => {
|
|
170
|
-
console.error('Chrome scripting injection failed:', error);
|
|
171
|
-
resolve({ success: false, error: error.message });
|
|
172
|
-
});
|
|
173
|
-
} else {
|
|
174
|
-
resolve({ success: false, error: 'No tab ID available' });
|
|
175
|
-
}
|
|
176
|
-
return;
|
|
177
|
-
}
|
|
178
|
-
if (request.action == 'CONTROLLER') {
|
|
179
|
-
const actionType = request.actionType || "MAIN";
|
|
180
|
-
getChromeId().subscribe((id) => {
|
|
181
|
-
console.log(id);
|
|
182
|
-
CommonFunc.load().then((commonCode)=> {
|
|
183
|
-
LoadScriptController.loadScriptByDomain(request.domain, actionType).then((code) => {
|
|
184
|
-
let script = `
|
|
185
|
-
window.vetgo = {...(window.vetgo || {} ) ,chromeId:"${id}"};
|
|
186
|
-
${commonCode}
|
|
187
|
-
${code}
|
|
188
|
-
`;
|
|
189
|
-
resolve({ script });
|
|
190
|
-
});
|
|
191
|
-
})
|
|
192
|
-
})
|
|
193
|
-
} else {
|
|
194
|
-
reject('//request is empty.');
|
|
195
|
-
}
|
|
196
|
-
});
|
|
197
|
-
handler.then(message => respond(message)).catch(error => respond(error));
|
|
198
|
-
return true;
|
|
199
|
-
|
|
200
|
-
});
|
|
@@ -1,172 +0,0 @@
|
|
|
1
|
-
import { ScriptInjector } from './script-injector';
|
|
2
|
-
import { isAIChatDomain } from './utils/ai-domains';
|
|
3
|
-
import { VG_CODER_INJECTOR_SCRIPT } from './utils/injector-script';
|
|
4
|
-
|
|
5
|
-
const addScript = async (script: string, actionType: string) => {
|
|
6
|
-
try {
|
|
7
|
-
const success = await ScriptInjector.injectScript(script, actionType);
|
|
8
|
-
if (!success) {
|
|
9
|
-
console.error('All script injection methods failed for:', actionType);
|
|
10
|
-
}
|
|
11
|
-
} catch (error) {
|
|
12
|
-
console.error('Script injection error:', error);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
// Wait for DOM to be ready before executing
|
|
16
|
-
const waitForDOM = (): Promise<void> => {
|
|
17
|
-
return new Promise((resolve) => {
|
|
18
|
-
if (document.readyState === 'loading') {
|
|
19
|
-
document.addEventListener('DOMContentLoaded', () => resolve(), { once: true });
|
|
20
|
-
} else {
|
|
21
|
-
resolve();
|
|
22
|
-
}
|
|
23
|
-
});
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Multi-layer detection to check if current page is loaded inside VG Coder's iframe
|
|
28
|
-
* Returns true if this is a nested context (should skip injection)
|
|
29
|
-
*/
|
|
30
|
-
const detectVGCoderContext = async (): Promise<boolean> => {
|
|
31
|
-
// Layer 1: URL parameter check (fastest)
|
|
32
|
-
const urlParams = new URLSearchParams(window.location.search);
|
|
33
|
-
if (urlParams.has('vg_coder_context')) {
|
|
34
|
-
console.log('⚡ VG Coder context detected: URL parameter');
|
|
35
|
-
return true;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// Layer 2: Referrer check (backup)
|
|
39
|
-
const referrer = document.referrer;
|
|
40
|
-
if (referrer && (referrer.includes(':6868') || referrer.includes('vg-coder'))) {
|
|
41
|
-
console.log('⚡ VG Coder context detected: Referrer contains :6868');
|
|
42
|
-
return true;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
// Layer 3: PostMessage ping (most reliable for iframe detection)
|
|
46
|
-
if (window.self !== window.top) {
|
|
47
|
-
try {
|
|
48
|
-
const isVGCoder = await new Promise<boolean>((resolve) => {
|
|
49
|
-
const timeout = setTimeout(() => resolve(false), 200);
|
|
50
|
-
|
|
51
|
-
const handler = (event: MessageEvent) => {
|
|
52
|
-
if (event.data?.type === 'VG_CODER_PARENT') {
|
|
53
|
-
clearTimeout(timeout);
|
|
54
|
-
window.removeEventListener('message', handler);
|
|
55
|
-
resolve(true);
|
|
56
|
-
}
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
window.addEventListener('message', handler);
|
|
60
|
-
window.parent.postMessage({ type: 'VG_CODER_PING' }, '*');
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
if (isVGCoder) {
|
|
64
|
-
console.log('⚡ VG Coder context detected: PostMessage confirmation');
|
|
65
|
-
return true;
|
|
66
|
-
}
|
|
67
|
-
} catch (e) {
|
|
68
|
-
console.log('PostMessage detection failed (expected for cross-origin):', e);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
console.log('✅ Normal context - not inside VG Coder iframe');
|
|
73
|
-
return false;
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
const initializeController = async () => {
|
|
77
|
-
try {
|
|
78
|
-
// Check if we're in VG Coder's iframe context
|
|
79
|
-
const isVGCoderNested = await detectVGCoderContext();
|
|
80
|
-
|
|
81
|
-
if (isVGCoderNested) {
|
|
82
|
-
console.log('🚫 VG Coder nested iframe context detected - skipping all script injections');
|
|
83
|
-
sessionStorage.setItem('VG_CODER_NESTED', 'true');
|
|
84
|
-
return; // Early exit - don't inject any scripts
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
// Ensure DOM is ready
|
|
88
|
-
await waitForDOM();
|
|
89
|
-
|
|
90
|
-
const currentHostname = window.location.hostname.replace(/(https?:\/\/)?(www.)?/i, '');
|
|
91
|
-
|
|
92
|
-
// Check if this is an AI chat domain - use bundled injector
|
|
93
|
-
if (isAIChatDomain(currentHostname)) {
|
|
94
|
-
console.log('🤖 AI chat domain detected:', currentHostname);
|
|
95
|
-
console.log('📦 Using bundled VG Coder injector (no Firebase needed)');
|
|
96
|
-
|
|
97
|
-
// Execute bundled injector script
|
|
98
|
-
await addScript(VG_CODER_INJECTOR_SCRIPT, 'VG_CODER_INJECTOR');
|
|
99
|
-
return; // Done - no need for Firebase script
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// For non-AI domains, continue with Firebase script loading
|
|
103
|
-
const actionType = new URL(window.location.href).searchParams.get("actionType") || "MAIN";
|
|
104
|
-
const cache = sessionStorage.getItem(actionType);
|
|
105
|
-
|
|
106
|
-
if (cache) {
|
|
107
|
-
await addScript(cache, actionType);
|
|
108
|
-
} else {
|
|
109
|
-
chrome.runtime.sendMessage({
|
|
110
|
-
action: "CONTROLLER",
|
|
111
|
-
domain: currentHostname,
|
|
112
|
-
actionType: actionType
|
|
113
|
-
}, async (response) => {
|
|
114
|
-
if (chrome.runtime.lastError) {
|
|
115
|
-
console.error('Runtime error:', chrome.runtime.lastError);
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
if (response && response.script) {
|
|
119
|
-
const fullScript = `var chromeId = "${chrome.runtime.id}"; ${response.script}`;
|
|
120
|
-
await addScript(fullScript, actionType);
|
|
121
|
-
sessionStorage.setItem(actionType, fullScript);
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
} catch (err) {
|
|
126
|
-
console.error('Controller initialization error:', err);
|
|
127
|
-
}
|
|
128
|
-
};
|
|
129
|
-
|
|
130
|
-
// Initialize controller
|
|
131
|
-
initializeController();
|
|
132
|
-
const setupRemoveExtensionListener = async () => {
|
|
133
|
-
try {
|
|
134
|
-
// Wait for DOM and body to be ready
|
|
135
|
-
await waitForDOM();
|
|
136
|
-
|
|
137
|
-
// Wait for body element
|
|
138
|
-
const waitForBody = (): Promise<HTMLBodyElement> => {
|
|
139
|
-
return new Promise((resolve) => {
|
|
140
|
-
const checkBody = () => {
|
|
141
|
-
const body = document.querySelector('body');
|
|
142
|
-
if (body) {
|
|
143
|
-
resolve(body as HTMLBodyElement);
|
|
144
|
-
} else {
|
|
145
|
-
setTimeout(checkBody, 100);
|
|
146
|
-
}
|
|
147
|
-
};
|
|
148
|
-
checkBody();
|
|
149
|
-
});
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
const body = await waitForBody();
|
|
153
|
-
|
|
154
|
-
// cach dung: document.querySelector('body').dispatchEvent(new CustomEvent('REMOVE_EXTENSION', { detail: { key: 'value' } }));
|
|
155
|
-
body.addEventListener('REMOVE_EXTENSION', function(event: any) {
|
|
156
|
-
console.log(event.detail); // { key: 'value' }
|
|
157
|
-
chrome.runtime.sendMessage({action: "REMOVE_EXTENSION", data: event.detail}, (response) => {
|
|
158
|
-
if (chrome.runtime.lastError) {
|
|
159
|
-
console.error('Runtime error:', chrome.runtime.lastError);
|
|
160
|
-
return;
|
|
161
|
-
}
|
|
162
|
-
console.log('tu huy thanh cong', response);
|
|
163
|
-
});
|
|
164
|
-
});
|
|
165
|
-
} catch (err) {
|
|
166
|
-
console.error('Setup remove extension listener error:', err);
|
|
167
|
-
}
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
// Setup remove extension listener
|
|
171
|
-
setupRemoveExtensionListener();
|
|
172
|
-
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import firebase from 'firebase/compat/app';
|
|
2
|
-
import 'firebase/compat/firestore';
|
|
3
|
-
import "firebase/compat/database";
|
|
4
|
-
import { EnvironmentStorageService } from '../utils/environment-storage.service';
|
|
5
|
-
|
|
6
|
-
// Không export trực tiếp biến đã khởi tạo nữa vì cần chờ async config
|
|
7
|
-
// export const FirebaseDatabase = firebase.database();
|
|
8
|
-
|
|
9
|
-
let initializedApp: firebase.app.App = null;
|
|
10
|
-
|
|
11
|
-
export const getFirebaseDatabase = async () => {
|
|
12
|
-
if (!initializedApp) {
|
|
13
|
-
// Lấy config từ storage hoặc mặc định
|
|
14
|
-
const config = await EnvironmentStorageService.getFirebaseConfig();
|
|
15
|
-
|
|
16
|
-
// Kiểm tra xem đã có app nào tên mặc định chưa để tránh lỗi duplicate app
|
|
17
|
-
if (firebase.apps.length === 0) {
|
|
18
|
-
initializedApp = firebase.initializeApp(config);
|
|
19
|
-
} else {
|
|
20
|
-
// Nếu đã có rồi (do reload extension context) thì dùng lại,
|
|
21
|
-
// nhưng nếu config thay đổi thì cần logic phức tạp hơn (xóa app cũ).
|
|
22
|
-
// Ở đây giả định đơn giản là dùng app hiện tại hoặc tái khởi tạo nếu cần.
|
|
23
|
-
// Với extension service worker, biến toàn cục có thể bị reset.
|
|
24
|
-
|
|
25
|
-
// Để an toàn, ta delete app cũ và init lại với config mới nhất
|
|
26
|
-
await firebase.app().delete().catch(() => { });
|
|
27
|
-
initializedApp = firebase.initializeApp(config);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
return initializedApp.database();
|
|
31
|
-
};
|
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import * as cloneDeep from 'lodash/cloneDeep';
|
|
2
|
-
import { uuid } from "../utils/db-utils";
|
|
3
|
-
interface EntityModel {
|
|
4
|
-
id?: string; // Optional for new entities
|
|
5
|
-
seqNo: number;
|
|
6
|
-
deleted: boolean;
|
|
7
|
-
}
|
|
8
|
-
export function firebaseSerialize<T>(object: T) {
|
|
9
|
-
return cloneDeep(object);
|
|
10
|
-
}
|
|
11
|
-
export class RealtimeDbCrud<T extends EntityModel> {
|
|
12
|
-
constructor(
|
|
13
|
-
protected database: any
|
|
14
|
-
) {
|
|
15
|
-
this.database = database;
|
|
16
|
-
}
|
|
17
|
-
getCurrentTimestamp() {
|
|
18
|
-
return new Date().getTime();
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
addAll(list, collectionName: string) {
|
|
22
|
-
const updates = {};
|
|
23
|
-
for (let item of list) {
|
|
24
|
-
if (!item.id) {
|
|
25
|
-
item.id = uuid();
|
|
26
|
-
}
|
|
27
|
-
item = { ...item, seqNo: this.getCurrentTimestamp(), deleted: false };
|
|
28
|
-
updates[`/${collectionName}/${item.id}`] = firebaseSerialize(item);
|
|
29
|
-
}
|
|
30
|
-
return this.database.ref().update(updates).then(() => list);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
list(collectionName: string) {
|
|
34
|
-
return new Promise((resolve, reject) => {
|
|
35
|
-
this.database
|
|
36
|
-
.ref(collectionName)
|
|
37
|
-
.once('value')
|
|
38
|
-
.then((snapshot) => {
|
|
39
|
-
const data = snapshot.val();
|
|
40
|
-
const list = Object.keys(data).map((key) => ({ id: key, ...data[key] }));
|
|
41
|
-
resolve(list);
|
|
42
|
-
})
|
|
43
|
-
.catch((error) => {
|
|
44
|
-
reject(error);
|
|
45
|
-
});
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
// work okie
|
|
49
|
-
getRT(id: string,collectionName: string, callback: (item: T) => void ) {
|
|
50
|
-
const databaseRef = this.database.ref(`${collectionName}/${id}`);
|
|
51
|
-
databaseRef.on('value', (snapshot) => {
|
|
52
|
-
const data = snapshot.val();
|
|
53
|
-
if (data) {
|
|
54
|
-
callback(data);
|
|
55
|
-
} else {
|
|
56
|
-
callback(null);
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
// work okie
|
|
61
|
-
get(id: string,collectionName: string) {
|
|
62
|
-
return new Promise((resolve, reject) => {
|
|
63
|
-
this.database
|
|
64
|
-
.ref(`${collectionName}/${id}`)
|
|
65
|
-
.once('value')
|
|
66
|
-
.then((snapshot) => {
|
|
67
|
-
const data = snapshot.val();
|
|
68
|
-
if (data) {
|
|
69
|
-
console.log(data);
|
|
70
|
-
resolve({ id, ...data });
|
|
71
|
-
} else {
|
|
72
|
-
resolve(null);
|
|
73
|
-
}
|
|
74
|
-
})
|
|
75
|
-
.catch((error) => {
|
|
76
|
-
reject(error);
|
|
77
|
-
});
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
add(entity, collectionName: string) {
|
|
82
|
-
entity = { ...entity, seqNo: this.getCurrentTimestamp(), deleted: false };
|
|
83
|
-
return new Promise((resolve, reject) => {
|
|
84
|
-
if (entity.id) {
|
|
85
|
-
const saveData = firebaseSerialize(entity);
|
|
86
|
-
this.database
|
|
87
|
-
.ref(`${collectionName}/${entity.id}`)
|
|
88
|
-
.set(saveData)
|
|
89
|
-
.then(() => {
|
|
90
|
-
resolve(entity);
|
|
91
|
-
})
|
|
92
|
-
.catch((error) => {
|
|
93
|
-
reject(error);
|
|
94
|
-
});
|
|
95
|
-
} else {
|
|
96
|
-
const id = uuid();
|
|
97
|
-
const newEntity = { id, ...entity };
|
|
98
|
-
this.database
|
|
99
|
-
.ref(collectionName)
|
|
100
|
-
.push(firebaseSerialize(newEntity))
|
|
101
|
-
.then(() => {
|
|
102
|
-
resolve(newEntity);
|
|
103
|
-
})
|
|
104
|
-
.catch((error) => {
|
|
105
|
-
reject(error);
|
|
106
|
-
});
|
|
107
|
-
}
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
update(entity, collectionName: string) {
|
|
112
|
-
return this.add(entity, collectionName );
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
listBySeq(maxSeqNo, collectionName: string) {
|
|
116
|
-
return new Promise((resolve, reject) => {
|
|
117
|
-
this.database
|
|
118
|
-
.ref(collectionName)
|
|
119
|
-
.orderByChild('seqNo')
|
|
120
|
-
.startAt(maxSeqNo)
|
|
121
|
-
.once('value')
|
|
122
|
-
.then((snapshot) => {
|
|
123
|
-
const data = snapshot.val();
|
|
124
|
-
const list = Object.keys(data).map((key) => ({ id: key, ...data[key] }));
|
|
125
|
-
resolve(list);
|
|
126
|
-
})
|
|
127
|
-
.catch((error) => {
|
|
128
|
-
reject(error);
|
|
129
|
-
});
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
delete(entity,collectionName: string) {
|
|
134
|
-
entity = { ...entity, seqNo: this.getCurrentTimestamp(), deleted: true };
|
|
135
|
-
return new Promise((resolve, reject) => {
|
|
136
|
-
this.database
|
|
137
|
-
.ref(`${collectionName}/${entity.id}`)
|
|
138
|
-
.set(firebaseSerialize(entity))
|
|
139
|
-
.then(() => {
|
|
140
|
-
resolve(entity);
|
|
141
|
-
})
|
|
142
|
-
.catch((error) => {
|
|
143
|
-
reject(error);
|
|
144
|
-
});
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { getFirebaseDatabase } from './common.firebase';
|
|
2
|
-
import { RealtimeDbCrud } from "./firebase-crud";
|
|
3
|
-
import { EnvironmentStorageService } from "../utils/environment-storage.service";
|
|
4
|
-
|
|
5
|
-
const COMMON_FUC = 'COMMON-FUNC';
|
|
6
|
-
|
|
7
|
-
export module CommonFunc {
|
|
8
|
-
export const load = async (): Promise<any> => {
|
|
9
|
-
const environmentName = await EnvironmentStorageService.getEnvironmentName();
|
|
10
|
-
|
|
11
|
-
// Khởi tạo DB async
|
|
12
|
-
const database = await getFirebaseDatabase();
|
|
13
|
-
const db = new RealtimeDbCrud<any>(database);
|
|
14
|
-
|
|
15
|
-
const list = await db.list(`ENV/${environmentName}/script`) as any[];
|
|
16
|
-
const l = (list || []).filter((it: any) => it.domain === COMMON_FUC && it.actionType === 'MAIN');
|
|
17
|
-
|
|
18
|
-
if (l.length !== 0) {
|
|
19
|
-
return l[0].code;
|
|
20
|
-
} else {
|
|
21
|
-
return `console.log('empty...${COMMON_FUC}')`;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { getFirebaseDatabase } from './common.firebase';
|
|
2
|
-
import { RealtimeDbCrud } from "./firebase-crud";
|
|
3
|
-
import { EnvironmentStorageService } from "../utils/environment-storage.service";
|
|
4
|
-
|
|
5
|
-
export module LoadScriptController {
|
|
6
|
-
|
|
7
|
-
export const loadScriptByDomain = async (domain: string, actionType: string): Promise<any> => {
|
|
8
|
-
const environmentName = await EnvironmentStorageService.getEnvironmentName();
|
|
9
|
-
|
|
10
|
-
// Khởi tạo DB async
|
|
11
|
-
const database = await getFirebaseDatabase();
|
|
12
|
-
const db = new RealtimeDbCrud<any>(database);
|
|
13
|
-
|
|
14
|
-
const list = await db.list(`ENV/${environmentName}/script`) as any[];
|
|
15
|
-
const l = (list || []).filter((it: any) => it.domain === domain && it.actionType === actionType.toUpperCase());
|
|
16
|
-
|
|
17
|
-
if (l.length !== 0) {
|
|
18
|
-
return l[0].code;
|
|
19
|
-
} else {
|
|
20
|
-
return "console.log('empty...')";
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
}
|