Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dustinmartin/ec611d8afb9079c058e5dbc927e3072d to your computer and use it in GitHub Desktop.

Select an option

Save dustinmartin/ec611d8afb9079c058e5dbc927e3072d to your computer and use it in GitHub Desktop.
Obsidian Web Clipper Bookmarklet to save articles and pages from the web (for Safari, Chrome, Firefox, and mobile browsers)
javascript: Promise.all([import('https://unpkg.com/[email protected]?module'), import('https://unpkg.com/@tehshrike/[email protected]'), ]).then(async ([{
default: Turndown
}, {
default: Readability
}]) => {
function getSelectionHtml() {
var html = "";
if (typeof window.getSelection != "undefined") {
var sel = window.getSelection();
if (sel.rangeCount) {
var container = document.createElement("div");
for (var 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();
const {
title,
byline,
content
} = new Readability(document.cloneNode(true)).parse();
const titleUri = title.replace(':', '').replace(/\//g, '-').replace(/\\/g, '-');
if (selection) {
var markdownify = selection;
} else {
var markdownify = content;
}
const markdown = new Turndown({
headingStyle: 'atx',
hr: '---',
bulletListMarker: '-',
codeBlockStyle: 'fenced',
emDelimiter: '*',
}).turndown(markdownify);
var date = new Date();
function convertDate(date) {
var yyyy = date.getFullYear().toString();
var mm = (date.getMonth()+1).toString();
var dd = date.getDate().toString();
var mmChars = mm.split('');
var ddChars = dd.split('');
return yyyy + '-' + (mmChars[1]?mm:"0"+mmChars[0]) + '-' + (ddChars[1]?dd:"0"+ddChars[0]);
}
const today = convertDate(date);
const obsidian = `author:: ${byline}\nsource:: [${title}](${document.URL})\nclipped:: [[${today}]]\npublished:: \n\n#clippings\n\n${markdown}`;
window.location.href = "obsidian://new?"
+ "name=" + titleUri
+ "&content="
+ encodeURIComponent(obsidian) + "" ;
})
javascript:(function()%7BPromise.all(%5Bimport(%22https%3A%2F%2Funpkg.com%2Fturndown%406.0.0%3Fmodule%22)%2Cimport(%22https%3A%2F%2Funpkg.com%2F%40tehshrike%2Freadability%400.2.0%22)%5D).then(async(%5B%7Bdefault%3Ae%7D%2C%7Bdefault%3At%7D%5D)%3D%3E%7Bconst%20n%3Dfunction()%7Bvar%20e%3D%22%22%3Bif(void%200!%3D%3Dwindow.getSelection)%7Bvar%20t%3Dwindow.getSelection()%3Bif(t.rangeCount)%7Bfor(var%20n%3Ddocument.createElement(%22div%22)%2Co%3D0%2Ci%3Dt.rangeCount%3Bo%3Ci%3B%2B%2Bo)n.appendChild(t.getRangeAt(o).cloneContents())%3Be%3Dn.innerHTML%7D%7Delse%20void%200!%3D%3Ddocument.selection%26%26%22Text%22%3D%3Ddocument.selection.type%26%26(e%3Ddocument.selection.createRange().htmlText)%3Breturn%20e%7D()%2C%7Btitle%3Ao%2Cbyline%3Ai%2Ccontent%3Al%7D%3Dnew%20t(document.cloneNode(!0)).parse()%2Cr%3Do.replace(%22%3A%22%2C%22%22).replace(%2F%5C%2F%2Fg%2C%22-%22).replace(%2F%5C%5C%2Fg%2C%22-%22)%3Bif(n)var%20c%3Dn%3Belse%20c%3Dl%3Bconst%20a%3Dnew%20e(%7BheadingStyle%3A%22atx%22%2Chr%3A%22---%22%2CbulletListMarker%3A%22-%22%2CcodeBlockStyle%3A%22fenced%22%2CemDelimiter%3A%22*%22%7D).turndown(c)%3Bconst%20d%3Dfunction(e)%7Bvar%20t%3De.getFullYear().toString()%2Cn%3D(e.getMonth()%2B1).toString()%2Co%3De.getDate().toString()%2Ci%3Dn.split(%22%22)%2Cl%3Do.split(%22%22)%3Breturn%20t%2B%22-%22%2B(i%5B1%5D%3Fn%3A%220%22%2Bi%5B0%5D)%2B%22-%22%2B(l%5B1%5D%3Fo%3A%220%22%2Bl%5B0%5D)%7D(new%20Date)%2Cu%3D%60author%3A%3A%20%24%7Bi%7D%5Cnsource%3A%3A%20%5B%24%7Bo%7D%5D(%24%7Bdocument.URL%7D)%5Cnclipped%3A%3A%20%5B%5B%24%7Bd%7D%5D%5D%5Cnpublished%3A%3A%20%5Cn%5Cn%23clippings%5Cn%5Cn%24%7Ba%7D%60%3Bwindow.location.href%3D%22obsidian%3A%2F%2Fnew%3Fname%3D%22%2Br%2B%22%26content%3D%22%2BencodeURIComponent(u)%7D)%3B%7D)()%3B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment