Ja pripojenie content.js s background.js k tomu 2 rôzne úlohy: aplikovať miestneho HTML a načítanie údajov z inej webovej stránky.
V súčasnosti, createContainer()
začne po fetchweb()
je hotovo a nie som si istý, prečo (prečo potrebujem createContainer()
ak chcete spustiť prvý). Snažil som sa transformovať obe funkcie do Sľub, ale stále rovnaký výsledok
Content.js
function createContainer1() {
// call html file container
chrome.runtime.sendMessage({ cmd: "read_cont1" }, function (html) {
$("#container1").html(html);
});
// more code
}
function fetchWeb() {
chrome.runtime.sendMessage(
{ cmd: "send_url", url: window.location.href},
function (response) {
console.log(JSON.stringify(response));
}
);
}
createContainer1()
fetchWeb()
background.js
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.cmd == "read_cont1") {
$.ajax({
url: chrome.extension.getURL("container1.html"),
dataType: "html",
success: sendResponse,
});
return true;
} else if (request.cmd == "send_url") {
sendResponse({ review_url: fetchData(request.url) });
return true;
}
});