I had developed a PDF saver Chrome extension for ChatGPT which takes conversation with ChatGPT and converts them into PDF files with specific formatting for paragraphs, lists, and code snippets etc. and saves them. At that time, there were no errors and it was working as expected.
But now it's not anymore and I can't seem to locate the bug despite spending hours trying to do so.
The background script is running correctly and sending a message to the content script whenever a conversation page is loaded:
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (
tab.url &&
tab.url.includes("https://chat.openai.com/c/") &&
changeInfo.status === "complete"
) {
chrome.tabs.sendMessage(tabId, {
action: "extractData"
},
(response) => {
if (chrome.runtime.lastError) {
console.log("Message sent but couldn't establish connection!")
} else {
console.log(response);
}
});
}
});
This message is being received in the content script like this:
// Extracting the conversation data
chrome.runtime.onMessage.addListener((message, sender, response) => {
if (message.action === "extractData") {
interval = setInterval(() => {
answers = extractAnswers();
if (answers.length !== 0) {
clearInterval(interval)
title = document.title;
questions = extractQuestions();
questions = questions.slice(0, answers.length)
}
}, 500)
response({ message: "Data extracted successfully!" });
}
})
I've checked and tested the functions for extracting questions and answers, they are working properly.
I get this message in the service worker:
Message sent but couldn't establish connection!
which means that the background script is sending the message.
However, I can't see any console log statements that I've put at various points inside the content script including the one at the start.
It seems like the content script is not being loaded at all. But I've defined it properly in the manifest.json file.
"content_scripts": [
{
"matches": ["https://chat.openai.com/c/*"],
"js": ["conversation.js", "pdfmake.min.js", "vfs_fonts.js"]
}
],
I'm also sending a message from the popup script to the content script to generate the pdf and then download it.
I'm getting this error in the console log of the popup:
Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist. Context popup.html Stack Trace popup.html:0 (anonymous function)
This is how I'm sending the message from the popup script:
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, { action: "generatePDF" }, (response) => {
if (response && response.reply === "pdfGenerated") {
setTimeout(() => {
displayMessage.classList.remove("message");
displayMessage.classList.add("display-hidden");
downloadPDFbutton.classList.remove("display-hidden");
downloadPDFbutton.classList.add("display-button");
}, 2000)
} else if (response && response.reply === "error") {
setTimeout (() => {
displayMessage.innerText = "Conversation wasn't loaded properly. Try again!"
}, 1500);
}
});
});
This is how I'm receiving it:
// Generating the PDF document
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "generatePDF") {
if (answers && questions && answers.length === questions.length && answers.length > 0) {
pdf = generatePDF(questions, answers, title);
console.log(pdf)
sendResponse({ reply: "pdfGenerated" });
} else {
sendResponse({ reply: "error", message: "Data was not extracted yet!" });
}
}
});
I just can't find the bug! Everything was working perfectly before but it's not anymore. Any help would be appreciated.