0

I got the following problem. I created a python script a while ago that allowed me to download a youtube video. Then i thought it would be cool if I created my own firefox addon working with the script to download the videos directry from the browser. So I started out and watched some tutorials and realized i could only work with js to create an addon. The I looked for a solution to send the data from my Javascript to my python script and then download the video. I then started with an AJAX Post method which I found out would be the best for my case but it just doesnt do anything. I tried multiple things to get it to work but I dont get anything returned. Here is my code(JavaScript):

console.log("Loaded Successfully!");
function postData(jsdata) {
    $.ajax({
        type: "POST",
        url: "/receivedata",
        data: { "myData": jsdata },
        success: callbackFunc
    });
}
function callbackFunc(response) {
    console.log(response);
}
postData('Hello')
console.log("Sent data successfully");

Here is the python script:

import flask
import json
from flask import request

app = flask.Flask(__name__)

@app.route('/receivedata/', methods=["GET", "POST"])
def receivedata_():
    if request.method == "POST":
        print(request.form["myData"])
        Message = {"Message":"Hello World"}
        return Message

if __name__ == "__test__":
    app.run(debug=True)

And if you need it heres my manifest.json:

{
  "manifest_version": 2,
  "name": "Test",
  "version": "1.0",
  "description": "Test Addon!",
  "content_scripts": [
    {
      "matches": ["*://*.youtube.com/watch?*"],
      "js": ["test.js"]
    }
  ],

  "permissions": [
    "*://youtube.com/*"
  ]

}
5
  • If the flask server is running on your own computer, you need to add http://localhost to the url or the extension will send that request to the site you're visiting I guess. Also, you're using jQuery in your extension script. a) did you include it? b) you can use fetch() instead Commented Apr 29, 2021 at 14:48
  • Im sry but im new to JS and I have never used Flask before too. Can you pls send me a link or explain how I can set up a Flask Server? I added the localhost. Then how do I include JQuery? I think it already is because I do not get any errors but Im not sure. And can you maybe explain how Im able to use the fetch() method to send data from the JS Script to the Python Script. Commented Apr 29, 2021 at 16:18
  • 1
    Here's a pretty good succinct example of creating a simple Flask server and view/route and a fetch that will retrieve data from that view/route: stackoverflow.com/questions/57891275/… Commented Apr 29, 2021 at 16:45
  • You should start here then: flask.palletsprojects.com/en/1.1.x/quickstart/… Commented Apr 29, 2021 at 16:46
  • Thank you guys so much. I'll read into that and then ask if theres any existing problem. Commented Apr 29, 2021 at 17:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.