1

I have a JavaScript file that is used for a google chrome extension, Also I have a Python file which has functions in it, only for the example let's imagine that in the python file we have a function like so

external.py

def name(x)
   return "your" + x

So I want to call this function through JavaScript and get the return value inside of JavaScript, so if the Python file named external.py and the JS file named index.js for example, so the code will JS be something like that

index.js

data = "external.py".name("John")     //(It doesn't have to be like that, just for the example)

I've tried using ajax but it does not work (I've tried to import jquery way too many times, but it doesn't seem to work) maybe because it's a google chrome extension. Also, I've tried to use fetch but I have no idea how to write it exactly and if it's impossible with fetch.

I am new to JS though, so go easy with me.

6haun
  • 17
  • 3
  • Please read the discussions in this post https://stackoverflow.com/q/13175510/18667225 ! – Markus Jun 02 '22 at 09:47
  • But I said I've problem with jquery, it doesn't work for me in chrome extensions, as much as i know – 6haun Jun 02 '22 at 09:58
  • Ah, sorry, let me see … does this help https://stackoverflow.com/a/21317597/18667225 ? Or you simply use xmlhttprequest without jquery like here: https://stackoverflow.com/a/8650135/18667225 – Markus Jun 02 '22 at 10:18
  • It doesn't work when I add those 2 lines https://prnt.sc/1o7PdYgaol6Y , Also I don't know if what am I doing is even possible, This python file not running in the background, at least now, so does this even possible? – 6haun Jun 02 '22 at 10:37
  • No, I don’t think that this is possible. If you can’t call a backend service as discussed in the first link I have posted, you will probably have to transcribe the python code to javascript to make it usable in a google chrome extension. Chrome won’t run python code for you. But you can wait a little bit. Perhaps someone else has a good suggestion to your question. – Markus Jun 02 '22 at 11:01
  • I've tried to web scrape now in JS instead of using Python to do that but require is not defined, any idea? I searched in google, but I don't get what they want me to do. should I install browserify or something like that? – 6haun Jun 02 '22 at 11:13
  • Well that is a differnt question. Does this help: https://stackoverflow.com/a/70884969/18667225 ? – Markus Jun 02 '22 at 12:00

1 Answers1

0

Use this code. Do ajax call in your js code. It may help you.

$.ajax({
  type: "POST",
  url: "~/pythoncode.py",
  data: { param: text}
}).done(function( o ) {
   // do something
});

Or you can ref to this link for js with python work https://pyscript.net/