Last active
October 16, 2023 01:00
-
-
Save yacafx/e269d07e775ad257b9bb1b797ad37c26 to your computer and use it in GitHub Desktop.
Obsidian Links Today Keeper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Self-invoking anonymous function to encapsulate the code and avoid global scope pollution | |
javascript: (function() { | |
// Define the vault and folder names | |
var vaultName = "ADD YOUR VAULT NAME"; | |
var folderName = "ADD YOUR FOLDER NAME"; | |
// Function to get the description from the meta tag | |
function getDescription() { | |
var metaDescription = document.querySelector('meta[name="description"]'); | |
return metaDescription ? metaDescription.getAttribute('content') : ''; | |
} | |
// Function to get the main image either from the og:image meta tag or the first image on the page | |
function getMainImage() { | |
var metaImage = document.querySelector('meta[property="og:image"]'); | |
if (metaImage) { | |
return metaImage.getAttribute('content'); | |
} else { | |
var firstImage = document.querySelector('img'); | |
return firstImage ? firstImage.src : ''; | |
} | |
} | |
// Get the page title | |
var title = document.title; | |
// Get the page URL | |
var url = document.URL; | |
// Get the description | |
var description = getDescription(); | |
// Get the main image | |
var mainImage = getMainImage(); | |
// Create content to append | |
var contentToAppend = | |
'## ' + title + '\n' + // Header with page title | |
'- URL: [' + title + '](' + url + ')\n' + // Markdown link to the page | |
'- Description: ' + description + '\n' + // Description from meta tag | |
'- Main Image: \n\n' + // Main image in Markdown image syntax | |
'---\n\n'; // Markdown divider followed by two new lines | |
// Get today's date in YYYY-MM-DD format | |
var today = new Date().toISOString().slice(0, 10); | |
// Encode the content to append to ensure it's URL-safe | |
var encodedContent = encodeURIComponent(contentToAppend); | |
// Construct the URL for appending to today's note in Obsidian | |
// This uses Obsidian's URI scheme to append content to a specified note | |
document.location.href = "obsidian://advanced-uri?vault=" + | |
encodeURIComponent(vaultName) + // Vault name URL-encoded | |
"&filepath=" + encodeURIComponent(folderName + '/' + today + '.md') + // Path to today's note, URL-encoded | |
"&data=" + encodedContent + // Content to append, URL-encoded | |
"&mode=append"; // Mode specifying to append the content | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment