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

如何实现外部脚本与Chrome扩展通信?本地按需取数方案咨询

可行的外部脚本与Chrome扩展通信方案

1. 官方推荐:Native Messaging(按需请求首选)

这是Chrome官方提供的扩展与本地应用通信的标准方式,支持双向按需交互,安全性高。

核心流程:

  • 扩展声明nativeMessaging权限,指定要通信的本地应用ID
  • 本地Python脚本作为原生应用,通过标准输入输出(stdin/stdout)与扩展交换JSON格式数据
  • 在系统中注册原生应用,让Chrome能识别并启动它

代码示例:

扩展端(background.js):

// 发送请求到本地Python脚本
function requestDataFromPython() {
  chrome.runtime.sendNativeMessage('com.your.app.id', {action: 'get_data'}, (response) => {
    if (chrome.runtime.lastError) {
      console.error(chrome.runtime.lastError);
    } else {
      console.log('收到Python返回的数据:', response);
    }
  });
}

// 按需调用(比如响应扩展内部事件或快捷键)
requestDataFromPython();

Python端(native_app.py):

import sys
import json

def read_message():
    # 读取Chrome发送的消息(前4字节是消息长度)
    raw_length = sys.stdin.read(4)
    if not raw_length:
        sys.exit(0)
    message_length = int.from_bytes(raw_length, byteorder='little')
    message = sys.stdin.read(message_length)
    return json.loads(message)

def send_message(message):
    # 发送消息给Chrome
    encoded_message = json.dumps(message).encode('utf-8')
    # 先发送消息长度(4字节小端)
    sys.stdout.write(len(encoded_message).to_bytes(4, byteorder='little'))
    sys.stdout.write(encoded_message)
    sys.stdout.flush()

def main():
    while True:
        request = read_message()
        if request.get('action') == 'get_data':
            # 这里返回扩展采集的数据(示例数据)
            response = {'data': '从Chrome扩展获取的内容', 'status': 'success'}
            send_message(response)

if __name__ == '__main__':
    main()

系统注册(以Windows为例):

在注册表HKEY_LOCAL_MACHINE\Software\Google\Chrome\NativeMessagingHosts\com.your.app.id下,新建字符串值(默认),指向你的配置文件路径。配置文件com.your.app.id.json内容:

{
  "name": "com.your.app.id",
  "description": "与Chrome扩展通信的Python脚本",
  "path": "C:\\path\\to\\native_app.py",
  "type": "stdio",
  "allowed_origins": ["chrome-extension://你的扩展ID/"]
}

2. 本地文件中间层(轻量简单场景)

适合不需要实时通信的简单场景,通过本地文件作为数据交换载体:

方案A:扩展主动写入数据,Python按需读取

  • 扩展使用chrome.fileSystemAPI请求用户授权本地文件读写权限,定时或触发事件时将采集的数据写入JSON文件
  • Python脚本直接读取该文件获取数据

方案B:Python发起请求,扩展监听文件响应

  • Python写入包含请求指令的JSON文件到指定路径
  • 扩展通过chrome.fileSystem.watch监听该文件变化,读取请求后将结果写入另一个响应文件
  • Python监听响应文件变化,读取结果

注意:需要处理文件并发读写的锁问题,避免数据冲突。


3. 本地WebSocket服务器(实时双向通信)

通过Python启动本地WebSocket服务器,扩展主动建立长连接,实现按需请求:

代码示例:

Python端(websocket_server.py):

import asyncio
import websockets
import json

async def handle_client(websocket):
    async for message in websocket:
        # 接收请求指令
        if message == 'get_chrome_data':
            # 转发请求逻辑(实际是扩展收到后返回数据)
            await websocket.send(json.dumps({'data': 'Chrome采集的内容'}))

async def main():
    async with websockets.serve(handle_client, "localhost", 8765):
        await asyncio.Future()  # 保持服务运行

asyncio.run(main())

扩展端(background.js):

const socket = new WebSocket('ws://localhost:8765');

socket.onopen = () => {
    console.log('WebSocket连接成功');
};

socket.onmessage = (event) => {
    const request = JSON.parse(event.data);
    if (request.action === 'get_data') {
        // 返回扩展采集的实时数据
        socket.send(JSON.stringify({data: '扩展采集的内容'}));
    }
};

socket.onerror = (error) => {
    console.error('WebSocket错误:', error);
};

方案对比

方案优点缺点
Native Messaging官方支持、安全、标准配置稍繁琐
本地文件中间层实现简单、无需额外依赖实时性差、需处理并发
WebSocket服务器实时双向、按需请求需处理端口占用、非官方标准

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 18:53:38