[Space-Script] Add URL to Page (Web Bookmark List)

Goal

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

  1. add the following script anywhere in your space in a code block in the space-script format > more information
  2. Run command System: Reload
  3. 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.

Cheers,
CF

3 Likes

I really like this, a lot actually…

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 :smile:

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");
    }
  }
);
1 Like

@CaffeineFueled Nice script. How to access current page name from the script? I’d like to append to the current page if no particular name is given.

This should do the trick

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");
    }
  }
);

@CaffeineFueled Ah, thanks. :smiley: I always forgot about the system calls. :wink:

1 Like