From ff55a041fd506e0552d2f60f90f520a06d549728 Mon Sep 17 00:00:00 2001 From: leomcelroy Date: Fri, 20 Oct 2023 12:35:25 -0400 Subject: [PATCH] remove url queries after use --- astro/src/components/Help.tsx | 5 +++++ astro/src/lib/events/addSrcURLParam.js | 2 ++ astro/src/lib/removeQueryParam.js | 16 ++++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 astro/src/lib/removeQueryParam.js diff --git a/astro/src/components/Help.tsx b/astro/src/components/Help.tsx index eee56783d..8f8f2e20b 100644 --- a/astro/src/components/Help.tsx +++ b/astro/src/components/Help.tsx @@ -2,6 +2,7 @@ import styles from './Help.module.css' import { useState, useEffect } from 'preact/hooks' import { marked } from 'marked' import { loadCodeFromString } from "../lib/loadCodeFromString.ts"; +import { removeQueryParam } from "../lib/removeQueryParam.js"; marked.setOptions({ highlight: function (code, language) { @@ -41,8 +42,12 @@ export default function Help({ if (workshop === null) return + if (confirm("Clear text editor?")) loadCodeFromString(""); + removeQueryParam("guide"); + + const res = await fetch( `https://raw.githubusercontent.com/hackclub/blot/main/guides/${workshop}.md` ) diff --git a/astro/src/lib/events/addSrcURLParam.js b/astro/src/lib/events/addSrcURLParam.js index 18c424810..ceb602f57 100644 --- a/astro/src/lib/events/addSrcURLParam.js +++ b/astro/src/lib/events/addSrcURLParam.js @@ -1,4 +1,5 @@ import { loadCodeFromString } from '../loadCodeFromString.ts' +import { removeQueryParam } from "../removeQueryParam.js"; export async function addSrcURLParam() { const currentUrl = new URL(window.location.href) @@ -12,6 +13,7 @@ export async function addSrcURLParam() { const content = await response.text() loadCodeFromString(content) + removeQueryParam("src") } catch (error) { console.error('Error fetching content:', error) } diff --git a/astro/src/lib/removeQueryParam.js b/astro/src/lib/removeQueryParam.js new file mode 100644 index 000000000..c38d8d8f5 --- /dev/null +++ b/astro/src/lib/removeQueryParam.js @@ -0,0 +1,16 @@ +export function removeQueryParam(paramName) { + // Get the current URL + let currentUrl = window.location.href; + + // Create a URL object from the current URL + let urlObj = new URL(currentUrl); + + // Remove the query parameter + urlObj.searchParams.delete(paramName); + + // Get the resulting URL string, after the parameter removal + let newUrl = urlObj.toString(); + + // Update the URL displayed in the browser without reloading the page + history.replaceState({}, null, newUrl); +} \ No newline at end of file