Currently, I am working on transferring URLs from an extension to a Flask app. The extension is able to access the current URL of the website. I have set up an AJAX request to connect to Flask, and the connection is successful. However, when trying to send the URL to Flask, the "test" page keeps outputting a value of "None."
When users use this extension, they enter a specific website. My goal is for the extension to automatically send the URL of the website to Flask. How can I properly format the code to return the current URL to Flask while using this extension? Ultimately, I want the test page to display the user's current URL.
main.py: Within the Flask file, there is a function called testfn() where I attempt to retrieve the request data.
app = Flask(__name__)
@app.route('/test/', methods=["GET","POST"])
def testfn():
data = request.get_json()
return render_template("index.html", **locals())
@app.after_request
def add_headers(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
return response
index2.html: In this part, I aim to display the value of the retrieved data. However, the page consistently shows a value of "None." I want to be able to show the actual URL instead of "None."
<!doctype html>
<html>
<body>
<p>{{data}}</p>
</body>
</html>
popup.js: This section involves successfully fetching the current URL of the website in the extension and sending an AJAX request.
chrome.tabs.query({active: true, lastFocusedWindow: true}, tabs => {
var link = "";
link = tabs[0].url;
link = JSON.stringify(link);
$.ajax({
type: "POST",
url: "http://localhost:5000/test/",
contentType: "application/json",
data: link,
success: function(response) {
console.log(response);
},
error: function(err) {
console.log(err);
}
});
});