I am trying to append the script on content script from the API response.
Following is the content.js
locationHref = window.location.href;
chrome.runtime.sendMessage({
action: locationHref,
value: false
}, function (data) {
localStorage();
console.log('contetn js ', data);
});
function localStorage() {
chrome.storage.local.get('script', (items) => {
var s = document.createElement("script");
s.type = "text/javascript";
s.innerHTML = items.script;
document.body.appendChild(s);
if (items.responseData) {
setTimeout((r) => {
loadPopUp(items.responseData); // this method is not triggering
}, 5000);
}
});
}
And following is the background.js file
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
fetchData(msg.action, sendResponse);
return true;
});
function fetchData(url, sendResponse) {
fetch("myscript.js", {
method: 'GET',
}).then(res => res.json())
.then(response => {
chrome.storage.local.set({
"script": response
}, function () {});
sendResponse(response);
}).catch(error => {
console.log(error)
});
}
And myscript.js has the following code.
function loadPopUp(data) {
document.body.insertAdjacentHTML('beforeend', `<h4 style="color:white; font-size: 25px; text-align: center; margin-top: 15px;">
${data.name} has <span style="font-weight:bold">${data.count}</span> gene count</h4>`)
}
API response has the loadPopUp() method, after creating the script tag I am trying to call the loadPopup method but it is giving undefined error.
How can I solve this problem.?