├── GetMacEx.cs ├── GetMacEx.exe ├── README.md ├── background.js ├── com.yd.macaddr.nativemessage.json ├── content.js ├── host-install.bat ├── host-uninstall.bat ├── icons ├── 128-gray.png ├── 128.png ├── 16-gray.png ├── 16.png ├── 48-gray.png └── 48.png ├── mac.html └── manifest.json /GetMacEx.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Management; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace GetMacEx 10 | { 11 | class Program 12 | { 13 | static void Main(string[] args) 14 | { 15 | string chromeMessage = OpenStandardStreamIn(); 16 | Stream output = Console.OpenStandardOutput(); 17 | String mac = "{\"mac\":\""+ GetMacAddress() + "\"}"; 18 | byte[] bytes = Encoding.UTF8.GetBytes(mac); 19 | output.WriteByte((byte)((bytes.Length >> 0) & 0xFF)); 20 | output.WriteByte((byte)((bytes.Length >> 8) & 0xFF)); 21 | output.WriteByte((byte)((bytes.Length >> 16) & 0xFF)); 22 | output.WriteByte((byte)((bytes.Length >> 24) & 0xFF)); 23 | output.Write(bytes, 0, bytes.Length); 24 | output.Flush(); 25 | output.Close(); 26 | 27 | return; 28 | } 29 | 30 | private static string OpenStandardStreamIn() 31 | { 32 | //// We need to read first 4 bytes for length information 33 | Stream stdin = Console.OpenStandardInput(); 34 | int length = 0; 35 | byte[] bytes = new byte[4]; 36 | stdin.Read(bytes, 0, 4); 37 | length = System.BitConverter.ToInt32(bytes, 0); 38 | 39 | string input = ""; 40 | for (int i = 0; i < length; i++) 41 | { 42 | input += (char)stdin.ReadByte(); 43 | 44 | } 45 | 46 | return input; 47 | } 48 | 49 | public static string GetMacAddress() 50 | { 51 | try 52 | { 53 | 54 | string strMac = string.Empty; 55 | ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); 56 | ManagementObjectCollection moc = mc.GetInstances(); 57 | 58 | foreach (ManagementObject mo in moc) 59 | { 60 | if ((bool)mo["IPEnabled"] == true) 61 | { 62 | 63 | strMac = strMac + mo["MacAddress"].ToString()+";"; 64 | 65 | } 66 | } 67 | moc = null; 68 | mc = null; 69 | return strMac; 70 | } 71 | catch 72 | { 73 | return "unknown"; 74 | } 75 | } 76 | 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /GetMacEx.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpyngithub/macaddr-cem/ba1acb1d84570e74991b501ca3d9bb4cb9ac9923/GetMacEx.exe -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # macaddr-chrome-extension 2 | 3 | #### 介绍 4 | chrome 获取mac地址插件 5 | 6 | #### 软件架构 7 | 参考chrome插件开发规范 8 | 9 | 10 | #### 安装教程 11 | 12 | 1. 将插件根目录拷贝到任意磁盘目录,一旦选定切勿搬移,目录名不包含空格。 13 | 2. 修改manifest.json 中matches属性为要启用的网站域名 14 | 3. 安装chrome浏览器,打开浏览器 => 更多工具 => 扩展程序 => 开发者模式 => 加载已解压的扩展程序 => 选中插件根目录完成。 15 | 4. 查看插件id,确保com.yd.macaddr.nativemessage.json 文件中chrome-extension 的id保持一致。关闭开发者模式。 16 | 5. 点击运行host-install.bat 显示安装成功。(运行host-uninstall.bat 进行下载) 17 | 18 | #### 使用说明 19 | 20 | 1. 以上步骤完成后,在项目代码中即可从浏览器localstorage中获取mac地址。 21 | 例:var mac = localStorage.getItem("mac") 22 | -------------------------------------------------------------------------------- /background.js: -------------------------------------------------------------------------------- 1 | var macAddr =""; 2 | //监听content js 启动事件 3 | chrome.runtime.onMessage.addListener( 4 | function(request, sender, sendResponse) { 5 | if (request.type == "launch"){ 6 | sendResponse(macAddr); 7 | } 8 | return true; 9 | }); 10 | 11 | 12 | var port; 13 | function onDisconnected() { 14 | console.log(chrome.runtime.lastError); 15 | port = null; 16 | } 17 | 18 | function onNativeMessage(message) { 19 | macAddr = message.mac; 20 | } 21 | 22 | function connect() { 23 | port = chrome.runtime.connectNative('com.yd.macaddr.nativemessage'); 24 | port.onMessage.addListener(onNativeMessage); 25 | port.onDisconnect.addListener(onDisconnected); 26 | port.postMessage(null); 27 | } 28 | 29 | // 调用本地服务 30 | connect(); -------------------------------------------------------------------------------- /com.yd.macaddr.nativemessage.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "com.yd.macaddr.nativemessage", 3 | "description": "get mac address", 4 | "path": "GetMacEx.exe", 5 | "type": "stdio", 6 | "allowed_origins": [ 7 | "chrome-extension://obpbhdncofcnpddeedgpmekbilmnpgnn/" 8 | ] 9 | } -------------------------------------------------------------------------------- /content.js: -------------------------------------------------------------------------------- 1 | chrome.runtime.sendMessage({type: "launch", message: ""}, function(response) { 2 | localStorage.setItem("mac",response); 3 | }); -------------------------------------------------------------------------------- /host-install.bat: -------------------------------------------------------------------------------- 1 | :: %~dp0 is the directory containing this bat script and ends with a backslash. 2 | @echo off 3 | REG ADD "HKCU\Software\Google\Chrome\NativeMessagingHosts\com.yd.macaddr.nativemessage" /ve /t REG_SZ /d "%~dp0com.yd.macaddr.nativemessage.json" /f 4 | echo "install complete!!!" 5 | pause -------------------------------------------------------------------------------- /host-uninstall.bat: -------------------------------------------------------------------------------- 1 | :: Deletes the entry created by install_host.bat 2 | @echo off 3 | REG DELETE "HKCU\Software\Google\Chrome\NativeMessagingHosts\com.yd.macaddr.nativemessage" /f 4 | echo "uninstall complete!!!" 5 | pause -------------------------------------------------------------------------------- /icons/128-gray.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpyngithub/macaddr-cem/ba1acb1d84570e74991b501ca3d9bb4cb9ac9923/icons/128-gray.png -------------------------------------------------------------------------------- /icons/128.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpyngithub/macaddr-cem/ba1acb1d84570e74991b501ca3d9bb4cb9ac9923/icons/128.png -------------------------------------------------------------------------------- /icons/16-gray.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpyngithub/macaddr-cem/ba1acb1d84570e74991b501ca3d9bb4cb9ac9923/icons/16-gray.png -------------------------------------------------------------------------------- /icons/16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpyngithub/macaddr-cem/ba1acb1d84570e74991b501ca3d9bb4cb9ac9923/icons/16.png -------------------------------------------------------------------------------- /icons/48-gray.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpyngithub/macaddr-cem/ba1acb1d84570e74991b501ca3d9bb4cb9ac9923/icons/48-gray.png -------------------------------------------------------------------------------- /icons/48.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cpyngithub/macaddr-cem/ba1acb1d84570e74991b501ca3d9bb4cb9ac9923/icons/48.png -------------------------------------------------------------------------------- /mac.html: -------------------------------------------------------------------------------- 1 | 2 |

3 | MAC地址是 not detected 4 |

5 | -------------------------------------------------------------------------------- /manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "name" : "MacAddr", 3 | "version" : "1.0.0", 4 | "description" : "获取MAC地址", 5 | 6 | "icons": { 7 | "16": "icons/16.png", 8 | "48": "icons/48.png", 9 | "128": "icons/128.png" 10 | }, 11 | "key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwuEul0QVHQ+5q+hKDdpdDGmdxieWlF0NsCaq8PMrnLIBpt6ESI/kxdIMhxCFSykNmjAa3t0mmg/ap5T+Wbe0K7GQsqTcY4ISbu7aYOyoxguxyCjdZg9XX4yir7p4eO8waYi5UVaKB4zx/3i7/m6JAFhHcQk6JlkU/Y9hE+5jBzTEhZ1omOlrRawoT7pJnIUq+3zn9iiD3hnD7UkB76Wb6DHmI5r434txfUcxauzekjiYX6CyP/vIrLmDv/sSKXrKeAGbDXyv4WvJJKKkxtJyKWp14ow4Ij9CDabRwv9Ofz3yktrxTcO5fgLPF7IUvuQja0JISLIqp8tzw2eZrCwNswIDAQAB", 12 | "page_action": { 13 | "default_popup": "mac.html", 14 | "default_icon": { 15 | "16": "icons/16-gray.png", 16 | "48": "icons/48-gray.png", 17 | "128": "icons/128-gray.png" 18 | } 19 | }, 20 | 21 | "background" : { "scripts": ["background.js"] }, 22 | "permissions" : [ 23 | "nativeMessaging", 24 | "tabs", 25 | "http://xxx/*" 26 | ], 27 | "content_scripts": [ 28 | { 29 | "matches": ["http://localhost:8080/*"], 30 | "js": ["content.js"] 31 | } 32 | ], 33 | "minimum_chrome_version" : "6.0.0.0", 34 | "manifest_version": 2 35 | } --------------------------------------------------------------------------------