-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpopup.js
More file actions
48 lines (40 loc) · 1.46 KB
/
popup.js
File metadata and controls
48 lines (40 loc) · 1.46 KB
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
chrome.runtime.onMessage.addListener(function(request, sender) {
if (request.action == "GetObjectinhtml") {
sendNativeMessage(request.source);
}
});
function onWindowLoad() {
var message = document.querySelector('#message');
chrome.tabs.executeScript(null, {
file: "GetObjectinhtml.js"
}, function() {
// If you try and inject into an extensions page or the webstore/NTP you'll get an error
if (chrome.runtime.lastError) {
message.innerText = 'There was an error injecting script : \n' + chrome.runtime.lastError.message;
}
});
connect();
}
window.onload = onWindowLoad;
function appendMessage(text) {
document.getElementById('message').innerHTML += "<p>" + text + "</p>";
}
function sendNativeMessage(text) {
message = {"text": text};
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;
}
function connect() {
var hostName = "neuralactiontest";
appendMessage("Connecting to native messaging host <b>" + hostName + "</b>");
port = chrome.runtime.connectNative(hostName);
port.onMessage.addListener(onNativeMessage);
port.onDisconnect.addListener(onDisconnected);
}