ฉันเชื่อมต่อ content.js กับ background.js ต้องทำ 2 ต่างออกงาน:ฉีดท้องถิ่นแบบ HTML และดึงข้อมูลจากอีกหน้าเว็บที่ถูกเขียนด้วย.
ตอนนี้คน createContainer()
หลังจากเริ่ม fetchweb()
เสร็จแล้วและฉันไม่แน่ใจว่าทำไม(ฉันต้องการ createContainer()
จะวิ่งไปก่อน). ฉันพยายามจะเปลี่ยนทั้งสองเกี่ยวในสัญญาแต่ยังคงเป็นผลลัพธ์เดียวกัน
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;
}
});