diff --git a/chrome_extension/cardboard/manifest.json b/chrome_extension/cardboard/manifest.json index f94dff13..745a71f2 100644 --- a/chrome_extension/cardboard/manifest.json +++ b/chrome_extension/cardboard/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 3, "name": "Cardboard Chrome Extension", - "version": "1.0.1", + "version": "1.0.2", "icons": { "16": "images/cardboard_bird.png", "32": "images/cardboard_bird.png", @@ -16,9 +16,9 @@ "cookies" ], "host_permissions": [ - "http://*:*/*", + "http://127.0.0.1/*", "http://localhost/*", "https://cardboard.rocks/*", "https://www.cardboard.rocks/*" ] -} +} diff --git a/chrome_extension/cardboard/popup.html b/chrome_extension/cardboard/popup.html index 03373c29..13feff96 100644 --- a/chrome_extension/cardboard/popup.html +++ b/chrome_extension/cardboard/popup.html @@ -14,6 +14,10 @@


+
+

diff --git a/chrome_extension/cardboard/popup.js b/chrome_extension/cardboard/popup.js index 3539920b..c3925baa 100644 --- a/chrome_extension/cardboard/popup.js +++ b/chrome_extension/cardboard/popup.js @@ -1,10 +1,38 @@ +// TODO: Default this to false and control this in the deployment process +const IS_PROD = true; + +const PROD_URL = "https://cardboard.rocks"; +const DEV_URL = "http://localhost:8000"; + +// TODO: Make this configurable +const HUNT_NUMBER = 10; + +let TARGET_URL = DEV_URL; +if (IS_PROD) { + TARGET_URL = PROD_URL; +} + +// Read all puzzles and filter down to those that are metas. +const hunt_puzzles = await fetch( + TARGET_URL + "/api/v1/hunts/" + HUNT_NUMBER + "/puzzles", + { + method: "GET", + } +); +const response = await hunt_puzzles.json(); +let metas = []; +for (const puzzle of response) { + if (puzzle.is_meta) { + metas.push(puzzle.name); + } +} + // There should only be one tab because there is only one currentWindow // and active tab. const tabs = await chrome.tabs.query({ active: true, currentWindow: true, }); - const template = document.getElementById("li_template"); const elements = new Set(); for (const tab of tabs) { @@ -16,21 +44,32 @@ for (const tab of tabs) { if (tab.url != undefined) { element.querySelector(".puzzle_url").value = tab.url; } + + // Add metas to dropdown + let meta_dropdown = element.querySelector(".puzzle_meta"); + for (const meta of metas) { + let option = document.createElement("option"); + option.text = meta; + option.value = meta; + option.key = meta; + meta_dropdown.add(option); + } elements.add(element); } document.querySelector("ul").append(...elements); + const button = document.querySelector("button"); button.addEventListener("click", async (e) => { e.preventDefault(); // Get Cardboard cookie const cardboard_cookie = await chrome.cookies.get({ - url: "https://cardboard.rocks", + url: TARGET_URL, name: "csrftoken", }); if (cardboard_cookie) { // Create puzzle. Puzzle name is limited to 30 characters - fetch("https://cardboard.rocks/api/v1/hunts/10/puzzles", { + fetch(TARGET_URL + "/api/v1/hunts/" + HUNT_NUMBER + "/puzzles", { method: "POST", mode: "cors", body: JSON.stringify({ @@ -38,6 +77,7 @@ button.addEventListener("click", async (e) => { is_meta: document.getElementById("is_meta").checked, name: document.getElementById("puzzle_name").value.slice(0, 30), url: document.getElementById("puzzle_url").value, + assigned_meta: document.getElementById("puzzle_meta").value, }), headers: { "X-CSRFToken": cardboard_cookie.value, @@ -48,17 +88,3 @@ button.addEventListener("click", async (e) => { console.log("No cookie found"); } }); - -// Doing a GET in order to get the metas that you can assign this new puzzles to. -/* -const hunt_puzzles = await fetch("http://localhost:8000/api/v1/hunts/3/puzzles", { - method: "GET", -}); -const response = await hunt_puzzles.json(); -console.log(response); -for (const puzzle of response) { - if (puzzle.is_meta) { - console.log(puzzle.name); - } -} -*/