I'm building a chrome extension and I want to run a python script which is in my PC on click of a button from an extension (which is basically HTML). The python script uses selenium web-driver to scrape data from a website and store it in another log file.
I'm building a chrome extension and I want to run a python script which is in my PC on click of a button from an extension (which is basically HTML). The python script uses selenium web-driver to scrape data from a website and store it in another log file.
Share Improve this question edited Nov 29, 2018 at 22:02 Antu 2,3334 gold badges26 silver badges42 bronze badges asked Nov 29, 2018 at 17:02 Ayush KumarAyush Kumar 211 silver badge2 bronze badges 3- 1 See nativeMessaging. – woxxom Commented Nov 29, 2018 at 17:06
- Can u please explain how to go about it? I'm not able to prehend. – Ayush Kumar Commented Nov 29, 2018 at 17:15
- 1 Inspect the code of the demo extension and demo host app in that documentation, I think it's quite trivial. Or try finding a tutorial that you like more. – woxxom Commented Nov 29, 2018 at 17:17
2 Answers
Reset to default 7You basically use nativeMessaging. It allows you to create a munication bridge between your extension and an external process (such as python).
The way nativeMessaging works is by installing a host on your machine, and municates to and from Chrome extension through stdin and stdout. For example:
Host in Python
This is how you write your nativeMessaging host in python, I have included the full example of this from the docs, but made it easier to understand with less code.
host.py
This is basically an echo server, respects stdin and stdout, makes sure it sends it as binary stream.
#!/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
This defines the munication python host, make sure the extension guid is the guid of your extension.
{
"name": ".google.chrome.example.echo",
"description": "Chrome Native Messaging API Example Host",
"path": "host.bat",
"type": "stdio",
"allowed_origins": [
"chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"
]
}
host.bat
This runs the python executable.
@echo off
python "%~dp0/host.py" %*
install_host.bat
You run this once, to register your host in your OS.
REG ADD "HKCU\Software\Google\Chrome\NativeMessagingHosts\.google.chrome.example.echo" /ve /t REG_SZ /d "%~dp0host.json" /f
Chrome Extension
manifest.json
Add the permissions for nativeMessing
{
"permissions": [
"nativeMessaging"
]
}
munication.js
In order to connect to the python host, you need to do the following:
const hostName = ".google.chrome.example.echo";
let port = chrome.runtime.connectNative(hostName);
port.onMessage.addListener(onNativeMessage);
port.onDisconnect.addListener(onDisconnected);
To send a message to your python host, just send a json object to the port.
const message = {"text": "Hello World"};
if (port) {
port.postMessage(message);
}
To know the error when it disconnects:
function onDisconnected() {
port = null;
console.error(`Failed to connect: "${chrome.runtime.lastError.message}"`);
}
This full example is in the docs, I just renamed some stuff for clarity, available for Windows/Unix https://chromium.googlesource./chromium/src/+/master/chrome/mon/extensions/docs/examples/api/nativeMessaging
Th other option instead of using nativeMessaging is to use pyodide
or pyscript
Pyodide:
Install:
npm i pyodide
Import:
const { loadPyodide } = require("pyodide");
Use:
async function main() {
let pyodide = await loadPyodide();
// Pyodide is now ready to use...
console.log(pyodide.runPython(`
import sys
sys.version
`));
};
main();
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744419908a4573318.html
评论列表(0条)