Неопределенный Json-ответ от бэкенда django на оповещение расширения chrome
Здравствуйте, я пытаюсь вернуть json файл в расширение chrome, чтобы показать его пользователю, запрос идет на сервер без проблем, fetched url также работает и возвращает json файл, когда я пробую напрямую, но расширение chrome показывает сообщение "undefined" в оповещении вместо json файла.
возвращаемый json файл имеет такой тип : JsonResponse(data)
data - это dict[str, Union[str, list]],
еще один вопрос, как я могу показать json файл во всплывающей html странице?
к моему коду расширения для хрома:
Manifest.json:
{
"name": "Price Comparator",
"description": "Compare prices across other websites!",
"version": "1.0",
"manifest_version": 3,
"background": {
"service_worker": "background.js"
},
"icons": {
"16": "images/logo16.png",
"48": "images/logo48.png",
"128": "images/logo128.png"
},
"action": {
"default_icon": "images/logo16.png",
"default_popup": "popup.html"
},
"host_permissions": ["XXX*"],
"permissions": [
"XXX",
"http://127.0.0.1:8000/",
"storage",
"activeTab",
"scripting",
"tabs",
"background",
"identity",
"notifications"
],
"content_scripts": [
{
"matches": [
"https://www.google.com/*"
],
"css": [
"main.css"
]
}
]
}
popup.js:
$(function(){
$('#keywordsubmit').click(function(){
var search_topic = $('#keyword').val();
if (search_topic){
chrome.runtime.sendMessage(
{topic: search_topic},
function(response) {
result = response.farewell;
alert(result.summary);
var notifOptions = {
type: "basic",
iconUrl: "icon48.png",
title: "WikiPedia Summary For Your Result",
};
chrome.notifications.create('WikiNotif', notifOptions);
});
}
$('#keyword').val('');
});
});
Background.js:
chrome.runtime.onInstalled.addListener(() => { console.log('Extension is running!'); });
chrome.tabs.onActivated.addListener(function (activeInfo) {
chrome.tabs.get(activeInfo.tabId, function (tab) {
y = tab.url;
console.log("you are here: " + tab.title);
});
});
chrome.tabs.onUpdated.addListener((tabId, change, tab) => {
if (tab.active && change.url) {
console.log("you are here: " + change.url);
}
});
var serverhost = 'XXXX';
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
var url = serverhost + '/wiki/get_wiki_summary/?topic=' + encodeURIComponent(request.topic);
console.log(url);
//var url = "http://127.0.0.1:8000/wiki/get_wiki_summary/?topic=%22COVID19%22"
fetch(url)
.then(response => response.json())
.then(response => sendResponse({ farewell: response }))
.catch(error => console.log(error))
return true; // Will respond asynchronously.
});
Вся проблема в коде Python.
В JavaScript вы хотите отобразить summary
alert(result.summary);
и в Python в parsefile()
вы создаете
data = {
'summary': str(listimage.get("src")),
'raw': 'Successful',
}
но позже вы посылаете Product
вместо data
return Product
так что ваш
data = parsefile()
return JsonResponse(data)
means
return JsonResponse(Product)
вместо
return JsonResponse({
'summary': str(listimage.get("src")),
'raw': 'Successful',
})
В parsefile()
вы должны сделать
return data
или
Product['summary'] = str(listimage.get("src"))
Product['raw'] = 'Successful'
return Product