forked from pga03/extension-chrome-windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (84 loc) · 3.24 KB
/
index.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
var port, message= null;
///////////////////////////////////////////////////////////////////////////////
// Native Messaging Functions //
///////////////////////////////////////////////////////////////////////////////
var getKeys = function(obj){
var keys = [];
for(var key in obj){
keys.push(key);
}
return keys;
}
function appendMessage(text) {
document.getElementById('response').innerHTML += "<p>" + text + "</p>";
}
function updateUiState() {
if (port) {
document.getElementById('connect-button').style.display = 'none';
document.getElementById('input-text').style.display = 'block';
document.getElementById('send-message-button').style.display = 'block';
} else {
document.getElementById('connect-button').style.display = 'block';
document.getElementById('input-text').style.display = 'none';
document.getElementById('send-message-button').style.display = 'none';
}
}
function sendNativeMessage() {
message = {"text": document.getElementById('input-text').value};
port.postMessage(message);
appendMessage("Sent message: <b>" + JSON.stringify(message) + "</b>");
}
function passMessageToNative(message) {
port.postMessage(message);
appendMessage("Sent message: <b>" + JSON.stringify(message) + "</b>");
}
function onNativeMessage(message) {
appendMessage("Received message: <b>" + JSON.stringify(message) + "</b>");
}
function onDisconnected() {
appendMessage("Failed to connect: " + chrome.runtime.lastError.message);
port = null;
updateUiState();
}
function connect() {
var hostName = "com.ibm.firstdiscovery";
appendMessage("Connecting to native messaging host <b>" + hostName + "</b>")
port = chrome.runtime.connectNative(hostName);
port.onMessage.addListener(onNativeMessage);
port.onDisconnect.addListener(onDisconnected);
updateUiState();
}
document.addEventListener('DOMContentLoaded', function () {
document.getElementById('connect-button').addEventListener(
'click', connect);
document.getElementById('send-message-button').addEventListener(
'click', sendNativeMessage);
updateUiState();
});
///////////////////////////////////////////////////////////////////////////////
// Non-Native Messaging Functions //
///////////////////////////////////////////////////////////////////////////////
// Listen to events from our web app and handle them.
chrome.runtime.onMessageExternal.addListener(
function (request, sender, sendResponse) {
// log the parameters to console for debug
console.log('a message was received');
console.log('request:');
console.dir(request);
console.log('sender:');
console.dir(sender);
console.log('sendResponse:');
console.dir(sendResponse);
if(request.message === undefined){
console.log('undefined message recieved. Its Probably no big deal though');
}
// wrap the message in an object and send it back to the caller
var msgResponse = {
"received": request
}
document.getElementById('message').innerHTML ="<h3>" + request.message + "</h3>";
sendResponse("I got your message!");
//Pass the non-native message to the native host
passMessageToNative(request.message);
sendResponse(msgResponse);
});