Skip to content

Instantly share code, notes, and snippets.

@braden-w
Last active November 30, 2022 05:00
Show Gist options
  • Save braden-w/e36f88837f03327fccf6ff983520e3b3 to your computer and use it in GitHub Desktop.
Save braden-w/e36f88837f03327fccf6ff983520e3b3 to your computer and use it in GitHub Desktop.
My current iteration of Obsidian Web Clipper
javascript: (function () {
/* Optional vault name */
// const vault = prompt("Vault name (leave blank for default):", "");
const vault = ""
const vaultName = vault ? "&vault=" + encodeURIComponent(`${vault}`) : ""
// Initialize variables
const URLtoFolder = {
"https://www.reddit.com": "Reddit",
"https://www.youtube.com/short": "Short",
"https://www.twitter.com": "Tweet",
"https://www.youtube.com/watch": "Video",
}
const URL = document.URL
/* Get Title and clean it */
const processTitle = (fileName) =>
fileName.replace(/[/\\]/g, "-").replace(/[:]/g, " -")
const titlePrompt = prompt("Title:", processTitle(document.title))
const cleanedTitle = processTitle(titlePrompt)
const fileName = cleanedTitle
const title = cleanedTitle
// If URL begins with any of the keys in URLtoFolder, use the corresponding value as the folder name. Otherwise, use "Articles" as the folder name
const prefilledType = Object.keys(URLtoFolder).some((key) =>
URL.startsWith(key)
)
? URLtoFolder[Object.keys(URLtoFolder).find((key) => URL.startsWith(key))]
: "Article"
/* Optional tags */
const on = prompt("On:", "")
.split(", ")
.map((tag) => ` - On/${tag.trim()}`)
.join("\n")
const type = prompt("Type:", prefilledType)
.split(", ")
.map((tag) => ` - Type/${tag.trim()}`)
.join("\n")
const folder = `Source/${prefilledType}/`
function getSelectionHtml() {
let html = ""
if (typeof window.getSelection != "undefined") {
let sel = window.getSelection()
if (sel.rangeCount) {
let container = document.createElement("div")
for (let i = 0, len = sel.rangeCount; i < len; ++i) {
container.appendChild(sel.getRangeAt(i).cloneContents())
}
html = container.innerHTML
}
} else if (typeof document.selection != "undefined") {
if (document.selection.type == "Text") {
html = document.selection.createRange().htmlText
}
}
return html
}
const selection = getSelectionHtml()
/* Get Date */
const now = new Date()
function convertDate(date) {
const yyyy = date.getFullYear().toString()
const mm = (date.getMonth() + 1).toString()
const dd = date.getDate().toString()
const mmChars = mm.split("")
const ddChars = dd.split("")
return (
yyyy +
"-" +
(mmChars[1] ? mm : "0" + mmChars[0]) +
"-" +
(ddChars[1] ? dd : "0" + ddChars[0])
)
}
const today = convertDate(now)
/* Set File Contents */
const fileContent = `---
URL: "[${document.title}](${URL})"
date: "${today}"
tags:
${type}
${on}
---
# ${title}
${selection}
`
document.location.href =
"obsidian://new?" +
"file=" +
encodeURIComponent(folder + fileName) +
"&content=" +
encodeURIComponent(fileContent) +
vaultName
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment