Skip to content

Commit

Permalink
fix CORS for preview popups
Browse files Browse the repository at this point in the history
  • Loading branch information
zeozeozeo committed Jul 10, 2024
1 parent 70a36c6 commit d373c10
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ function timeSince(date) {

const DB_URL = "https://zeozeozeo.github.io/clickpack-db/db.json";

// https://github.com/zeozeozeo/clickpack-db/raw/main/out/ABEST.zip -> https://zeozeozeo.github.io/clickpack-db/out/ABEST.zip
function fixupOrigin(url) {
const BAD_PREFIX = "https://github.com/zeozeozeo/clickpack-db/raw/main/out/";
const GOOD_PREFIX = "https://zeozeozeo.github.io/clickpack-db/out/";
if (url.startsWith(BAD_PREFIX)) {
return GOOD_PREFIX + url.substring(BAD_PREFIX.length);
} else {
return url; // shouldn't happen, but just in case
}
}

async function loadClickpacks() {
try {
const response = await fetch(DB_URL);
Expand Down Expand Up @@ -105,7 +116,7 @@ async function loadClickpacks() {

const cell2 = document.createElement("td");
const downloadButton = document.createElement("a");
downloadButton.href = clickpack.url;
downloadButton.href = fixupOrigin(clickpack.url);
downloadButton.className = "button-3 tooltip";
downloadButton.setAttribute("role", "button");
downloadButton.textContent = "Download";
Expand All @@ -118,7 +129,9 @@ async function loadClickpacks() {
tryButton.className = "button-4";
tryButton.setAttribute("role", "button");
tryButton.textContent = "Preview";
tryButton.addEventListener("click", () => tryPopup(clickpack.url));
tryButton.addEventListener("click", () =>
tryPopup(fixupOrigin(clickpack.url))
);

cell2.appendChild(downloadButton);
cell2.appendChild(tryButton);
Expand Down

0 comments on commit d373c10

Please sign in to comment.