Skip to content

Commit

Permalink
migrate to mv3
Browse files Browse the repository at this point in the history
  • Loading branch information
Shubham-Rasal committed Mar 6, 2023
1 parent cf2df4f commit b96f4fe
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ messaging API that allows to communicate with a native application.
In order for this example to work you must first install the native messaging
host from the host directory.

To install the host:
## To install the host:


**On Windows:**

On Windows:
Run install_host.bat script in the host directory.
This script installs the native messaging host for the current user, by
creating a registry key
Expand All @@ -17,7 +19,8 @@ On Windows:
HKLM.
Note that you need to have python installed.

On Mac and Linux:
**On Mac and Linux:**

Run install_host.sh script in the host directory:
host/install_host.sh
By default the host is installed only for the user who runs the script, but if
Expand Down
13 changes: 7 additions & 6 deletions _archive/mv2/api/nativeMessaging/app/manifest.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
// Extension ID: knldjmfmopnpolahpmmgbagdohdnhkik
"manifest_version": 3,
"key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcBHwzDvyBQ6bDppkIs9MP4ksKqCMyXQ/A52JivHZKh4YO/9vJsT3oaYhSpDCE9RPocOEQvwsHsFReW2nUEc6OLLyoCFFxIb7KkLGsmfakkut/fFdNJYh0xOTbSN8YvLWcqph09XAY2Y/f0AL7vfO1cuCqtkMt8hFrBGWxDdf9CQIDAQAB",
"name": "Native Messaging Example",
"version": "1.0",
"manifest_version": 2,
"description": "Send a message to a native application.",
"app": {
"launch": {
"local_path": "main.html"
}
"background": {
"service_worker": "main.js"
},
"action": {
"default_title": "Native Messaging Example",
"default_popup": "main.html"
},
"icons": {
"128": "icon-128.png"
Expand Down
10 changes: 5 additions & 5 deletions _archive/mv2/api/nativeMessaging/host/install_host.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
set -e

DIR="$( cd "$( dirname "$0" )" && pwd )"
if [ "$(uname -s)" = "Darwin" ]; then
if [ "$(whoami)" = "root" ]; then
if [ $(uname -s) == 'Darwin' ]; then
if [ "$(whoami)" == "root" ]; then
TARGET_DIR="/Library/Google/Chrome/NativeMessagingHosts"
else
TARGET_DIR="$HOME/Library/Application Support/Google/Chrome/NativeMessagingHosts"
fi
else
if [ "$(whoami)" = "root" ]; then
if [ "$(whoami)" == "root" ]; then
TARGET_DIR="/etc/opt/chrome/native-messaging-hosts"
else
TARGET_DIR="$HOME/.config/google-chrome/NativeMessagingHosts"
Expand All @@ -29,11 +29,11 @@ mkdir -p "$TARGET_DIR"
cp "$DIR/$HOST_NAME.json" "$TARGET_DIR"

# Update host path in the manifest.
HOST_PATH=$DIR/native-messaging-example-host
HOST_PATH="$DIR/native-messaging-example-host"
ESCAPED_HOST_PATH=${HOST_PATH////\\/}
sed -i -e "s/HOST_PATH/$ESCAPED_HOST_PATH/" "$TARGET_DIR/$HOST_NAME.json"

# Set permissions for the manifest so that all users can read it.
chmod o+r "$TARGET_DIR/$HOST_NAME.json"

echo "Native messaging host $HOST_NAME has been installed."
echo Native messaging host $HOST_NAME has been installed.
46 changes: 22 additions & 24 deletions _archive/mv2/api/nativeMessaging/host/native-messaging-example-host
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
#!/usr/bin/env python
# Copyright (c) 2012 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.

# A simple native messaging host. Shows a Tkinter dialog with incoming messages
# that also allows to send message back to the webapp.

import struct
import sys
import threading
import Queue
import queue as Queue

try:
import Tkinter
import tkMessageBox
import tkinter as Tkinter
import tkinter.messagebox
except ImportError:
Tkinter = None

Expand All @@ -27,7 +22,7 @@ if sys.platform == "win32":
# Helper function that sends a message to the webapp.
def send_message(message):
# Write message size.
sys.stdout.write(struct.pack('I', len(message)))
sys.stdout.buffer.write(struct.pack('I', len(message)))
# Write the message itself.
sys.stdout.write(message)
sys.stdout.flush()
Expand All @@ -37,18 +32,21 @@ def read_thread_func(queue):
message_number = 0
while 1:
# Read the message length (first 4 bytes).
text_length_bytes = sys.stdin.read(4)
text_length_bytes = sys.stdin.buffer.read(4)

if len(text_length_bytes) == 0:
if queue:
queue.put(None)
sys.exit(0)

# Unpack message length as 4 byte integer.
text_length = struct.unpack('i', text_length_bytes)[0]
text_length = struct.unpack('@I', text_length_bytes)[0]

# Read the text (JSON object) of the message.
text = sys.stdin.read(text_length).decode('utf-8')
text = sys.stdin.buffer.read(text_length).decode('utf-8')

if text == '{"text":"exit"}':
break

if queue:
queue.put(text)
Expand All @@ -57,22 +55,22 @@ def read_thread_func(queue):
send_message('{"echo": %s}' % text)

if Tkinter:
class NativeMessagingWindow(Tkinter.Frame):
class NativeMessagingWindow(tkinter.Frame):
def __init__(self, queue):
self.queue = queue

Tkinter.Frame.__init__(self)
tkinter.Frame.__init__(self)
self.pack()

self.text = Tkinter.Text(self)
self.text = tkinter.Text(self)
self.text.grid(row=0, column=0, padx=10, pady=10, columnspan=2)
self.text.config(state=Tkinter.DISABLED, height=10, width=40)
self.text.config(state=tkinter.DISABLED, height=10, width=40)

self.messageContent = Tkinter.StringVar()
self.sendEntry = Tkinter.Entry(self, textvariable=self.messageContent)
self.messageContent = tkinter.StringVar()
self.sendEntry = tkinter.Entry(self, textvariable=self.messageContent)
self.sendEntry.grid(row=1, column=0, padx=10, pady=10)

self.sendButton = Tkinter.Button(self, text="Send", command=self.onSend)
self.sendButton = tkinter.Button(self, text="Send", command=self.onSend)
self.sendButton.grid(row=1, column=1, padx=10, pady=10)

self.after(100, self.processMessages)
Expand All @@ -93,14 +91,14 @@ if Tkinter:
try:
send_message(text)
except IOError:
tkMessageBox.showinfo('Native Messaging Example',
tkinter.messagebox.showinfo('Native Messaging Example',
'Failed to send message.')
sys.exit(1)

def log(self, message):
self.text.config(state=Tkinter.NORMAL)
self.text.insert(Tkinter.END, message + "\n")
self.text.config(state=Tkinter.DISABLED)
self.text.config(state=tkinter.NORMAL)
self.text.insert(tkinter.END, message + "\n")
self.text.config(state=tkinter.DISABLED)


def Main():
Expand All @@ -125,4 +123,4 @@ def Main():


if __name__ == '__main__':
Main()
Main()

0 comments on commit b96f4fe

Please sign in to comment.