{"id":191,"date":"2013-12-04T14:06:34","date_gmt":"2013-12-04T05:06:34","guid":{"rendered":"http:\/\/isitea.net\/wordpress\/?p=191"},"modified":"2013-12-04T14:06:34","modified_gmt":"2013-12-04T05:06:34","slug":"c-chrome-extension-app-communicate-using-port-sendnativemessage","status":"publish","type":"post","link":"https:\/\/isitea.net\/wordpress\/2013\/12\/04\/c-chrome-extension-app-communicate-using-port-sendnativemessage\/","title":{"rendered":"C# &#8211; Chrome extension \/ App communicate using port \/ sendNativeMessage"},"content":{"rendered":"<p>C#<\/p>\n<blockquote><p>private static string OpenStandardStreamIn()<br \/>\n{<br \/>\n\/\/\/\/ We need to read first 4 bytes for length information<br \/>\nStream stdin = Console.OpenStandardInput();<br \/>\nint length = 0;<br \/>\nbyte[] bytes = new byte[4];<br \/>\nstdin.Read(bytes, 0, 4);<br \/>\nlength = System.BitConverter.ToInt32(bytes, 0);<\/p>\n<p>string input = &#8220;&#8221;;<br \/>\nfor (int i = 0; i &lt; length;i++ )<br \/>\n{<br \/>\ninput += (char)stdin.ReadByte();<br \/>\n}<\/p>\n<p>return input;<br \/>\n}<\/p>\n<p>private static void OpenStandardStreamOut(string stringData)<br \/>\n{<br \/>\n\/\/\/\/ We need to send the 4 btyes of length information<br \/>\nint DataLength = stringData.Length;<br \/>\nStream stdout = Console.OpenStandardOutput();<br \/>\nstdout.WriteByte((byte)((DataLength &gt;&gt; 0) &amp; 0xFF));<br \/>\nstdout.WriteByte((byte)((DataLength &gt;&gt; 8) &amp; 0xFF));<br \/>\nstdout.WriteByte((byte)((DataLength &gt;&gt; 16) &amp; 0xFF));<br \/>\nstdout.WriteByte((byte)((DataLength &gt;&gt; 24) &amp; 0xFF));<br \/>\n\/\/Available total length : 4,294,967,295 ( FF FF FF FF )<\/p>\n<p>Console.Write(stringData);<br \/>\n}<\/p><\/blockquote>\n<p>&#8220;OpenStandardStremIn()&#8221;\u00a0returns a string which contains JSON type data.<\/p>\n<p>and OpenStandardStreamOut( string stringData ) must start with a string which belongs in JSON.<\/p>\n<p>&nbsp;<\/p>\n<p>Chrome<\/p>\n<blockquote><p>\/\/ Copyright 2013 The Chromium Authors. All rights reserved.<br \/>\n\/\/ Use of this source code is governed by a BSD-style license that can be<br \/>\n\/\/ found in the LICENSE file.<\/p>\n<p>var port = null;<\/p>\n<p>var getKeys = function(obj){<br \/>\nvar keys = [];<br \/>\nfor(var key in obj){<br \/>\nkeys.push(key);<br \/>\n}<br \/>\nreturn keys;<br \/>\n}<br \/>\nfunction appendMessage(text) {<br \/>\ndocument.getElementById(&#8216;response&#8217;).innerHTML += &#8220;&lt;p&gt;&#8221; + text + &#8220;&lt;\/p&gt;&#8221;;<br \/>\n}<\/p>\n<p>function updateUiState() {<br \/>\nif (port) {<br \/>\ndocument.getElementById(&#8216;connect-button&#8217;).style.display = &#8216;none&#8217;;<br \/>\ndocument.getElementById(&#8216;input-text&#8217;).style.display = &#8216;block&#8217;;<br \/>\ndocument.getElementById(&#8216;send-message-button&#8217;).style.display = &#8216;block&#8217;;<br \/>\n} else {<br \/>\ndocument.getElementById(&#8216;connect-button&#8217;).style.display = &#8216;block&#8217;;<br \/>\ndocument.getElementById(&#8216;input-text&#8217;).style.display = &#8216;none&#8217;;<br \/>\ndocument.getElementById(&#8216;send-message-button&#8217;).style.display = &#8216;none&#8217;;<br \/>\n}<br \/>\n}<\/p>\n<p>function sendNativeMessage() {<br \/>\nmessage = {&#8220;text&#8221;: document.getElementById(&#8216;input-text&#8217;).value};<br \/>\nport.postMessage(message);<br \/>\nappendMessage(&#8220;Sent message: &lt;b&gt;&#8221; + JSON.stringify(message) + &#8220;&lt;\/b&gt;&#8221;);<br \/>\n}<\/p>\n<p>function onNativeMessage(message) {<br \/>\nappendMessage(&#8220;Received message: &lt;b&gt;&#8221; + JSON.stringify(message) + &#8220;&lt;\/b&gt;&#8221;);<br \/>\n}<\/p>\n<p>function onDisconnected() {<br \/>\nappendMessage(&#8220;Failed to connect: &#8221; + chrome.runtime.lastError.message);<br \/>\nport = null;<br \/>\nupdateUiState();<br \/>\n}<\/p>\n<p>function connect() {<br \/>\nvar hostName = &#8220;com.google.chrome.example.echo&#8221;;<br \/>\nappendMessage(&#8220;Connecting to native messaging host &lt;b&gt;&#8221; + hostName + &#8220;&lt;\/b&gt;&#8221;)<br \/>\nport = chrome.runtime.connectNative(hostName);<br \/>\nport.onMessage.addListener(onNativeMessage);<br \/>\nport.onDisconnect.addListener(onDisconnected);<br \/>\nupdateUiState();<br \/>\n}<\/p>\n<p>document.addEventListener(&#8216;DOMContentLoaded&#8217;, function () {<br \/>\ndocument.getElementById(&#8216;connect-button&#8217;).addEventListener(<br \/>\n&#8216;click&#8217;, connect);<br \/>\ndocument.getElementById(&#8216;send-message-button&#8217;).addEventListener(<br \/>\n&#8216;click&#8217;, sendNativeMessage);<br \/>\nupdateUiState();<br \/>\n});<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p>Chrome extension \/ app side script comes from Chromium project NativeMessaging example<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C# private static string OpenStandardStreamIn() { \/\/\/\/ We need to read first 4 bytes for length information Stream stdin = Console.OpenStandardInput(); int length = 0; byte[] bytes = new byte[4]; stdin.Read(bytes, 0, 4); length = System.BitConverter.ToInt32(bytes, 0); string input = &#8220;&#8221;; for (int i = 0; i &lt; length;i++ ) { input += (char)stdin.ReadByte(); } [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-191","post","type-post","status-publish","format-standard","hentry","category-it"],"_links":{"self":[{"href":"https:\/\/isitea.net\/wordpress\/wp-json\/wp\/v2\/posts\/191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/isitea.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/isitea.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/isitea.net\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/isitea.net\/wordpress\/wp-json\/wp\/v2\/comments?post=191"}],"version-history":[{"count":1,"href":"https:\/\/isitea.net\/wordpress\/wp-json\/wp\/v2\/posts\/191\/revisions"}],"predecessor-version":[{"id":192,"href":"https:\/\/isitea.net\/wordpress\/wp-json\/wp\/v2\/posts\/191\/revisions\/192"}],"wp:attachment":[{"href":"https:\/\/isitea.net\/wordpress\/wp-json\/wp\/v2\/media?parent=191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/isitea.net\/wordpress\/wp-json\/wp\/v2\/categories?post=191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/isitea.net\/wordpress\/wp-json\/wp\/v2\/tags?post=191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}