Skip to content

Instantly share code, notes, and snippets.

@svanas
Created August 11, 2016 09:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save svanas/8215964d88cf0ac091ea0fc7d53a45c5 to your computer and use it in GitHub Desktop.
Save svanas/8215964d88cf0ac091ea0fc7d53a45c5 to your computer and use it in GitHub Desktop.
Chrome Native Messaging Example
program com.google.chrome.example.echo;
{$APPTYPE CONSOLE}
uses
WinAPI.Windows,
System.Classes,
System.SysUtils,
System.JSON;
type
TNativeMessagingHost = class
private
class var
FStdIn,
FStdOut: THandleStream;
class function NextMessageLength: LongInt;
class function ReadMessage(msgLen: LongInt): UTF8String;
class procedure WriteMessage(const msg: UTF8String);
class property StdIn : THandleStream read FStdIn;
class property StdOut: THandleStream read FStdOut;
public
class constructor Create;
class destructor Destroy;
class procedure Listen;
end;
{ TNativeMessagingHost }
class constructor TNativeMessagingHost.Create;
begin
FStdIn := THandleStream.Create(GetStdHandle(STD_INPUT_HANDLE));
FStdOut := THandleStream.Create(GetStdHandle(STD_OUTPUT_HANDLE));
end;
class destructor TNativeMessagingHost.Destroy;
begin
if Assigned(FStdOut) then
FStdOut.Free;
if Assigned(FStdIn) then
FStdIn.Free;
end;
class function TNativeMessagingHost.NextMessageLength: LongInt;
var
Buffer: array[0..3] of Byte;
begin
Result := 0;
if StdIn.Read(Buffer, SizeOf(Result)) > 0 then
Result := PLongInt(@Buffer)^;
end;
class function TNativeMessagingHost.ReadMessage(msgLen: LongInt): UTF8String;
var
I: Integer;
begin
SetLength(Result, msgLen);
I := StdIn.Read(PUTF8Char(Result)^, msgLen);
SetLength(Result, I);
end;
class procedure TNativeMessagingHost.WriteMessage(const msg: UTF8String);
begin
StdOut.WriteData(Length(msg));
StdOut.Write(PUTF8Char(msg)^, Length(msg));
end;
class procedure TNativeMessagingHost.Listen;
var
msgLen: LongInt;
msg,
reply : UTF8String;
msgObj: TJSONValue;
begin
while True do
begin
msgLen := NextMessageLength;
if msgLen > 0 then
begin
msg := ReadMessage(msgLen);
if msg <> '' then
begin
msgObj := TJSONObject.ParseJSONValue(msg);
if Assigned(msgObj) then
try
if msgObj is TJSONObject then
begin
reply := UTF8Encode(Format('{"echo": "%s"}', [TJSONObject(msgObj).Values['text'].Value]));
WriteMessage(reply);
end;
finally
msgObj.Free;
end;
end;
end;
end;
end;
begin
try
TNativeMessagingHost.Listen;
except
on E: Exception do
WriteLn(E.Classname, ': ', E.Message);
end;
end.
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
{
"name": "com.google.chrome.example.echo",
"description": "Chrome Native Messaging API Example Host",
"path": "com.google.chrome.example.echo.exe",
"type": "stdio",
"allowed_origins": [
"chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"
]
}
:: Copyright 2014 The Chromium Authors. All rights reserved.
:: Use of this source code is governed by a BSD-style license that can be
:: found in the LICENSE file.
:: Change HKCU to HKLM if you want to install globally.
:: %~dp0 is the directory containing this bat script and ends with a backslash.
REG ADD "HKCU\Software\Google\Chrome\NativeMessagingHosts\com.google.chrome.example.echo" /ve /t REG_SZ /d "%~dp0com.google.chrome.example.echo.json" /f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment