6

I'm writing a chrome extension that injects a content script into every page the user goes to. What i want to do is to get the output of a python function for some use in the content script (can't write it in javascript, since it requires raw sockets to connect to my remote SSL server).

I've read that one might use CGI and Ajax or the like, to get output from the python code into the javascript code, but i ran into 3 problems:

  1. I cannot allow hosting the python code on a local server, since it is security sensitive data that only the local computer should be able to know.

  2. Chrome demands that HTTP and HTTPS can not mix- if the user goes to an HTTPS website, i can't host the python code on a HTTP server.

  3. I don't think Chrome even supports CGI on extensions-when i try to access a local file, all it does is print out the text (the python code itself) instead of what i defined to be its output (I tried to do so using Flask). As I said in 1, I shouldn't even try this anyway, but this is just a side note.

So my question is, how do i get the output of my python functions inside a Content Script, built with javascript?

asdfguy
  • 97
  • 3
  • 7

2 Answers2

3

Native Messaging may be your answer here.

You can designate a Native Host application that an extension or app will be able to invoke (probably through a background page in case of an extension), and that can be a Python script.

In fact, the sample app for it uses a Python script.

Xan
  • 74,770
  • 16
  • 179
  • 206
  • This looks like what i was looking for! ill try it and post the results. – asdfguy Feb 12 '16 at 09:50
  • Okay, i've got it setup, but i have one last problem: the chrome sample from https://chromium.googlesource.com/chromium/src/+/master/chrome/common/extensions/docs/examples/api/nativeMessaging/host/native-messaging-example-host runs, but the web app part of the sample says "Error when communicating with the native messaging host". I've confirmed the script itself runs (if i put a code to create a messagebox inside, i see it). What's the problem? – asdfguy Feb 12 '16 at 18:05
  • See the docs, there's an extensive section on errors. In short, something is wrong with the manifest, most probably. – Xan Feb 12 '16 at 18:06
  • Actually, nevermind: "_Error when communicating with the native messaging host._ This is a very common error and indicates an incorrect implementation of the communication protocol in the native messaging host." – Xan Feb 12 '16 at 18:07
  • Exactly, but what could go wrong with the sample google themselves provided? i dont think its something about the implementation of their protocol. – asdfguy Feb 12 '16 at 18:12
  • nevermind, clean reinstall fixed the problem for some reason. Thanks for the answer :) – asdfguy Feb 14 '16 at 16:39
0

The only way to get the output of a Python script inside a content script built with Javascript is to call the file with XMLHttpRequest. As you noted, you will have to use an HTTPS connection if the page is served over HTTPS. A workaround for this is to make a call to your background script, which can then fetch the data in whichever protocol it likes, and return it to your content script.

Steve B
  • 212
  • 1
  • 8
  • And does that not require bringing up a server for accessing the script? as far as i understand, it does, as i tried it and the output was the same (the python code itself) – asdfguy Feb 11 '16 at 20:53
  • Are you sure your server, local or remote, is running python? Javascript will fetch whatever the output of the file is, but if Python is not running on that server you will see the actual code. – Steve B Feb 11 '16 at 21:01
  • http://stackoverflow.com/questions/13175510/call-python-function-from-javascript-code I used the last answer here. Didn't use a local server. Basically like i said, im trying to stay away from hosting a local server- If i must, i will, but i feel it would be weird if nobody has ever thought of integrating python with javascript directly... there must be some other solution. – asdfguy Feb 11 '16 at 22:16