· 6 years ago · Sep 06, 2019, 01:14 PM
1Main.js
2// Copyright 2013 The Chromium Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5
6var port = null;
7
8var getKeys = function(obj){
9 var keys = [];
10 for(var key in obj){
11 keys.push(key);
12 }
13 return keys;
14}
15
16
17function appendMessage(text) {
18 document.getElementById('response').innerHTML += "<p>" + text + "</p>";
19}
20
21function updateUiState() {
22 if (port) {
23 document.getElementById('connect-button').style.display = 'none';
24 document.getElementById('input-text').style.display = 'block';
25 document.getElementById('send-message-button').style.display = 'block';
26 } else {
27 document.getElementById('connect-button').style.display = 'block';
28 document.getElementById('input-text').style.display = 'none';
29 document.getElementById('send-message-button').style.display = 'none';
30 }
31}
32
33function sendNativeMessage() {
34 message = {"text": document.getElementById('input-text').value};
35 port.postMessage(message);
36 appendMessage("Sent message: <b>" + JSON.stringify(message) + "</b>");
37}
38
39function onNativeMessage(message) {
40 appendMessage("Received message: <b>" + JSON.stringify(message) + "</b>");
41}
42
43function onDisconnected() {
44 appendMessage("Failed to connect: " + chrome.runtime.lastError.message);
45 port = null;
46 updateUiState();
47}
48
49function connect() {
50 var hostName = "com.google.chrome.example.echo";
51 appendMessage("Connecting to native messaging host <b>" + hostName + "</b>")
52 port = chrome.runtime.connectNative(hostName);
53 port.onMessage.addListener(onNativeMessage);
54 port.onDisconnect.addListener(onDisconnected);
55 updateUiState();
56}
57
58document.addEventListener('DOMContentLoaded', function () {
59 document.getElementById('connect-button').addEventListener(
60 'click', connect);
61 document.getElementById('send-message-button').addEventListener(
62 'click', sendNativeMessage);
63 updateUiState();
64});
65
66
67
68
69Manifest file in app
70{
71 // Extension ID: knldjmfmopnpolahpmmgbagdohdnhkik
72 "key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcBHwzDvyBQ6bDppkIs9MP4ksKqCMyXQ/A52JivHZKh4YO/9vJsT3oaYhSpDCE9RPocOEQvwsHsFReW2nUEc6OLLyoCFFxIb7KkLGsmfakkut/fFdNJYh0xOTbSN8YvLWcqph09XAY2Y/f0AL7vfO1cuCqtkMt8hFrBGWxDdf9CQIDAQAB",
73 "name": "Test App",
74 "version": "1.0",
75 "manifest_version": 2,
76 "description": "Send a message.",
77 "app": {
78 "launch": {
79 "local_path": "main.html"
80 }
81 },
82 "icons": {
83 "128": "icon-128.png"
84 },
85 "permissions": [
86 "nativeMessaging"
87 ]
88}
89HOST com.example json file
90{
91 "name": "com.google.chrome.example.echo",
92 "description": "Chrome Native Messaging API Example Host",
93 "path": "ConsoleApp6.exe",
94 "type": "stdio",
95 "allowed_origins": [
96 "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"
97 ]
98}
99
100
101native-messaging-example-host.bat file
102@echo off
103start %~dp0/ConsoleApp6.exe
104
105install_host.bat
106
107REG ADD "HKCU\Software\Google\Chrome\NativeMessagingHosts\com.google.chrome.example.echo" /ve /t REG_SZ /d "%~dp0com.google.chrome.example.echo.json" /f
108
109
110.EXE FILE CONTENTS
111using System;
112using System.Collections.Generic;
113using System.IO;
114using System.Linq;
115using System.Text;
116using System.Threading.Tasks;
117
118namespace ConsoleApp5
119{
120 class Program
121 {
122 static void Main(string[] args)
123 {
124 {
125 string message = "test message from native app.";
126 OpenStandardStreamOut(message);
127
128 while (OpenStandardStreamIn() != null || OpenStandardStreamIn() != "")
129 {
130 OpenStandardStreamOut("Received to Native App: " + OpenStandardStreamIn());
131 OpenStandardStreamOut("Recieved: " + OpenStandardStreamIn());
132 }
133 }
134 }
135
136 private static string OpenStandardStreamIn()
137 {
138 //// We need to read first 4 bytes for length information
139 Stream stdin = Console.OpenStandardInput();
140 int length = 0;
141 byte[] bytes = new byte[4];
142 stdin.Read(bytes, 0, 4);
143 length = System.BitConverter.ToInt32(bytes, 0);
144
145 string input = "";
146 for (int i = 0; i < length; i++)
147 {
148 input += (char)stdin.ReadByte();
149 }
150
151 return input;
152 }
153 private static void OpenStandardStreamOut(string stringData)
154 {
155 //// We need to send the 4 btyes of length information
156 string msgdata = "{\"text\":\"" + stringData + "\"}";
157 int DataLength = msgdata.Length;
158 Stream stdout = Console.OpenStandardOutput();
159 stdout.WriteByte((byte)((DataLength >> 0) & 0xFF));
160 stdout.WriteByte((byte)((DataLength >> 8) & 0xFF));
161 stdout.WriteByte((byte)((DataLength >> 16) & 0xFF));
162 stdout.WriteByte((byte)((DataLength >> 24) & 0xFF));
163 //Available total length : 4,294,967,295 ( FF FF FF FF )
164 Console.Write(msgdata);
165 }
166 }
167}
168
169UPDATED C# CODE
170using System;
171using System.Collections.Generic;
172using System.Linq;
173using System.Text;
174using System.Threading.Tasks;
175using System.IO;
176
177namespace ConsoleApp2
178{
179 class Program
180 {
181 static void Main(string[] args)
182 {
183 String line;
184 try
185 {
186 //Pass the file path and file name to the StreamReader constructor
187 StreamReader sr = new StreamReader("C:/Users/kabilesh/Desktop/kartic.txt");
188
189 //Read the first line of text
190 line = sr.ReadLine();
191
192 //Continue to read until you reach end of file
193 while (line != null)
194 {
195 //write the lie to console window
196 // Console.WriteLine(line);
197 //Read the next line
198
199 OpenStandardStreamOut(line);
200 line = sr.ReadLine();
201
202 }
203
204 //close the file
205
206 sr.Close();
207 Console.ReadLine();
208
209
210 }
211 catch (Exception e)
212 {
213 Console.WriteLine("Exception: " + e.Message);
214 }
215 // string message = "test message from native app";
216 // OpenStandardStreamOut(line);
217
218 while (OpenStandardStreamIn() != null || OpenStandardStreamIn() != "")
219 {
220 OpenStandardStreamOut("Received to Native App: " + OpenStandardStreamIn());
221 OpenStandardStreamOut("Recieved: " + OpenStandardStreamIn());
222 }
223 }
224
225
226 private static string OpenStandardStreamIn()
227 {
228 //// We need to read first 4 bytes for length information
229 Stream stdin = Console.OpenStandardInput();
230 int length = 0;
231 byte[] bytes = new byte[4];
232 stdin.Read(bytes, 0, 4);
233 length = System.BitConverter.ToInt32(bytes, 0);
234
235 string input = "";
236 for (int i = 0; i < length; i++)
237 {
238 input += (char)stdin.ReadByte();
239 }
240
241 return input;
242 }
243 private static void OpenStandardStreamOut(string stringData)
244 {
245 //// We need to send the 4 btyes of length information
246 string msgdata = "{\"text\":\"" + stringData + "\"}";
247 int DataLength = msgdata.Length;
248 Stream stdout = Console.OpenStandardOutput();
249 stdout.WriteByte((byte)((DataLength >> 0) & 0xFF));
250 stdout.WriteByte((byte)((DataLength >> 8) & 0xFF));
251 stdout.WriteByte((byte)((DataLength >> 16) & 0xFF));
252 stdout.WriteByte((byte)((DataLength >> 24) & 0xFF));
253 //Available total length : 4,294,967,295 ( FF FF FF FF )
254 Console.Write(msgdata);
255 }
256 }
257}
258
259
260
261CHANGED .js FILE
262
263// Copyright 2013 The Chromium Authors. All rights reserved.
264// Use of this source code is governed by a BSD-style license that can be
265// found in the LICENSE file.
266
267var port = null;
268
269var getKeys = function(obj){
270 var keys = [];
271 for(var key in obj){
272 keys.push(key);
273 }
274 return keys;
275}
276
277
278function appendMessage(text) {
279 document.getElementById('response').innerHTML += "<p>" + text + "</p>";
280}
281
282function updateUiState() {
283 if (port) {
284 document.getElementById('connect-button').style.display = 'none';
285
286 } else {
287 document.getElementById('connect-button').style.display = 'block';
288
289 }
290}
291
292
293
294function onNativeMessage(message) {
295 appendMessage("Received message: <b>" + JSON.stringify(message) + "</b>");
296}
297
298function onDisconnected() {
299 appendMessage("Failed to connect: " + chrome.runtime.lastError.message);
300 port = null;
301 updateUiState();
302}
303
304function connect() {
305 var hostName = "com.google.chrome.example.echo";
306 appendMessage("Connecting to load your data <b>")
307 port = chrome.runtime.connectNative(hostName);
308 port.onMessage.addListener(onNativeMessage);
309 port.onDisconnect.addListener(onDisconnected);
310 updateUiState();
311}
312
313document.addEventListener('DOMContentLoaded', function () {
314 document.getElementById('connect-button').addEventListener(
315 'click', connect);
316
317 updateUiState();
318});
319
320
321
322Updated Output
323
324
325
326
327REFERRED SITE
328https://stackoverflow.com/questions/24219144/native-messaging-chrome
329
330NEW AS AN EXTENSION
331MAIN.JS
332// Copyright 2013 The Chromium Authors. All rights reserved.
333// Use of this source code is governed by a BSD-style license that can be
334// found in the LICENSE file.
335
336var port = null;
337
338var getKeys = function(obj){
339 var keys = [];
340 for(var key in obj){
341 keys.push(key);
342 }
343 return keys;
344}
345
346
347function appendMessage(text) {
348 document.getElementById('response').innerHTML += "<p>" + text + "</p>";
349}
350
351function updateUiState() {
352 if (port) {
353 document.getElementById('connect-button').style.display = 'none';
354
355 } else {
356 document.getElementById('connect-button').style.display = 'block';
357
358 }
359}
360
361
362
363function onNativeMessage(message) {
364 appendMessage("Received message: <b>" + JSON.stringify(message) + "</b>");
365}
366
367function onDisconnected() {
368 appendMessage("Failed to connect: " + chrome.runtime.lastError.message);
369 port = null;
370 updateUiState();
371}
372
373function connect() {
374 var hostName = "com.google.chrome.example.echo";
375 appendMessage("Connecting to load your data <b>")
376 port = chrome.runtime.connectNative(hostName);
377 port.onMessage.addListener(onNativeMessage);
378 port.onDisconnect.addListener(onDisconnected);
379 updateUiState();
380}
381
382document.addEventListener('DOMContentLoaded', function () {
383 document.getElementById('connect-button').addEventListener(
384 'click', connect);
385
386 updateUiState();
387});
388
389MANIFEST.JSON
390{
391 // Extension ID: knldjmfmopnpolahpmmgbagdohdnhkik
392 "key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcBHwzDvyBQ6bDppkIs9MP4ksKqCMyXQ/A52JivHZKh4YO/9vJsT3oaYhSpDCE9RPocOEQvwsHsFReW2nUEc6OLLyoCFFxIb7KkLGsmfakkut/fFdNJYh0xOTbSN8YvLWcqph09XAY2Y/f0AL7vfO1cuCqtkMt8hFrBGWxDdf9CQIDAQAB",
393 "name": "Try Again",
394 "version": "1.0",
395
396
397 "background":{
398 "scripts":[ "background.js" ]
399 },
400
401
402 "browser_action": {
403 "default_title":"open"
404 },
405 "icons": {
406 "128": "icon-128.png"
407 },
408
409 "permissions": [
410 "nativeMessaging"
411 ],
412 "manifest_version": 2,
413 "description": "Dont get feared."
414}
415
416
417
418
419BACKGROUND.JS
420chrome.browserAction.onClicked.addListener(function() {
421 chrome.extension.connectNative("com.google.chrome.example.echo");
422});
423com.google.chrome.example.echo
424
425// Copyright 2013 The Chromium Authors. All rights reserved.
426// Use of this source code is governed by a BSD-style license that can be
427// found in the LICENSE file.
428
429{
430 "name": "com.google.chrome.example.echo",
431 "description": "Chrome Native Messaging API Example Host",
432 "path": "ConsoleApp8.exe",
433 "type": "stdio",
434 "allowed_origins": [
435 "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"
436 ]
437}
438
439
440
441
442install_host
443
444REG ADD "HKCU\Software\Google\Chrome\NativeMessagingHosts\com.google.chrome.example.echo" /ve /t REG_SZ /d "%~dp0com.google.chrome.example.echo.json" /f
445
446native-messaging-example-host
447@echo off
448:: Copyright (c) 2013 The Chromium Authors. All rights reserved.
449:: Use of this source code is governed by a BSD-style license that can be
450:: found in the LICENSE file.
451
452start %~dp0/ConsoleApp8.exe