mirror of
https://github.com/FranP-code/unduck.git
synced 2025-10-13 00:32:41 +00:00
Using window.location.origin
This commit is contained in:
72
src/main.ts
72
src/main.ts
@@ -2,8 +2,8 @@ import { bangs } from "./bang";
|
||||
import "./global.css";
|
||||
|
||||
function noSearchDefaultPageRender() {
|
||||
const app = document.querySelector<HTMLDivElement>("#app")!;
|
||||
app.innerHTML = `
|
||||
const app = document.querySelector<HTMLDivElement>("#app")!;
|
||||
app.innerHTML = `
|
||||
<div style="display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh;">
|
||||
<div class="content-container">
|
||||
<h1>Und*ck</h1>
|
||||
@@ -12,7 +12,7 @@ function noSearchDefaultPageRender() {
|
||||
<input
|
||||
type="text"
|
||||
class="url-input"
|
||||
value="https://unduck.link?q=%s"
|
||||
value="${`${window.location.origin}/?q=%s`}"
|
||||
readonly
|
||||
/>
|
||||
<button class="copy-button">
|
||||
@@ -30,55 +30,55 @@ function noSearchDefaultPageRender() {
|
||||
</div>
|
||||
`;
|
||||
|
||||
const copyButton = app.querySelector<HTMLButtonElement>(".copy-button")!;
|
||||
const copyIcon = copyButton.querySelector("img")!;
|
||||
const urlInput = app.querySelector<HTMLInputElement>(".url-input")!;
|
||||
const copyButton = app.querySelector<HTMLButtonElement>(".copy-button")!;
|
||||
const copyIcon = copyButton.querySelector("img")!;
|
||||
const urlInput = app.querySelector<HTMLInputElement>(".url-input")!;
|
||||
|
||||
copyButton.addEventListener("click", async () => {
|
||||
await navigator.clipboard.writeText(urlInput.value);
|
||||
copyIcon.src = "/clipboard-check.svg";
|
||||
copyButton.addEventListener("click", async () => {
|
||||
await navigator.clipboard.writeText(urlInput.value);
|
||||
copyIcon.src = "/clipboard-check.svg";
|
||||
|
||||
setTimeout(() => {
|
||||
copyIcon.src = "/clipboard.svg";
|
||||
}, 2000);
|
||||
});
|
||||
setTimeout(() => {
|
||||
copyIcon.src = "/clipboard.svg";
|
||||
}, 2000);
|
||||
});
|
||||
}
|
||||
|
||||
const LS_DEFAULT_BANG = localStorage.getItem("default-bang") ?? "g";
|
||||
const defaultBang = bangs.find((b) => b.t === LS_DEFAULT_BANG);
|
||||
|
||||
function getBangredirectUrl() {
|
||||
const url = new URL(window.location.href);
|
||||
const query = url.searchParams.get("q")?.trim() ?? "";
|
||||
if (!query) {
|
||||
noSearchDefaultPageRender();
|
||||
return null;
|
||||
}
|
||||
const url = new URL(window.location.href);
|
||||
const query = url.searchParams.get("q")?.trim() ?? "";
|
||||
if (!query) {
|
||||
noSearchDefaultPageRender();
|
||||
return null;
|
||||
}
|
||||
|
||||
const match = query.match(/!(\S+)/i);
|
||||
const match = query.match(/!(\S+)/i);
|
||||
|
||||
const bangCandidate = match?.[1]?.toLowerCase();
|
||||
const selectedBang = bangs.find((b) => b.t === bangCandidate) ?? defaultBang;
|
||||
const bangCandidate = match?.[1]?.toLowerCase();
|
||||
const selectedBang = bangs.find((b) => b.t === bangCandidate) ?? defaultBang;
|
||||
|
||||
// Remove the first bang from the query
|
||||
const cleanQuery = query.replace(/!\S+\s*/i, "").trim();
|
||||
// Remove the first bang from the query
|
||||
const cleanQuery = query.replace(/!\S+\s*/i, "").trim();
|
||||
|
||||
// Format of the url is:
|
||||
// https://www.google.com/search?q={{{s}}}
|
||||
const searchUrl = selectedBang?.u.replace(
|
||||
"{{{s}}}",
|
||||
// Replace %2F with / to fix formats like "!ghr+t3dotgg/unduck"
|
||||
encodeURIComponent(cleanQuery).replace(/%2F/g, "/")
|
||||
);
|
||||
if (!searchUrl) return null;
|
||||
// Format of the url is:
|
||||
// https://www.google.com/search?q={{{s}}}
|
||||
const searchUrl = selectedBang?.u.replace(
|
||||
"{{{s}}}",
|
||||
// Replace %2F with / to fix formats like "!ghr+t3dotgg/unduck"
|
||||
encodeURIComponent(cleanQuery).replace(/%2F/g, "/")
|
||||
);
|
||||
if (!searchUrl) return null;
|
||||
|
||||
return searchUrl;
|
||||
return searchUrl;
|
||||
}
|
||||
|
||||
function doRedirect() {
|
||||
const searchUrl = getBangredirectUrl();
|
||||
if (!searchUrl) return;
|
||||
window.location.replace(searchUrl);
|
||||
const searchUrl = getBangredirectUrl();
|
||||
if (!searchUrl) return;
|
||||
window.location.replace(searchUrl);
|
||||
}
|
||||
|
||||
doRedirect();
|
||||
|
||||
Reference in New Issue
Block a user