Javascript 如何在google chrome中从HTML运行python脚本?
我正在构建一个chrome扩展,我想运行一个python脚本,它在我的PC中,只需点击扩展中的一个按钮(基本上是HTML)。python脚本使用selenium web驱动程序从网站中抓取数据并将其存储在另一个日志文件中。您基本上使用的是。它允许您在扩展和外部进程(如python)之间创建通信桥梁 其工作方式是在您的机器上安装主机,并通过stdin和stdout与Chrome扩展进行通信。例如: Python中的主机 这就是用python编写主机的方式,我已经在文档中包含了完整的示例,但是用更少的代码就更容易理解了 host.py 这基本上是一个echo服务器,尊重stdin和stdout,确保它以二进制流的形式发送Javascript 如何在google chrome中从HTML运行python脚本?,javascript,python,selenium,flask,google-chrome-extension,Javascript,Python,Selenium,Flask,Google Chrome Extension,我正在构建一个chrome扩展,我想运行一个python脚本,它在我的PC中,只需点击扩展中的一个按钮(基本上是HTML)。python脚本使用selenium web驱动程序从网站中抓取数据并将其存储在另一个日志文件中。您基本上使用的是。它允许您在扩展和外部进程(如python)之间创建通信桥梁 其工作方式是在您的机器上安装主机,并通过stdin和stdout与Chrome扩展进行通信。例如: Python中的主机 这就是用python编写主机的方式,我已经在文档中包含了完整的示例,但是用更少
#!/usr/bin/env python
import struct
import sys
import os, msvcrt
# Set the I/O to O_BINARY to avoid modifications from input/output streams.
msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
# Helper function that sends a message to the webapp.
def send_message(message):
# Write message size.
sys.stdout.write(struct.pack('I', len(message)))
# Write the message itself.
sys.stdout.write(message)
sys.stdout.flush()
# Thread that reads messages from the webapp.
def read_thread_func():
message_number = 0
while 1:
# Read the message length (first 4 bytes).
text_length_bytes = sys.stdin.read(4)
if len(text_length_bytes) == 0:
sys.exit(0)
# Unpack message length as 4 byte integer.
text_length = struct.unpack('i', text_length_bytes)[0]
# Read the text (JSON object) of the message.
text = sys.stdin.read(text_length).decode('utf-8')
send_message('{"echo": %s}' % text)
def Main():
read_thread_func()
sys.exit(0)
if __name__ == '__main__':
Main()
host.json
这定义了通信python主机,请确保扩展guid是扩展的guid
{
"name": "com.google.chrome.example.echo",
"description": "Chrome Native Messaging API Example Host",
"path": "host.bat",
"type": "stdio",
"allowed_origins": [
"chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"
]
}
host.bat
这将运行python可执行文件
@echo off
python "%~dp0/host.py" %*
安装_host.bat
您只需运行一次,即可在操作系统中注册主机
REG ADD "HKCU\Software\Google\Chrome\NativeMessagingHosts\com.google.chrome.example.echo" /ve /t REG_SZ /d "%~dp0host.json" /f
铬延伸
manifest.json
添加nativeMessing
{
"permissions": [
"nativeMessaging"
]
}
communication.js
要连接到python主机,需要执行以下操作:
const hostName = "com.google.chrome.example.echo";
let port = chrome.runtime.connectNative(hostName);
port.onMessage.addListener(onNativeMessage);
port.onDisconnect.addListener(onDisconnected);
要向python主机发送消息,只需向端口发送一个json对象
const message = {"text": "Hello World"};
if (port) {
port.postMessage(message);
}
要了解断开连接时的错误,请执行以下操作:
function onDisconnected() {
port = null;
console.error(`Failed to connect: "${chrome.runtime.lastError.message}"`);
}
这个完整的例子在文档中,为了清晰起见,我刚刚重命名了一些东西,可用于基本使用的Windows/Unix。它允许您在扩展和外部进程(如python)之间创建通信桥梁
其工作方式是在您的机器上安装主机,并通过stdin和stdout与Chrome扩展进行通信。例如:
Python中的主机
这就是用python编写主机的方式,我已经在文档中包含了完整的示例,但是用更少的代码就更容易理解了
host.py
这基本上是一个echo服务器,尊重stdin和stdout,确保它以二进制流的形式发送
#!/usr/bin/env python
import struct
import sys
import os, msvcrt
# Set the I/O to O_BINARY to avoid modifications from input/output streams.
msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
# Helper function that sends a message to the webapp.
def send_message(message):
# Write message size.
sys.stdout.write(struct.pack('I', len(message)))
# Write the message itself.
sys.stdout.write(message)
sys.stdout.flush()
# Thread that reads messages from the webapp.
def read_thread_func():
message_number = 0
while 1:
# Read the message length (first 4 bytes).
text_length_bytes = sys.stdin.read(4)
if len(text_length_bytes) == 0:
sys.exit(0)
# Unpack message length as 4 byte integer.
text_length = struct.unpack('i', text_length_bytes)[0]
# Read the text (JSON object) of the message.
text = sys.stdin.read(text_length).decode('utf-8')
send_message('{"echo": %s}' % text)
def Main():
read_thread_func()
sys.exit(0)
if __name__ == '__main__':
Main()
host.json
这定义了通信python主机,请确保扩展guid是扩展的guid
{
"name": "com.google.chrome.example.echo",
"description": "Chrome Native Messaging API Example Host",
"path": "host.bat",
"type": "stdio",
"allowed_origins": [
"chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"
]
}
host.bat
这将运行python可执行文件
@echo off
python "%~dp0/host.py" %*
安装_host.bat
您只需运行一次,即可在操作系统中注册主机
REG ADD "HKCU\Software\Google\Chrome\NativeMessagingHosts\com.google.chrome.example.echo" /ve /t REG_SZ /d "%~dp0host.json" /f
铬延伸
manifest.json
添加nativeMessing
{
"permissions": [
"nativeMessaging"
]
}
communication.js
要连接到python主机,需要执行以下操作:
const hostName = "com.google.chrome.example.echo";
let port = chrome.runtime.connectNative(hostName);
port.onMessage.addListener(onNativeMessage);
port.onDisconnect.addListener(onDisconnected);
要向python主机发送消息,只需向端口发送一个json对象
const message = {"text": "Hello World"};
if (port) {
port.postMessage(message);
}
要了解断开连接时的错误,请执行以下操作:
function onDisconnected() {
port = null;
console.error(`Failed to connect: "${chrome.runtime.lastError.message}"`);
}
这个完整的例子在文档中,为了清晰起见,我刚刚重命名了一些东西,适用于Windows/Unix请参见。您能解释一下如何操作吗?我无法理解。检查文档中演示扩展和演示主机应用程序的代码,我认为这是非常琐碎的。或者试着找一个你更喜欢的教程。看。你能解释一下怎么做吗?我无法理解。检查文档中演示扩展和演示主机应用程序的代码,我认为这是非常琐碎的。或者试着找一个你更喜欢的教程。