Skip to content

Instantly share code, notes, and snippets.

@jkbecker
Forked from kepano/obsidian-web-clipper.js
Last active February 23, 2023 06:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkbecker/b9fa8db0fba2ec5198736143a3aceaaf to your computer and use it in GitHub Desktop.
Save jkbecker/b9fa8db0fba2ec5198736143a3aceaaf 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)

By @kepano

🎉 Support my work at buymeacoffee.com/kepano

Demo

You can find a demo of this bookmarklet on YouTube

Installation

Create a new bookmark in your browser, then copy/paste the minified code below into the URL field.

You can customize the output using the optional variables at the top, and the template at the bottom. The default template is designed for use with the Dataview plugin. If you make changes I recommend using Bookmarklet Maker to minify and URI encode the bookmarklet.

Usage

By default, clicking the bookmarklet creates a new Obsidian file from the main body of the article (similar to Readability view). Alternatively you can choose to create a file from a selection, by either selecting all (CMD+A), or just a portion of the page.

Any images in the content will be embedded as external references. If you want to download images locally you can use the Local Images plugin which allows you to download images for a note.

Troubleshooting

This bookmarklet may not work on all websites. If you run into issues, you can also try the MarkDownload browser extension which provides similar functionality. You can troubleshoot issues by opening the Developer Console in your browser and checking if any errors appear when you click the bookmarklet. The most common error is that a website or the browser itself is blocking third party code execution. Unfortunately there is no good solve for that yet.

javascript: Promise.all([import('https://unpkg.com/turndown@6.0.0?module'), import('https://unpkg.com/@tehshrike/readability@0.2.0'), ]).then(async ([{
default: Turndown
}, {
default: Readability
}]) => {
/* Optional vault name */
const vault = "";
/* Optional folder name such as "Clippings/" */
const folder = "";
/* Optional tags */
const tags = "#clippings";
const weekdays = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
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();
function getFileName(fileName) {
var userAgent = window.navigator.userAgent,
platform = window.navigator.platform,
windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'];
if (windowsPlatforms.indexOf(platform) !== -1) {
fileName = fileName.replace(':', '').replace(/[/\\?%*|"<>]/g, '-');
} else {
fileName = fileName.replace(':', '').replace(/\//g, '-').replace(/\\/g, '-');
}
return fileName;
}
const fileName = getFileName(title);
if (selection) {
var markdownify = selection;
} else {
var markdownify = content;
}
if (vault) {
var vaultName = '&vault=' + encodeURIComponent(`${vault}`);
} else {
var vaultName = '';
}
const markdownBody = 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('');
var day = weekdays[date.getDay()];
return yyyy + '-' + (mmChars[1]?mm:"0"+mmChars[0]) + '-' + (ddChars[1]?dd:"0"+ddChars[0]) + '-' + day;
}
const today = convertDate(date);
const fileContent =
"author:: " + byline + "\n"
+ "source:: [" + title + "](" + document.URL + ")\n"
+ "clipped:: [[" + today + "]]\n"
+ "published:: \n\n"
+ tags + "\n\n"
+ markdownBody ;
document.location.href = "obsidian://new?"
+ "file=" + encodeURIComponent(folder + fileName)
+ "&content=" + encodeURIComponent(fileContent)
+ vaultName ;
})
javascript:(function()%7Bjavascript%3A Promise.all(%5Bimport('https%3A%2F%2Funpkg.com%2Fturndown%406.0.0%3Fmodule')%2C import('https%3A%2F%2Funpkg.com%2F%40tehshrike%2Freadability%400.2.0')%2C %5D).then(async (%5B%7B%0A default%3A Turndown%0A%7D%2C %7B%0A default%3A Readability%0A%7D%5D) %3D> %7B%0A%0A %2F* Optional vault name *%2F%0A const vault %3D ""%3B%0A%0A %2F* Optional folder name such as "Clippings%2F" *%2F%0A const folder %3D ""%3B%0A%0A %2F* Optional tags *%2F%0A const tags %3D "%23clippings"%3B%0A%0A const weekdays %3D %5B"Sunday"%2C"Monday"%2C"Tuesday"%2C"Wednesday"%2C"Thursday"%2C"Friday"%2C"Saturday"%5D%3B%0A%0A function getSelectionHtml() %7B%0A var html %3D ""%3B%0A if (typeof window.getSelection !%3D "undefined") %7B%0A var sel %3D window.getSelection()%3B%0A if (sel.rangeCount) %7B%0A var container %3D document.createElement("div")%3B%0A for (var i %3D 0%2C len %3D sel.rangeCount%3B i < len%3B %2B%2Bi) %7B%0A container.appendChild(sel.getRangeAt(i).cloneContents())%3B%0A %7D%0A html %3D container.innerHTML%3B%0A %7D%0A %7D else if (typeof document.selection !%3D "undefined") %7B%0A if (document.selection.type %3D%3D "Text") %7B%0A html %3D document.selection.createRange().htmlText%3B%0A %7D%0A %7D%0A return html%3B%0A %7D%0A%0A const selection %3D getSelectionHtml()%3B%0A%0A const %7B%0A title%2C%0A byline%2C%0A content%0A %7D %3D new Readability(document.cloneNode(true)).parse()%3B%0A%0A function getFileName(fileName) %7B%0A var userAgent %3D window.navigator.userAgent%2C%0A platform %3D window.navigator.platform%2C%0A windowsPlatforms %3D %5B'Win32'%2C 'Win64'%2C 'Windows'%2C 'WinCE'%5D%3B%0A%0A if (windowsPlatforms.indexOf(platform) !%3D%3D -1) %7B%0A fileName %3D fileName.replace('%3A'%2C '').replace(%2F%5B%2F%5C%5C%3F%25*%7C"<>%5D%2Fg%2C '-')%3B%0A %7D else %7B%0A fileName %3D fileName.replace('%3A'%2C '').replace(%2F%5C%2F%2Fg%2C '-').replace(%2F%5C%5C%2Fg%2C '-')%3B%0A %7D%0A return fileName%3B%0A %7D%0A const fileName %3D getFileName(title)%3B%0A%0A if (selection) %7B%0A var markdownify %3D selection%3B%0A %7D else %7B%0A var markdownify %3D content%3B%0A %7D%0A%0A if (vault) %7B%0A var vaultName %3D '%26vault%3D' %2B encodeURIComponent(%60%24%7Bvault%7D%60)%3B%0A %7D else %7B%0A var vaultName %3D ''%3B%0A %7D%0A%0A const markdownBody %3D new Turndown(%7B%0A headingStyle%3A 'atx'%2C%0A hr%3A '---'%2C%0A bulletListMarker%3A '-'%2C%0A codeBlockStyle%3A 'fenced'%2C%0A emDelimiter%3A '*'%2C%0A %7D).turndown(markdownify)%3B%0A%0A var date %3D new Date()%3B%0A%0A function convertDate(date) %7B%0A var yyyy %3D date.getFullYear().toString()%3B%0A var mm %3D (date.getMonth()%2B1).toString()%3B%0A var dd %3D date.getDate().toString()%3B%0A var mmChars %3D mm.split('')%3B%0A var ddChars %3D dd.split('')%3B%0A var day %3D weekdays%5Bdate.getDay()%5D%3B%0A return yyyy %2B '-' %2B (mmChars%5B1%5D%3Fmm%3A"0"%2BmmChars%5B0%5D) %2B '-' %2B (ddChars%5B1%5D%3Fdd%3A"0"%2BddChars%5B0%5D) %2B '-' %2B day%3B%0A %7D%0A%0A const today %3D convertDate(date)%3B%0A%0A const fileContent %3D %0A "author%3A%3A " %2B byline %2B "%5Cn"%0A %2B "source%3A%3A %5B" %2B title %2B "%5D(" %2B document.URL %2B ")%5Cn"%0A %2B "clipped%3A%3A %5B%5B" %2B today %2B "%5D%5D%5Cn"%0A %2B "published%3A%3A %5Cn%5Cn" %0A %2B tags %2B "%5Cn%5Cn"%0A %2B markdownBody %3B%0A %0A document.location.href %3D "obsidian%3A%2F%2Fnew%3F"%0A %2B "file%3D" %2B encodeURIComponent(folder %2B fileName)%0A %2B "%26content%3D" %2B encodeURIComponent(fileContent)%0A %2B vaultName %3B%0A%7D)%7D)()%3B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment