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