Chrome扩展与PowerShell原生消息通信报错排查求助
Chrome扩展与原生PowerShell应用通信报错排查
我正在开发一款简单的Chrome扩展,流程是:从输入框获取Active Directory用户名,发送给作为原生应用的PowerShell脚本;脚本借助Active Directory模块查询用户信息,返回JSON格式的电话与邮箱。目前触发错误:Unchecked runtime.lastError: Error when communicating with the native messaging host.
已确认注册表中已添加原生应用路径,且nativeApp.json的allowed_origins字段与扩展ID完全匹配。
以下是全部代码(含关键错误修正标注):
manifest.json
{ "name": "AD Extension", "description": "Get phone and email from AD", "version": "1.0", "manifest_version": 3, "icons": {}, "permissions": [ "nativeMessaging", "activeTab" ], "host_permissions": [ "https://*/*", "http://*/*" ], "action": { "default_popup": "popup.html" }, "background": { "service_worker": "background.js" } }
popup.html
<!DOCTYPE html> <html> <head> <title>AD Extension</title> <!-- 修正:popup应引入自身逻辑脚本,而非background.js --> <script defer src="popup.js"></script> </head> <body> <label for="username">输入AD用户名:</label> <input type="text" id="username" name="username"><br> <button id="get-info">获取电话与邮箱</button> <div id="info"></div> </body> </html>
popup.js
document.addEventListener("DOMContentLoaded", function () { const button = document.getElementById("get-info"); button.addEventListener("click", function () { const username = document.getElementById("username").value; // 修正:消息参数需用大括号包裹为对象 chrome.runtime.sendMessage( {command: "get_user_info", username: username }, function (response) { const outputObj = JSON.parse(response); const phone = outputObj.phone; const email = outputObj.email; const infoDiv = document.getElementById("info"); infoDiv.innerHTML = "电话: " + phone + "<br>邮箱: " + email; } ); }); });
background.js
// 修正:移除冗余的connectNative,改为监听popup消息后调用sendNativeMessage chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { if (request.command === "get_user_info") { chrome.runtime.sendNativeMessage( "com.smm.adextension", {command: "get_user_info", username: request.username }, function(response) { sendResponse(response); } ); } return true; });
nativeapp.json
{ "name": "com.smm.adextension", "description": "AD扩展原生消息应用", "path": "./nativeApp.ps1", "type": "stdio", "allowed_origins": [ "chrome-extension://dhfgiijbgbcpjinfhhhlniaegdjpldbm/" ] }
nativeapp.ps1
# 修正:PowerShell注释用#而非// # 导入ActiveDirectory模块 Import-Module ActiveDirectory # 修正:遵循Chrome原生消息规范,先读取4字节长度前缀 $lengthBytes = New-Object byte[] 4 [Console]::In.BaseStream.Read($lengthBytes, 0, 4) | Out-Null $length = [System.BitConverter]::ToInt32($lengthBytes, 0) # 读取JSON内容 $buffer = New-Object byte[] $length [Console]::In.BaseStream.Read($buffer, 0, $length) | Out-Null $inputJson = [System.Text.Encoding]::UTF8.GetString($buffer) $request = ConvertFrom-Json $inputJson # 查询AD用户信息 $user = Get-ADUser -Identity $request.username -Properties telephoneNumber, emailAddress # 构造输出对象 $output = @{ phone = $user.telephoneNumber email = $user.emailAddress } # 转换为JSON并添加长度前缀输出 $outputJson = ConvertTo-Json $output $outputBytes = [System.Text.Encoding]::UTF8.GetBytes($outputJson) $outputLengthBytes = [System.BitConverter]::GetBytes($outputBytes.Length) [Console]::Out.BaseStream.Write($outputLengthBytes, 0, 4) [Console]::Out.BaseStream.Write($outputBytes, 0, $outputBytes.Length)
核心错误总结
- JavaScript语法错误:
sendNativeMessage的消息参数未用大括号包裹为合法对象,导致消息无法解析。 - Popup脚本引用错误:误将background.js引入popup页面,需改为引入自身逻辑脚本popup.js。
- Background逻辑冗余:同时使用
connectNative和sendNativeMessage,造成通信冲突,改为监听popup消息后统一调用sendNativeMessage。 - PowerShell脚本不符合规范:
- 使用错误的注释语法
- 未处理Chrome原生消息要求的4字节长度前缀(输入输出均需遵循该规范)
- 错误通过
$args[0]获取参数,Chrome原生消息是通过标准输入发送JSON,需解析输入内容获取用户名。
内容的提问来源于stack exchange,提问作者Dreamweaver
相关产品推荐
相关产品推荐

