Skip to content

Commit

Permalink
fix: situations where there is no 'try again' button
Browse files Browse the repository at this point in the history
  • Loading branch information
liady committed Dec 19, 2022
1 parent 72f26e1 commit 6b9be9b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
57 changes: 50 additions & 7 deletions src/content_script.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const buttonOuterHTMLFallback = `<button class="btn flex justify-center gap-2 btn-neutral" id="download-png-button">Try Again</button>`;
async function init() {
if (window.buttonsInterval) {
clearInterval(window.buttonsInterval);
Expand All @@ -7,19 +8,58 @@ async function init() {
if (!actionsArea) {
return;
}
const buttons = actionsArea.querySelectorAll("button");
const hasTryAgainButton = Array.from(buttons).some((button) => {
return !button.id?.includes("download");
});
if (hasTryAgainButton && buttons.length === 1) {
const TryAgainButton = actionsArea.querySelector("button");
if (shouldAddButtons(actionsArea)) {
let TryAgainButton = actionsArea.querySelector("button");
if (!TryAgainButton) {
const parentNode = document.createElement("div");
parentNode.innerHTML = buttonOuterHTMLFallback;
TryAgainButton = parentNode.querySelector("button");
}
addActionsButtons(actionsArea, TryAgainButton);
} else if (!hasTryAgainButton) {
} else if(shouldRemoveButtons()){
removeButtons();
}
}, 200);
}

function shouldRemoveButtons() {
const isOpenScreen = document.querySelector("h1.text-4xl");
if(isOpenScreen){
return true;
}
const inConversation = document.querySelector("form button>div");
if(inConversation){
return true;
}
return false;
}

function shouldAddButtons(actionsArea) {
// first, check if there's a "Try Again" button and no other buttons
const buttons = actionsArea.querySelectorAll("button");
const hasTryAgainButton = Array.from(buttons).some((button) => {
return !button.id?.includes("download");
});
if (hasTryAgainButton && buttons.length === 1) {
return true;
}

// otherwise, check if open screen is not visible
const isOpenScreen = document.querySelector("h1.text-4xl");
if (isOpenScreen) {
return false;
}

// check if the conversation is finished and there are no share buttons
const finishedConversation = document.querySelector("form button>svg");
const hasShareButtons = actionsArea.querySelectorAll("button[share-ext]");
if (finishedConversation && !hasShareButtons.length) {
return true;
}

return false;
}

const Format = {
PNG: "png",
PDF: "pdf",
Expand All @@ -43,20 +83,23 @@ function removeButtons() {
function addActionsButtons(actionsArea, TryAgainButton) {
const downloadButton = TryAgainButton.cloneNode(true);
downloadButton.id = "download-png-button";
downloadButton.setAttribute("share-ext", "true");
downloadButton.innerText = "Generate PNG";
downloadButton.onclick = () => {
downloadThread();
};
actionsArea.appendChild(downloadButton);
const downloadPdfButton = TryAgainButton.cloneNode(true);
downloadPdfButton.id = "download-pdf-button";
downloadButton.setAttribute("share-ext", "true");
downloadPdfButton.innerText = "Download PDF";
downloadPdfButton.onclick = () => {
downloadThread({ as: Format.PDF });
};
actionsArea.appendChild(downloadPdfButton);
const exportHtml = TryAgainButton.cloneNode(true);
exportHtml.id = "download-html-button";
downloadButton.setAttribute("share-ext", "true");
exportHtml.innerText = "Share Link";
exportHtml.onclick = () => {
sendRequest();
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 3,
"name": "ChatGPT Download",
"description": "Download a PNG or PDF of your chat with ChatGPT",
"version": "1.3.3",
"version": "1.3.4",
"author": "Liad Yosef",
"icons": {
"48": "icons/48.png"
Expand Down

0 comments on commit 6b9be9b

Please sign in to comment.