I has a code in background script:
const getFullImg = () => {
return new Promise((resolve) => {
let element = imagesWidgetDocument.querySelector(
".rc-imageselect-challenge"
);
let rect = element.getBoundingClientRect();
chrome.runtime.sendMessage({ action: "takeScreenshot", rect: rect },function (response) {
console.log(response)
resolve(response);
}
);
});
};
And such a script in the content:
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.action === "takeScreenshot") {
console.log('Получено')
let rect = request.rect;
chrome.tabs.captureVisibleTab({ format: "png", quality: 100 }, function(screenshotUrl) {
if (screenshotUrl) {
var img = new Image();
img.onload = function() {
var canvas = document.createElement("canvas");
canvas.width = 500;
canvas.height = 500;
let x = rect.left + window.scrollX;
let y = rect.top + window.scrollY;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, x + 110, y + 130, 500, 500, 0, 0, 500, 500);
var dataURL = canvas.toDataURL("image/png");
console.log(dataURL)
chrome.runtime.sendMessage({ action: "screenshotTaken", data: dataURL });
sendResponse(dataURL);
};
img.src = screenshotUrl;
} else {
sendResponse(null);
}
});
return true; /Message to chrome that the request is asynchronous /
}
});
Content script working right, but when it has to send a response to the message i get this error:
Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.
My manifest_version - 3
my manifest.json:
"host_permissions": [
"<all_urls>"
],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["./js/screenshot.js", "./js/popup.js"]
}
],
"action": {
"default_popup": "popup.html"
},
"permissions": ["scripting", "activeTab", "storage", "tabs"],
"background": {
"service_worker": "./js/background.js"
}
I trying to send new message from content script and get it on background, but it's not working, so i don't know how to fix that