Skip to content

Instantly share code, notes, and snippets.

@miharekar
Forked from kepano/obsidian-web-clipper.js
Last active November 9, 2022 08:00
Show Gist options
  • Save miharekar/bd901faabd4a40536c3aca3c3339089c to your computer and use it in GitHub Desktop.
Save miharekar/bd901faabd4a40536c3aca3c3339089c 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/turndown@7.0?module"),
import("https://unpkg.com/@tehshrike/readability@0.2"),
]).then(([{ default: Turndown }, { default: Readability }]) => {
const vault = "";
const folder = "quick";
const tags = "#clippings";
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('');
return yyyy + '-' + (mmChars[1] ? mm : "0" + mmChars[0]) + '-' + (ddChars[1] ? dd : "0" + ddChars[0]);
}
const today = convertDate(date);
const dateFileName = new Date().toISOString().replace(/:/g, '-').replace(/-\d\d\..+/, '').replace(/T/, '-');
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 + dateFileName + "-" + fileName)
+ "&content=" + encodeURIComponent(fileContent)
+ vaultName;
})
javascript: Promise.all([
import("https://unpkg.com/turndown@7.0?module"),
import("https://unpkg.com/@tehshrike/readability@0.2"),
]).then(([{ default: Turndown }, { default: Readability }]) => {
const vault = "";
const folder = "quick/";
const tags = "clippings";
const getSelectionHtml = () => {
let html = "";
if (typeof window.getSelection != "undefined") {
const sel = window.getSelection();
if (sel.rangeCount) {
const 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 getFileName = (fileName) => {
const platform = window.navigator.platform;
const 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 selection = getSelectionHtml();
const {
title,
byline,
content,
} = new Readability(document.cloneNode(true)).parse();
const fileName = getFileName(title);
const markdownify = selection ?? content;
const vaultName = vault ? `&vault=${encodeURIComponent(`${vault}`)}` : "";
const markdownBody = new Turndown({
headingStyle: "atx",
hr: "---",
bulletListMarker: "-",
codeBlockStyle: "fenced",
emDelimiter: "*",
}).turndown(markdownify);
const today = new Date().toISOString().split("T")[0];
const dateFileName = new Date().toISOString().replace(/:/g, '-').replace(/-\d\d\..+/, '').replace(/T/, '-');
const fileContent =
"---\n"
+ "author: " + byline + "\n"
+ "title: " + title + "\n"
+ "source: " + document.URL + "\n"
+ "clipped: " + today + "\n"
+ "tags: " + tags + "\n"
+ "---\n\n"
+ markdownBody;
document.location.href = [
"obsidian://new?",
`file=${encodeURIComponent(folder + dateFileName + " " + fileName)}`,
`&content=${encodeURIComponent(fileContent)}`,
vaultName,
].join("");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment