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
|
// Background service worker for Cerberus Chrome/Edge extension (MV3)
function connectNative() {
try {
return chrome.runtime.connectNative('com.cerberus.pm');
} catch (e) {
console.error('Native host connection failed', e);
return null;
}
}
function nativeRequest(payload) {
return new Promise((resolve, reject) => {
const port = connectNative();
if (!port) {
reject(new Error('no_native_host'));
return;
}
const onMessage = (resp) => {
port.onMessage.removeListener(onMessage);
try { port.disconnect(); } catch {}
resolve(resp);
};
const onDisconnect = () => {
port.onMessage.removeListener(onMessage);
reject(new Error('disconnected'));
};
port.onMessage.addListener(onMessage);
port.onDisconnect.addListener(onDisconnect);
port.postMessage(payload);
});
}
async function getActiveTab() {
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
return tabs[0];
}
async function getOriginForActiveTab() {
const tab = await getActiveTab();
try {
const url = new URL(tab.url);
return url.origin;
} catch (e) {
return tab.url;
}
}
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
(async () => {
if (!message || !message.type) return;
if (message.type === 'GET_PAGE_FORMS') {
const tab = await getActiveTab();
chrome.tabs.sendMessage(tab.id, { type: 'SCAN_FORMS' }, sendResponse);
return true;
}
if (message.type === 'FILL_CREDENTIALS') {
const tab = await getActiveTab();
chrome.tabs.sendMessage(tab.id, { type: 'FILL_CREDENTIALS', payload: message.payload }, sendResponse);
return true;
}
if (message.type === 'GET_CREDENTIALS_FOR_TAB') {
const origin = await getOriginForActiveTab();
try {
const resp = await nativeRequest({ type: 'get_for_origin', origin, include_password: true });
sendResponse(resp);
} catch (e) {
sendResponse({ ok: false, error: String(e) });
}
return true;
}
if (message.type === 'PING_NATIVE') {
try {
const resp = await nativeRequest({ type: 'ping' });
sendResponse(resp);
} catch (e) {
sendResponse({ ok: false, error: String(e) });
}
return true;
}
})();
return true; // keep channel open for async
});
|