Being able to add a web bookmark to a list of links wherever I am in my space.
The Script
creates a new command called Add Web Bookmark
it will open 2 prompts: first, to get the URL, then the page to add it to
the page prompt is optional and the script will use a default value if empty - change pageName = "read"; to change the default page
the URL will be added to the very bottom of the selected page
I’ve tried to keep it simple to make it easier to modify. The script can be used for any kind of content you want to add to a page
the default format can be changed here: ${pageContent}\n${url}
e.g. to make it a list item and add a default tag #read just change it to ${pageContent}\n- ${url} #read
Adding the script
add the following script anywhere in your space in a code block in the space-script format > more information
Run command System: Reload
Command should be accessible
silverbullet.registerCommand(
{ name: "Add Web Bookmark" },
async () => {
const url = await editor.prompt("Enter the URL:");
if (!url) {
await editor.flashNotification("No URL provided.", "error");
return;
}
let pageName = await editor.prompt("Enter the page name to insert the URL into:");
if (!pageName || pageName.trim() === "") {
pageName = "read"; // Use a default page name if none is provided
}
try {
// Read the page content, or initialize it if the page does not exist
let pageContent;
try {
pageContent = await space.readPage(pageName);
} catch (e) {
// Assume the page does not exist, initialize with empty content
pageContent = "";
}
// Append the URL as a markdown link
const updatedContent = `${pageContent}\n${url}`;
// Write the updated content back to the page
await space.writePage(pageName, updatedContent);
await editor.flashNotification(`URL successfully inserted into ${pageName}.`);
} catch (error) {
console.error("Error while inserting URL:", error);
await editor.flashNotification("An error occurred while inserting the URL.", "error");
}
}
);
Feedback is welcome - not a coder and happy to learn more. As a disclaimer: script was created with the help of AI.
I modified for myself to use the same page instead of creating a new one, but your script gave me the idea and I wanted to say I appreciate it
silverbullet.registerCommand(
{ name: "Add Web Bookmark" },
async () => {
const url = await editor.prompt("Enter the URL:");
if (!url) {
await editor.flashNotification("No URL provided.", "error");
return;
}
const pageName = "General_Pages/Bookmarks";
try {
// Read the page content, or initialize it if the page does not exist
let pageContent;
try {
pageContent = await space.readPage(pageName);
} catch (e) {
// Assume the page does not exist, initialize with empty content
pageContent = "";
}
// Append the URL as a markdown link
const updatedContent = `${pageContent}\n${url}`;
// Write the updated content back to the page
await space.writePage(pageName, updatedContent);
await editor.flashNotification("URL successfully added to bookmarks.");
} catch (error) {
console.error("Error while inserting URL:", error);
await editor.flashNotification("An error occurred while adding the bookmark.", "error");
}
}
);
silverbullet.registerCommand(
{ name: "Add Web Bookmark" },
async () => {
// Prompt the user for the URL
const url = await editor.prompt("Enter the URL:");
if (!url) {
await editor.flashNotification("No URL provided.", "error");
return;
}
// Retrieve the current page name
const currentPageName = await editor.getCurrentPage();
// Prompt the user for the page name, with the current page name as the default value
const pageName = await editor.prompt(
"Enter the page name to insert the URL into:",
"" // Suggest the current page name as the default value
) || currentPageName; // Fallback to the current page if input is empty
try {
// Read the page content, or initialize it if the page does not exist
let pageContent;
try {
pageContent = await space.readPage(pageName);
} catch (e) {
// Assume the page does not exist, initialize with empty content
pageContent = "";
}
// Append the URL as plain text
const updatedContent = `${pageContent}\n${url}`;
// Write the updated content back to the page
await space.writePage(pageName, updatedContent);
await editor.flashNotification(`URL successfully inserted into ${pageName}.`);
} catch (error) {
console.error("Error while inserting URL:", error);
await editor.flashNotification("An error occurred while inserting the URL.", "error");
}
}
);
So I'm trying to use this as a way of bookmarking websites but for some reason I can't get it to run. I created a new page on my space and then pasted the code, setting the formatting as 'space-script', then reloading, but I'm not finding the command. Is space-script disabled due to the security concerns? I would fun the Chrome extension but I don't think it will work as I'm running self-hosted behind a Cloudflare tunnel secured by MFA.
If you ask deepwiki nicely, it could rewrite/convert simple space-scripts into space-lua. or if it fails you could always try to use some smarter LLM's like claude or gemini or even chatGPT.
something like this:
Could you please rewrite this space-script into space-lua:
...paste code here...
command.define {
name = "Add Web Bookmark",
run = function()
-- Prompt the user for the URL
local url = editor.prompt("Enter the URL:")
if not url then
editor.flashNotification("No URL provided.", "error")
return
end
-- Retrieve the current page name
local currentPageName = editor.getCurrentPage()
-- Prompt the user for the page name, with the current page name as the default value
local pageName = editor.prompt(
"Enter the page name to insert the URL into:",
"" -- Suggest the current page name as the default value
) or currentPageName -- Fallback to the current page if input is empty
-- Read the page content, or initialize it if the page does not exist
local success, pageContent = pcall(function()
return space.readPage(pageName)
end)
if not success then
-- Assume the page does not exist, initialize with empty content
pageContent = ""
end
-- Append the URL as plain text
local updatedContent = pageContent .. "\n" .. url
-- Write the updated content back to the page
local success2, err = pcall(function()
space.writePage(pageName, updatedContent)
editor.flashNotification("URL successfully inserted into " .. pageName .. ".")
end)
if not success2 then
print("Error while inserting URL: " .. tostring(err))
editor.flashNotification("An error occurred while inserting the URL.", "error")
end
end
}
sometimes you need to adjust some things, commands but 70-80% of the job is done.
Thanks, this was very helpful! I added some tagging and timestamps to it also.
command.define {
name = "New Web Bookmark",
run = function()
-- Prompt for URL
local url = editor.prompt("Enter the URL:")
if not url or url == "" then
editor.flashNotification("No URL provided.", "error")
return
end
-- Prompt for TITLE
local title = editor.prompt("Enter a title for this bookmark:")
if not title or title == "" then
title = url
end
-- Prompt for TAGS
local tags = editor.prompt("Enter tags (e.g. #bookmarks #reading #security):")
if not tags or tags == "" then
tags = "#bookmarks"
end
-- Default bookmark PAGE
local pageName = "Bookmarks"
-- Read the page content, or initialize it if the page does not exist
local success, pageContent = pcall(function()
return space.readPage(pageName)
end)
if not success or not pageContent then
pageContent = ""
end
-- Prevent duplicate URLs
if string.find(pageContent, url, 1, true) then
editor.flashNotification("This URL already exists on the page.", "error")
return
end
-- Ensure Bookmarks heading exists
local heading = "## Bookmarks"
if not string.find(pageContent, heading, 1, true) then
pageContent = pageContent .. "\n\n" .. heading .. "\n"
end
-- Create timestamp
-- local timestamp = os.date("%Y-%m-%d %H:%M")
-- use the line above for a more traditional date/time stamp
local timestamp = os.date("%d%b%Y")
local uppercasetimestamp = string.upper(timestamp)
-- Build Markdown bookmark line
local bookmarkLine = "- [" .. title .. "](" .. url .. ") — " .. uppercasetimestamp .. " " .. tags
-- Insert after Bookmarks heading
local updatedContent = pageContent:gsub(
heading .. "\n",
heading .. "\n" .. bookmarkLine .. "\n",
1
)
-- Write back to the page
local success2, err = pcall(function()
space.writePage(pageName, updatedContent)
editor.flashNotification("Bookmark successfully inserted into " .. pageName .. ".")
end)
if not success2 then
print("Error while inserting URL: " .. tostring(err))
editor.flashNotification("An error occurred while inserting the URL.", "error")
end
end
}