web search done right
cannot use alpine when having strict CSP headers. User security > developer convenience
Mikkel Denker912dcc5a8e92
diff --git a/frontend/package.json b/frontend/package.jsonindex 5fca07f1..d91f140c 100644--- a/frontend/package.json+++ b/frontend/package.json@@ -11,9 +11,6 @@"prettier": "prettier -w . --cache"},"dependencies": {- "@alpinejs/persist": "^3.11.1",- "@types/alpinejs": "^3.7.1",- "alpinejs": "^3.11.1","astro": "^2.0.2","astro-icon": "^0.8.0"},diff --git a/frontend/public/js/optics_settings.js b/frontend/public/js/optics_settings.jsnew file mode 100644index 00000000..1b26bf15--- /dev/null+++ b/frontend/public/js/optics_settings.js@@ -0,0 +1,71 @@+const nameInput = document.getElementById("name");+const description = document.getElementById("description");+const url = document.getElementById("url");+const addBtn = document.getElementById("add-btn");+let deleteBtn = document.querySelectorAll(".delete-btn");+const originalOpticsList = document.getElementById("optics-list").cloneNode(true);++function updateDeleteButtons() {+ deleteBtn = document.querySelectorAll(".delete-btn");++ for (const btn of deleteBtn) {+ btn.addEventListener("click", (e) => {+ const index = e.target.dataset.opticIndex;+ optics.splice(index, 1);+ saveOptics();+ loadOptics();+ });+ }+}+++addBtn.addEventListener('click', (_) => {+ if (nameInput.value != '' && url.value != '') {+ optics.push({ name: nameInput.value, description: description.value, url: url.value });+ nameInput.value = url.value = description.value = '';++ saveOptics();+ loadOptics();+ }+});++// {"name":string, "description":string, "url":string}[]+let optics = [];++loadOptics();++function loadOptics() {+ optics = JSON.parse(+ window.localStorage.getItem("optics") || "[]"+ );++ const opticsList = originalOpticsList.cloneNode(true);+ for (let i = 0; i < optics.length; i++) {+ const item = optics[i];+ const div = document.createElement("div");+ div.classList.add("flex");+ div.innerHTML = `+ <div style="width: 2.5rem;">+ <img+ src="/images/delete.svg"+ class="delete-btn"+ data-optic-index="${i}"+ />+ </div>+ <div class="col-data mr-5">${item.name}</div>+ <div class="col-data mr-5">${item.description}</div>+ <div class="col-data">+ <a href="${item.url}">Source</a>+ </div>+ `;+ opticsList.appendChild(div);+ }++ document.getElementById("optics-list").replaceWith(opticsList);++ updateDeleteButtons();+}++function saveOptics() {+ window.localStorage.setItem("optics", JSON.stringify(optics));+}\ No newline at end of filediff --git a/frontend/public/js/privacy_settings.js b/frontend/public/js/privacy_settings.jsnew file mode 100644index 00000000..217068ba--- /dev/null+++ b/frontend/public/js/privacy_settings.js@@ -0,0 +1,16 @@+const allowStatsBox = document.getElementById("allowStats");+let allowStats = window.localStorage.getItem("allowStats");++if (allowStats === null) {+ allowStats = "true";+}++allowStatsBox.checked = allowStats === "true";++allowStatsBox.addEventListener("change", () => {+ if (allowStatsBox.checked) {+ window.localStorage.setItem("allowStats", "true");+ } else {+ window.localStorage.setItem("allowStats", "false");+ }+});\ No newline at end of filediff --git a/frontend/public/js/search.js b/frontend/public/js/search.jsindex b7b42c7c..0892324f 100644--- a/frontend/public/js/search.js+++ b/frontend/public/js/search.js@@ -23,7 +23,12 @@ alertCrosses.forEach((cross) => {var searchParams = new URLSearchParams(window.location.search);var query = searchParams.get("q");-const allowStats = window.localStorage.getItem("allowStats") === "true";+let allowStats = window.localStorage.getItem("allowStats") === "true";++if (allowStats === null) {+ allowStats = "true";+}+var qid = null;diff --git a/frontend/public/js/search_on_optics_change.js b/frontend/public/js/search_on_optics_change.jsindex ade49289..a81580de 100644--- a/frontend/public/js/search_on_optics_change.js+++ b/frontend/public/js/search_on_optics_change.js@@ -1,5 +1,3 @@-if (search_on_change) {- document.querySelector("#optics-selector").addEventListener("change", (_) => {- document.getElementById("searchbar-form").submit();- });-}\ No newline at end of file+document.querySelector("#optics-selector").addEventListener("change", (_) => {+ document.getElementById("searchbar-form").submit();+});\ No newline at end of filediff --git a/frontend/public/js/sites_settings.js b/frontend/public/js/sites_settings.jsindex 178f5c02..c820839a 100644--- a/frontend/public/js/sites_settings.js+++ b/frontend/public/js/sites_settings.js@@ -108,8 +108,7 @@ exportButton.addEventListener("click", () => {});function updateExportLink() {- console.log(sites);- if (sites.length == 0) {+ if (Object.keys(sites) == 0) {exportButton.href = "#";return;}diff --git a/frontend/src/alpine.ts b/frontend/src/alpine.tsdeleted file mode 100644index 1a2ccbf1..00000000--- a/frontend/src/alpine.ts+++ /dev/null@@ -1,9 +0,0 @@-import Alpine from "alpinejs";-// @ts-ignore-import persist from "@alpinejs/persist";--Alpine.plugin(persist);--// @ts-ignore-window.Alpine = Alpine;-Alpine.start();diff --git a/frontend/src/layouts/Layout.astro b/frontend/src/layouts/Layout.astroindex 6308c792..0ce0d538 100644--- a/frontend/src/layouts/Layout.astro+++ b/frontend/src/layouts/Layout.astro@@ -9,6 +9,9 @@ const { title } = Astro.props;<!DOCTYPE html><html lang="en"><head>+ <!-- This is just for dev. There is also CSP in the HTTP header from trystract.com -->+ <meta http-equiv="Content-Security-Policy" content="default-src 'self' 'unsafe-inline';img-src 'self' data:;script-src 'self';frame-ancestors 'none';">+<meta charset="UTF-8" /><meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="referrer" content="strict-origin" />diff --git a/frontend/src/pages/settings/index.astro b/frontend/src/pages/settings/index.astroindex a6dd7ffc..ef9c244f 100644--- a/frontend/src/pages/settings/index.astro+++ b/frontend/src/pages/settings/index.astro@@ -27,19 +27,18 @@ import { askama } from "../../askama";</div><divclass="mt-16"- x-data="{ items: $persist([]).as('optics'), name: '', description: '', url: '' }"><div class="flex w-full justify-between px-5 h-10">- <input id="name" type="text" placeholder="Name" x-model="name" />- <input id="url" type="text" placeholder="Url" x-model="url" />+ <input id="name" type="text" placeholder="Name" />+ <input id="url" type="text" placeholder="Url" /><button- x-on:click="if (name != '' && url != '') {items.push({ name, description, url }); name = url = description = ''}"class="w-20 rounded-full border-0 bg-brand text-sm text-white"+ id="add-btn">Add</button></div><div class="flex w-full px-5 h-10 mt-2">- <input id="description" type="text" placeholder="Description" x-model="description" />+ <input id="description" type="text" placeholder="Description" /></div><div class="mt-5"><div class="grid w-full space-y-5" id="optics-list">@@ -54,30 +53,12 @@ import { askama } from "../../askama";<div class="mr-5 w-5"><img src="/images/disabled-delete.svg" class="h-5 w-5" /></div>- <div class="flex-1 text-sm mr-5">{askama`optic.name $ {{random.words}}`}</div>- <div class="flex-1 text-sm mr-5">{askama`optic.description $ {{random.words}}`}</div>- <div class="flex-1 text-sm"><a href={askama`optic.url $ {{internet.url}}`}>Source</a></div>+ <div class="col-data mr-5">{askama`optic.name $ {{random.words}}`}</div>+ <div class="col-data mr-5">{askama`optic.description $ {{random.words}}`}</div>+ <div class="col-data"><a href={askama`optic.url $ {{internet.url}}`}>Source</a></div></div>))}- <template x-for="(item, index) in items">- <div class="flex">- <div class="w-10">- <img- src="/images/delete.svg"- class="h-5 w-5 hover:cursor-pointer"- x-on:click="items.splice(index, 1)"- />- </div>- <div class="user-optic flex-1 text-sm mr-5" x-text="item.name">- </div>- <div class="user-optic flex-1 text-sm mr-5" x-text="item.description">- </div>- <div class="user-optic flex-1 text-sm">- <a x-bind:href="item.url">Source</a>- </div>- </div>- </template></div></div></div>@@ -86,10 +67,26 @@ import { askama } from "../../askama";<style>a {- @apply underline font-medium;+ text-decoration: underline;+ font-weight: 500;+ }+</style>++<style is:inline>+ .delete-btn {+ width: 1.25rem;+ height: 1.25rem;+ }++ .delete-btn:hover {+ cursor: pointer;+ }++ .col-data {+ flex: 1 1 0%;+ font-size: 0.875rem;+ line-height: 1.25rem;}</style>-<script>- import "$/alpine";-</script>\ No newline at end of file+<script is:inline src="/js/optics_settings.js"></script>\ No newline at end of filediff --git a/frontend/src/pages/settings/privacy.astro b/frontend/src/pages/settings/privacy.astroindex 79061aa5..70e7e651 100644--- a/frontend/src/pages/settings/privacy.astro+++ b/frontend/src/pages/settings/privacy.astro@@ -33,6 +33,4 @@ import SettingsLayout from "../../layouts/Settings.astro";}</style>-<script>- import "$/alpine";-</script>\ No newline at end of file+<script is:inline src="/js/privacy_settings.js"></script>\ No newline at end of file