Compile the chrome plug-in, call the local application, and send messages to the application)

Source: Internet
Author: User

Development instructions

1. browser plug-in implementation
Manifest. JSON file content. This file is required for each chrome plug-in, which records the key information of the plug-in.

{    "name" : "callapp",    "version" : "1.0.1",    "description" : "call local application",    "background" : { "scripts": ["background.js"] },    "icons": {        "16": "16.png",        "128": "128.png"    },    "permissions" : [    "nativeMessaging",    "contextMenus",    "tabs"    ],    "minimum_chrome_version" : "6.0.0.0",    "manifest_version": 2}

Because you need to add options in the context menu, add "nativemessaging" and "contextmenus" to permissions. You need to communicate with the local program and add the "nativemessaging" item.

The background. js required by maniffest. JSON

//Author: jyx//Date: 2014.10.11//Description: This is a javaScript file use for handle contextMenus action//When click the contextMenus, it will sent the infomation to native app//connect to native appvar port = null;var nativeHostName = "com.ctrip.ops.mysql.callapp";//chrome与本地程序通信的桥梁,根据该名称进行配置项的寻找。windows下在注册表HKEY_CURRENT_USER\Software\Google\Chrome\NativeMessagingHosts内寻找,linux下在目录/etc/opt/chrome/native-messaging-hosts/寻找该名称的json文件(本例子为com.ctrip.ops.mysql.callapp.json)//onNativeDisconnectfunction onDisconnected(){    //alert("连接到FastDownload服务失败: " + chrome.runtime.lastError.message);    port = null;}//connect to native host and get the communicatetion portfunction connectToNativeHost(){    port = chrome.runtime.connectNative(nativeHostName);//根据配置文件连接到本地程序    port.onDisconnect.addListener(onDisconnected);}//调用connectToNativeHost函数连接到本地程序,完成后使用port.postMessage函数将纤细传递给应用程序//将信息写入I/O流与本地程序通信function getClickHandler() {      return function(info, tab) {         connectToNativeHost();        port.postMessage(info.linkUrl);      };};//在浏览器启动时即创建右键菜单,在页面链接上右击鼠标会显示该菜单,当点击"start program"的时候就会调用getClickHandler()函数处理 chrome.contextMenus.create({    "title" : "start program",    "type" : "normal",     "id": "callapp",    "contexts" : ["link"],     "enabled": true,    "onclick" : getClickHandler()});

The nativehostname in the program requires special instructions. It is through this variable that the chrome plug-in finds the configuration to call the local program.
The tutorial environment in this article is Windows 7. Create the corresponding item in HKEY_CURRENT_USER \ Software \ google \ chrome \ nativemessaginghosts of the registry. The exported content of the Registry is as follows: Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Google\Chrome\NativeMessagingHosts\com.ctrip.ops.mysql.callapp]@="D:\\temp\\chromeExtension\\callapp\\callapp.json"

Linux reference http://match-yang.blog.163.com/blog/static/2109902542014319103739996/

2. browser call Configuration

Callapp. JSON
The file path is saved in the HKEY_CURRENT_USER \ Software \ google \ chrome \ nativemessaginghosts \ com. Ctrip. Ops. MySQL. callapp entry of the Registry.

{    "name": "com.ctrip.ops.mysql.callapp",    "description": "Chrome call native app and sent message to app.",    "path": "C:\\MyApp.exe",    "type": "stdio",    "allowed_origins": [        "chrome-extension://imcfacgnnkhdheiajocckejfmeiokgol/"    ]}

3. Local Application Example

Use C # development, C ++ reference http://match-yang.blog.163.com/blog/static/2109902542014319103739996/

using System;using System.IO;namespace ConsoleApplication8{    class Program    {        static void Main(string[] args)        {            log2file("--------------------program start at " + DateTime.Now.ToString() + "--------------------");            if (args.Length != 0)            {                for (int i = 0; i < args.Length; i++)                    log2file("arg " + i.ToString() + args[i]);                string chromeMessage = OpenStandardStreamIn();                log2file("--------------------My application starts with Chrome Extension message: " + chromeMessage + "---------------------------------");            }            log2file("--------------------program end at " + DateTime.Now.ToString() + "--------------------");        }        static void log2file(string s)        {            FileStream fs = new FileStream(@"c:\test.log", FileMode.Append);            StreamWriter sw = new StreamWriter(fs);            sw.WriteLine(s);            sw.Close();            fs.Close();        }        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 = "";            for (int i = 0; i < length; i++)            {                input += (char)stdin.ReadByte();            }            return input;        }    }}

Note:
The background. JS Code has made some non-framework modifications based on the following link, mainly comments. However, I modified author.
Http://match-yang.blog.163.com/blog/static/2109902542014319103739996/

Refer:
Explains how to add
Http://my.oschina.net/ruben/blog/92813
Call Local Applications
Http://match-yang.blog.163.com/blog/static/2109902542014319103739996/
Http://blog.csdn.net/talking12391239/article/details/38498557 #

 

Compile the chrome plug-in, call the local application, and send messages to the application)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.