Skip to content

armgit / mwmbl

Public Imported from GitHub
0

An open source, non-profit web search engine

Set up a listener to refresh htmx; send the csrf token from the index, not the results returned by the extension

Daoud Clarke75ca46dae5e9
diff --git a/devdata/index-v2.tinysearch b/devdata/index-v2.tinysearch
index 321dd37..77813c8 100644
Binary files a/devdata/index-v2.tinysearch and b/devdata/index-v2.tinysearch differ
diff --git a/front-end/package-lock.json b/front-end/package-lock.json
index f6c979a..8b79aaa 100644
--- a/front-end/package-lock.json
+++ b/front-end/package-lock.json
@@ -7,6 +7,7 @@
"name": "front-end",
"dependencies": {
"chart.js": "^4.4.0",
+ "htmx.org": "^1.9.9",
"sortablejs": "^1.15.0"
},
"devDependencies": {
@@ -574,6 +575,11 @@
"node": ">= 0.4.0"
}
},
+ "node_modules/htmx.org": {
+ "version": "1.9.9",
+ "resolved": "https://registry.npmjs.org/htmx.org/-/htmx.org-1.9.9.tgz",
+ "integrity": "sha512-PDEZU1me7UGLzQk98LyfLvwFgdtn9mrCVMmAxv1/UjshUnxsc+rouu+Ot2QfFZxsY4mBCoOed5nK7m9Nj2Tu7g=="
+ },
"node_modules/is-core-module": {
"version": "2.11.0",
"resolved": "https://registry.npmjs.org/is-core-module/-/is-core-module-2.11.0.tgz",
@@ -1122,6 +1128,11 @@
"function-bind": "^1.1.1"
}
},
+ "htmx.org": {
+ "version": "1.9.9",
+ "resolved": "https://registry.npmjs.org/htmx.org/-/htmx.org-1.9.9.tgz",
+ "integrity": "sha512-PDEZU1me7UGLzQk98LyfLvwFgdtn9mrCVMmAxv1/UjshUnxsc+rouu+Ot2QfFZxsY4mBCoOed5nK7m9Nj2Tu7g=="
+ },
"is-core-module": {
"version": "2.11.0",
"resolved": "https://registry.npmjs.org/is-core-module/-/is-core-module-2.11.0.tgz",
diff --git a/front-end/package.json b/front-end/package.json
index 2343e5a..d811c48 100644
--- a/front-end/package.json
+++ b/front-end/package.json
@@ -14,6 +14,7 @@
},
"dependencies": {
"chart.js": "^4.4.0",
+ "htmx.org": "^1.9.9",
"sortablejs": "^1.15.0"
}
}
diff --git a/front-end/src/components/molecules/result.js b/front-end/src/components/molecules/result.js
index 9ab38ee..287b456 100644
--- a/front-end/src/components/molecules/result.js
+++ b/front-end/src/components/molecules/result.js
@@ -1,5 +1,4 @@
import define from '../../utils/define.js';
-import escapeString from '../../utils/escapeString.js';
import { globalBus } from '../../utils/events.js';
@@ -33,13 +32,4 @@ export default define('result', class extends HTMLLIElement {
}
})
}
-
- __handleBold(input) {
- let text = '';
- for (const part of input) {
- if (part.is_bold) text += `<strong>${escapeString(part.value)}</strong>`;
- else text += escapeString(part.value);
- }
- return text;
- }
}, { extends: 'li' });
\ No newline at end of file
diff --git a/front-end/src/index.js b/front-end/src/index.js
index 41b9f4f..04b4b11 100644
--- a/front-end/src/index.js
+++ b/front-end/src/index.js
@@ -6,12 +6,14 @@
* util or component files instead.
*/
import 'vite/modulepreload-polyfill';
+import {setupResultsLoadedListener} from "./utils/events.js";
// Waiting for top-level await to be better supported.
(async () => {
// Check if a suggestion redirect is needed.
const { redirectToSuggestions } = await import("./utils/suggestions.js");
const redirected = redirectToSuggestions();
+ setupResultsLoadedListener();
if (!redirected) {
// Load components only after redirects are checked.
diff --git a/front-end/src/utils/debounce.js b/front-end/src/utils/debounce.js
deleted file mode 100644
index 1636257..0000000
--- a/front-end/src/utils/debounce.js
+++ /dev/null
@@ -1,13 +0,0 @@
-/**
- * A debounce function to reduce input spam
- * @param {*} callback Function that will be called
- * @param {*} timeout Minimum amount of time between calls
- * @returns The debounced function
- */
-export default (callback, timeout = 100) => {
- let timer;
- return (...args) => {
- clearTimeout(timer);
- timer = setTimeout(() => { callback.apply(this, args); }, timeout);
- };
-}
diff --git a/front-end/src/utils/escapeString.js b/front-end/src/utils/escapeString.js
deleted file mode 100644
index f5a7e85..0000000
--- a/front-end/src/utils/escapeString.js
+++ /dev/null
@@ -1,10 +0,0 @@
-/**
- * Escapes string with HTML Characters Codes.
- * @param {string} input String to escape
- * @returns {string}
- */
-export default (input) => {
- return String(input).replace(/[^\w. ]/gi, (character) => {
- return `&#${character.charCodeAt(0)};`;
- });
-}
\ No newline at end of file
diff --git a/front-end/src/utils/events.js b/front-end/src/utils/events.js
index a7d5435..7bd7e9f 100644
--- a/front-end/src/utils/events.js
+++ b/front-end/src/utils/events.js
@@ -4,6 +4,8 @@
* It is simply a trick using a div element
* to carry events.
*/
+import htmx from 'htmx.org';
+
class Bus {
constructor() {
this.element = document.createElement('div');
@@ -24,7 +26,18 @@ class Bus {
* */
const globalBus = new Bus();
+const setupResultsLoadedListener = () => {
+ window.addEventListener('message', e => {
+ console.log("Message event", e);
+ if (e.data === 'results-loaded') {
+ htmx.process(document.querySelector('.results'));
+ }
+ });
+}
+
+
export {
Bus,
globalBus,
+ setupResultsLoadedListener,
}
\ No newline at end of file
diff --git a/mwmbl/templates/home.html b/mwmbl/templates/home.html
index 9ab0243..d848156 100644
--- a/mwmbl/templates/home.html
+++ b/mwmbl/templates/home.html
@@ -4,9 +4,8 @@
<form class="modal-content"
hx-post="/app/add/"
hx-target=".main"
- hx-include=".result"
- hx-vals='{"query": "{{ query }}"{% if curation %}, "curation_id": {{ curation.id }}{% endif %}}'
- hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'>
+ hx-include=".result,[name='csrfmiddlewaretoken']"
+ hx-vals='{"query": "{{ query }}"{% if curation %}, "curation_id": {{ curation.id }}{% endif %}}'>
<span class="close">&times;</span>
<label>
Enter a URL: <input class="add-result" name="new_url" placeholder="https://something.org">
@@ -21,8 +20,8 @@
id="revert-button"
hx-post="/app/revert-current/"
hx-target=".main"
- hx-vals='{"curation_id": {{ curation.id }}}'
- hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'>↺ Revert {{ curation.num_changes }} change{{ curation.num_changes|pluralize:"s" }}</button>
+ hx-include="[name='csrfmiddlewaretoken']"
+ hx-vals='{"curation_id": {{ curation.id }}}'>↺ Revert {{ curation.num_changes }} change{{ curation.num_changes|pluralize:"s" }}</button>
{% else %}
<button class="disabled-button" id="revert-button">↺ Revert</button>
{% endif %}
diff --git a/mwmbl/templates/index.html b/mwmbl/templates/index.html
index d7f3730..974fce3 100644
--- a/mwmbl/templates/index.html
+++ b/mwmbl/templates/index.html
@@ -43,6 +43,7 @@
</head>
<body>
+ {% csrf_token %}
<mwmbl-app></mwmbl-app>
<header class="search-menu compact">
<a href="/" class="branding">
diff --git a/mwmbl/templates/result.html b/mwmbl/templates/result.html
index e1dea4b..7d4f49d 100644
--- a/mwmbl/templates/result.html
+++ b/mwmbl/templates/result.html
@@ -17,9 +17,8 @@
<button class="button curate-approve{% if result.state >= 7 %} validated{% endif %}"
hx-post="/app/approve/"
hx-target=".main"
- hx-include=".result"
- hx-vals='{"approve_url": "{{ result.url }}", "query": "{{ query }}"{% if curation %}, "curation_id": {{ curation.id }}{% endif %}}'
- hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'>✓</button>
+ hx-include=".result,[name='csrfmiddlewaretoken']"
+ hx-vals='{"approve_url": "{{ result.url }}", "query": "{{ query }}"{% if curation %}, "curation_id": {{ curation.id }}{% endif %}}'>✓</button>
</div>
</div>
</li>