blob: d05cd377c8910de35dadb525bd2abc133d6efa24 (
plain)
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
|
// Basic background script for Cerberus Firefox extension
browser.runtime.onInstalled.addListener(() => {
console.log("Cerberus extension installed");
});
function connectNative() {
try {
return browser.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);
port.disconnect();
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 getOriginForTab(tabId) {
const tab = await browser.tabs.get(tabId);
try {
const url = new URL(tab.url);
return url.origin;
} catch (e) {
return tab.url;
}
}
// Message router between popup/content and native host
browser.runtime.onMessage.addListener(async (message, sender) => {
if (!message || !message.type) return;
if (message.type === 'GET_PAGE_FORMS') {
return browser.tabs.sendMessage(sender.tab.id, { type: 'SCAN_FORMS' });
}
if (message.type === 'FILL_CREDENTIALS') {
return browser.tabs.sendMessage(sender.tab.id, {
type: 'FILL_CREDENTIALS',
payload: message.payload,
});
}
if (message.type === 'GET_CREDENTIALS_FOR_TAB') {
const origin = await getOriginForTab(sender.tab.id);
const resp = await nativeRequest({ type: 'get_for_origin', origin, include_password: true }).catch(err => ({ ok: false, error: String(err) }));
return resp;
}
if (message.type === 'PING_NATIVE') {
const resp = await nativeRequest({ type: 'ping' }).catch(err => ({ ok: false, error: String(err) }));
return resp;
}
});
|