You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过PowerShell请求向Chrome扩展发送消息并解决连接失败问题

解决PowerShell与Chrome扩展的通信问题

你直接用Invoke-WebRequest发送POST请求到127.0.0.1的方式是错误的——Chrome扩展并不监听HTTP端口,它的外部通信依赖Chrome原生的消息机制,而非HTTP协议。下面提供两种可行的解决方案:


方案一:Native Messaging(推荐,原生支持)

这是Chrome官方支持的程序与扩展通信的方式,适用于需要稳定双向通信的场景。

1. 调整扩展配置(manifest.json)

确保添加nativeMessaging权限,并正确配置externally_connectable:

{
  "manifest_version": 3,
  "name": "你的扩展名称",
  "version": "1.0",
  "permissions": ["nativeMessaging"],
  "externally_connectable": {
    "ids": ["你的扩展ID"],
    "matches": ["chrome-extension://你的扩展ID/*"]
  },
  "background": {
    "service_worker": "background.js"
  }
}

2. 修改扩展消息监听逻辑(background.js)

外部程序的消息需要用onMessageExternal监听,而非普通的onMessage:

chrome.runtime.onMessageExternal.addListener((message, sender, sendResponse) => {
  // 处理收到的消息,比如将'test'转为'processed test'
  const result = `processed ${message}`;
  sendResponse(result);
  // 若存在异步操作,需返回true保持sendResponse可用
  return true;
});

3. 创建Native Messaging主机配置文件

新建JSON文件(例如com.my.extension.host.json),内容如下:

{
  "name": "com.my.extension.host",
  "description": "扩展通信的Native主机",
  "path": "C:\\你的脚本路径\\message_handler.ps1",
  "type": "stdio",
  "allowed_origins": ["chrome-extension://你的扩展ID/"]
}

4. 注册Native主机到注册表

在Windows注册表中创建对应项:

  • 系统级:HKEY_LOCAL_MACHINE\Software\Google\Chrome\NativeMessagingHosts\com.my.extension.host
  • 用户级:HKEY_CURRENT_USER\Software\Google\Chrome\NativeMessagingHosts\com.my.extension.host
    将默认值设为上述JSON配置文件的绝对路径。

5. PowerShell通信脚本

Native Messaging要求消息以「4字节小端序长度 + JSON字符串」的格式传输,脚本示例:

$extensionId = "你的扩展ID"
$sendMsg = "test"

# 格式化消息为Native Messaging要求的格式
$jsonMsg = ConvertTo-Json $sendMsg -Compress
$byteLength = [System.Text.Encoding]::UTF8.GetByteCount($jsonMsg)
$lengthBytes = [System.BitConverter]::GetBytes($byteLength)
if (-not [System.BitConverter]::IsLittleEndian) { $lengthBytes = $lengthBytes[::-1] }

# 启动Chrome Native Messaging进程
$process = Start-Process -FilePath "chrome.exe" -ArgumentList "--native-messaging-host=com.my.extension.host $extensionId" -NoNewWindow -PassThru -RedirectStandardInput $true -RedirectStandardOutput $true

# 发送消息
$process.StandardInput.BaseStream.Write($lengthBytes, 0, 4)
$process.StandardInput.Write($jsonMsg)
$process.StandardInput.Flush()

# 读取扩展返回的结果
$respLenBytes = New-Object byte[] 4
$process.StandardOutput.BaseStream.Read($respLenBytes, 0, 4) | Out-Null
$respLength = [System.BitConverter]::ToInt32($respLenBytes, 0)

$respBytes = New-Object byte[] $respLength
$process.StandardOutput.BaseStream.Read($respBytes, 0, $respLength) | Out-Null
$response = [System.Text.Encoding]::UTF8.GetString($respBytes)

Write-Host "扩展返回结果:$response"
$process.Close()

方案二:本地网页中间层(快速测试用)

适合临时调试,通过本地网页作为桥梁,PowerShell调用网页,网页再与扩展通信。

1. 调整扩展的externally_connectable

允许本地网页的来源:

"externally_connectable": {
  "matches": ["file:///*", "http://localhost:*/*"]
}

2. 创建本地网页(message_bridge.html)

<!DOCTYPE html>
<html>
<body>
<script>
const urlParams = new URLSearchParams(window.location.search);
const msg = urlParams.get('msg');

// 发送消息给扩展并接收结果
chrome.runtime.sendMessage('你的扩展ID', msg, (response) => {
  document.body.textContent = response;
  // 将结果写入本地文件供PowerShell读取
  fetch('result.txt', { method: 'POST', body: response });
  window.close();
});
</script>
</body>
</html>

3. PowerShell调用脚本

$htmlPath = "C:\\你的网页路径\\message_bridge.html"
$sendMsg = "test"

# 启动Chrome打开网页并传递消息
Start-Process -FilePath "chrome.exe" -ArgumentList "--app=$htmlPath?msg=$sendMsg"

# 等待处理完成并读取结果
Start-Sleep -Seconds 2
$response = Get-Content "C:\\你的网页路径\\result.txt"
Write-Host "扩展返回结果:$response"

常见问题排查

  • 确认扩展ID正确:在Chrome扩展管理页面开启开发者模式,查看扩展ID。
  • 监听事件正确:外部消息必须用chrome.runtime.onMessageExternal,而非onMessage。
  • Native Messaging格式正确:必须先发送4字节小端序长度,再发送JSON字符串,否则Chrome无法解析。

内容的提问来源于stack exchange,提问作者sYsTeM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 07:35:11