-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbackground.js
111 lines (101 loc) · 3.58 KB
/
background.js
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
"use strict"
// Background page
// https://developer.chrome.com/extensions/event_pages
// Our background page isn't persistent.
chrome.runtime.onStartup.addListener(()=> { init('onStartup'); });
// onInstalled when user uses chrome://extensions page to reload
chrome.runtime.onInstalled.addListener(() => { init('onInstalled'); });
let portNMH = null;
function nmhSend(oMsg) {
/*
When a messaging port is created using runtime.connectNative Chrome starts native messaging host process and keeps it running until the port
is destroyed. On the other hand, when a message is sent using runtime.sendNativeMessage, without creating a messaging port, Chrome starts a
new native messaging host process for each message. In that case the first message generated by the host process is handled as a response to
the original request, i.e. Chrome will pass it to the response callback specified when runtime.sendNativeMessage is called. All other messages
generated by the native messaging host in that case are ignored.
*/
console.log('About to send ' + JSON.stringify(oMsg));
if (!portNMH) {
console.log('Port not listening'); return;
}
try {
portNMH.postMessage( oMsg );
} catch (e) {
console.log('! postMessage failed! ' + e.message);
}
}
function init(sReason)
{
// Spin up the NativeMessaging listener
console.log(`init('${sReason}'): Attempting to attach NativeMessaging Host!`);
if (portNMH) {
console.log('Warning: Port was already in existence. Bailing out.');
return;
}
try {
portNMH = chrome.runtime.connectNative('com.bayden.nmf.demo');
portNMH.onDisconnect.addListener( () => {
console.log("!!!NativeMessagingHost.onDisconnect(); " + chrome.runtime.lastError.message);
});
portNMH.onMessage.addListener( (msg) => {
console.log("@@@ Got Message from NMH: ");
console.dir(msg);
});
}
catch (e) {
console.log('!!!Failed to connect to nativeMessagingHost! ' + e.message + " " + chrome.runtime.lastError.message);
}
console.log(`init('${sReason}'): completed.`);
}
chrome.webNavigation.onBeforeNavigate.addListener(function (data) {
const oEvent = {
'event': 'navigation',
'destination': data.url,
'tabId': data.tabId,
'frameId': data.frameId,
'parentFrameId': data.parentFrameId,
'timeStamp': data.timeStamp
};
nmhSend(oEvent);
});
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/onBeforeRequest
chrome.webRequest.onBeforeRequest.addListener(
function(data) {
const oEvent = {
'event': 'wrBeforeRequest',
'requestId': data.requestId,
'method': data.method,
'url': data.url,
'documentUrl': data.documentUrl,
'tabId': data.tabId,
'frameId': data.frameId,
'parentFrameId': data.parentFrameId,
'timeStamp': data.timeStamp,
'type': data.type
};
nmhSend(oEvent);
},
{urls: ["<all_urls>"]},
[]);
chrome.webRequest.onHeadersReceived.addListener(
function(data) {
const oEvent = {
'event': 'wrResponseHeaders',
'requestId': data.requestId,
'statusCode': data.statusCode,
'url': data.url,
'timeStamp': data.timeStamp,
'fromCache': data.fromCache,
'ip': data.ip,
'type': data.type,
'responseHeaders': data.responseHeaders
};
nmhSend(oEvent);
},
{urls: ["<all_urls>"]},
["responseHeaders"]);
chrome.browserAction.onClicked.addListener (
(t)=>{
chrome.tabs.create({ url: "/monitor.html" });
}
)