Bookmarklet Blogging

I recently came across an interesting Bookmarklet bei Nicolas. It’s meant to make link-blogging easier for anyone with a GitHub-hosted blog. The script isn’t perfect yet, but it inspired me to develop it further. Here’s my version of the bookmarklet (function() { // Funktion zum Erstellen eines "Slug" aus einem Text const slugify = text => { let str = text.toString(); // Den Text als Zeichenkette behandeln str = str.replaceAll("/", " "); // Schrägstriche (/) durch Leerzeichen ersetzen str = str.normalize("NFD"); // Unicode normalisieren (diakritische Zeichen trennen) str = str.replace(/[^\w\s-]+/g, ""); // Sonderzeichen entfernen (außer Buchstaben, Zahlen, Leerzeichen, Bindestriche) str = str.toLowerCase(); // In Kleinbuchstaben umwandeln str = str.replace(/\s+/g, " "); // Mehrere Leerzeichen auf ein einzelnes reduzieren str = str.trim(); // Führende und endende Leerzeichen entfernen str = str.replace(/ +/g, "-"); // Alle Leerzeichen durch Bindestriche ersetzen return str; // Slug zurückgeben }; // Titel der aktuellen Seite aus dem Dokument abrufen let pageTitle = window.document.title; // Falls ein Text markiert ist, diesen als "linkSelection" setzen, ansonsten leer let linkSelection = "getSelection" in window ? window.getSelection().toString().trim() : ""; // Inhalt für den Link ermitteln: // Zuerst den markierten Text verwenden, falls vorhanden, ansonsten eine Meta-Beschreibung, // oder den ersten Absatz im <main>, <article>, oder <p>-Tag let linkContent = linkSelection || window.document.querySelector("head meta[name=description]")?.content.trim() || window.document.querySelector("main p")?.textContent.trim() || window.document.querySelector("article p")?.textContent.trim() || window.document.querySelector("p")?.textContent.trim(); // Die URL der aktuellen Seite let linkUrl = window.location.href; // Eingabeaufforderung für den Link-Titel (Standardwert: Seiten-Titel) let title = window.prompt("Title of the link?", pageTitle); if (title !== null) { // Nur fortfahren, wenn der Benutzer einen Titel angegeben hat // Eingabeaufforderung für den Link-Slug (Standardwert: aus Titel generierter Slug) let slug = window.prompt("Slug of the link?", slugify(title)); if (slug !== null) { // Nur fortfahren, wenn der Benutzer einen Slug angegeben hat // Das aktuelle Datum und die Uhrzeit im ISO-Format abrufen und in lesbare Form bringen const currentDate = (new Date()).toISOString().replace("T", " ").replace(/\.\d{3}Z/, " +00:00"); // Dateiinhalt für den Blogeintrag erstellen let fileContent = `---\n` + `date: ${currentDate}\n` + `title: "${title}"\n` + `authors: "maik"\n` + `tags: []\n` + `---\n\n` + `[${title}](${linkUrl} "${title}") für ${linkUrl}\n\n` + `${linkContent ? linkContent.replaceAll("\n", "\n> ") : ""}\n`; // GitHub-URL für das Erstellen einer neuen Datei zusammenbauen let githubUrl = `https://github.com/Maik-Wi/blog/new/main/?` + `filename=${`content/notes/${currentDate.slice(0, 10)}-${slug}-index.md`}` + `&value=${encodeURIComponent(fileContent)}` + `&message=${encodeURIComponent(`New link: ${title}`)}`; // Die URL in einem neuen Fenster öffnen, sodass der Nutzer sie bei GitHub weiterbearbeiten kann window.open(githubUrl); } } })(); The text was automatically translated from German into English. The German quotations were also translated in sense. ...

August 19, 2024 · 2 min · 414 words

Make Bookmarklets

The web app Make Bookmarklets lets you turn JavaScript code into a bookmarklet in no time. Make Bookmarklets · Make It Easey: All JavaScript is wrapped in an Immediately Invoked Function Expression (IIFE) for you JavaScript linting Multiline cursor JavaScript IntelliSense Code is automatically minified and uglified Works great on mobile devices The text was automatically translated from German into English. The German quotations were also translated in sense. ...

May 5, 2023 · 1 min · 69 words

Ghost Bookmarklet

After switching from Grav to Ghost I have to tweak a few articles. To be able to quickly jump from the frontend to the backend, I wrote a super-simple bookmarklet. Just save the following code as a bookmark. javascript:window.location.href=window.location.href+'/edit'; After you click the bookmark, the browser will navigate to http://DEINEBLOG.URL/edit and, if you’re logged in, you can edit the article directly. The text was automatically translated from German into English. The German quotations were also translated in sense. ...

December 23, 2021 · 1 min · 78 words