Obsidian Bookmarklet Adaptation for Craft

Created Diff never expires
11 removals
Lines
Total
Removed
Words
Total
Removed
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
152 lines
11 additions
Lines
Total
Added
Words
Total
Added
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
152 lines
javascript: Promise.all([import('https://unpkg.com/turndown@6.0.0?module'), import('https://unpkg.com/@tehshrike/readability@0.2.0'), ]).then(async ([{
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: Turndown
}, {
}, {
default: Readability
default: Readability
}]) => {
}]) => {


/* Optional vault name */
/* Optional vault name */
const vault = "";
const vault = "davod";


/* Optional folder name such as "Clippings/" */
/* Optional folder name such as "Clippings/" */
const folder = "Clippings/";
const folder = "capture/";


/* Optional tags */
/* Optional tags */
let tags = "clippings";
let tags = "capture";


/* Parse the site's meta keywords content into tags, if present */
/* Parse the site's meta keywords content into tags, if present */
if (document.querySelector('meta[name="keywords" i]')) {
if (document.querySelector('meta[name="keywords" i]')) {
var keywords = document.querySelector('meta[name="keywords" i]').getAttribute('content').split(',');
var keywords = document.querySelector('meta[name="keywords" i]').getAttribute('content').split(',');


keywords.forEach(function(keyword) {
keywords.forEach(function(keyword) {
let tag = ' ' + keyword.split(' ').join('');
let tag = ' ' + keyword.split(' ').join('');
tags += tag;
tags += tag;
});
});
}
}


function getSelectionHtml() {
function getSelectionHtml() {
var html = "";
var html = "";
if (typeof window.getSelection != "undefined") {
if (typeof window.getSelection != "undefined") {
var sel = window.getSelection();
var sel = window.getSelection();
if (sel.rangeCount) {
if (sel.rangeCount) {
var container = document.createElement("div");
var container = document.createElement("div");
for (var i = 0, len = sel.rangeCount; i < len; ++i) {
for (var i = 0, len = sel.rangeCount; i < len; ++i) {
container.appendChild(sel.getRangeAt(i).cloneContents());
container.appendChild(sel.getRangeAt(i).cloneContents());
}
}
html = container.innerHTML;
html = container.innerHTML;
}
}
} else if (typeof document.selection != "undefined") {
} else if (typeof document.selection != "undefined") {
if (document.selection.type == "Text") {
if (document.selection.type == "Text") {
html = document.selection.createRange().htmlText;
html = document.selection.createRange().htmlText;
}
}
}
}
return html;
return html;
}
}


const selection = getSelectionHtml();
const selection = getSelectionHtml();


const {
const {
title,
title,
byline,
byline,
content
content
} = new Readability(document.cloneNode(true)).parse();
} = new Readability(document.cloneNode(true)).parse();


function getFileName(fileName) {
function getFileName(fileName) {
var userAgent = window.navigator.userAgent,
var userAgent = window.navigator.userAgent,
platform = window.navigator.platform,
platform = window.navigator.platform,
windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'];
windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'];


if (windowsPlatforms.indexOf(platform) !== -1) {
if (windowsPlatforms.indexOf(platform) !== -1) {
fileName = fileName.replace(':', '').replace(/[/\\?%*|"<>]/g, '-');
fileName = fileName.replace(':', '').replace(/[/\\?%*|"<>]/g, '-');
} else {
} else {
fileName = fileName.replace(':', '').replace(/\//g, '-').replace(/\\/g, '-');
fileName = fileName.replace(':', '').replace(/\//g, '-').replace(/\\/g, '-');
}
}
return fileName;
return fileName;
}
}
const fileName = getFileName(title);
const fileName = getFileName(title);


if (selection) {
if (selection) {
var markdownify = selection;
var markdownify = selection;
} else {
} else {
var markdownify = content;
var markdownify = content;
}
}


if (vault) {
if (vault) {
var vaultName = '&vault=' + encodeURIComponent(`${vault}`);
var vaultName = '&vault=' + encodeURIComponent(`${vault}`);
} else {
} else {
var vaultName = '';
var vaultName = '';
}
}


const markdownBody = new Turndown({
const markdownBody = new Turndown({
headingStyle: 'atx',
headingStyle: 'atx',
hr: '---',
hr: '---',
bulletListMarker: '-',
bulletListMarker: '-',
codeBlockStyle: 'fenced',
codeBlockStyle: 'fenced',
emDelimiter: '*',
emDelimiter: '*',
}).turndown(markdownify);
}).turndown(markdownify);


var date = new Date();
var date = new Date();


function convertDate(date) {
function convertDate(date) {
var yyyy = date.getFullYear().toString();
var yyyy = date.getFullYear().toString();
var mm = (date.getMonth() + 1).toString();
var mm = (date.getMonth() + 1).toString();
var dd = date.getDate().toString();
var dd = date.getDate().toString();
var mmChars = mm.split('');
var mmChars = mm.split('');
var ddChars = dd.split('');
var ddChars = dd.split('');
return yyyy + '-' + (mmChars[1] ? mm : "0" + mmChars[0]) + '-' + (ddChars[1] ? dd : "0" + ddChars[0]);
return yyyy + '-' + (mmChars[1] ? mm : "0" + mmChars[0]) + '-' + (ddChars[1] ? dd : "0" + ddChars[0]);
}
}


const today = convertDate(date);
const today = convertDate(date);


// Utility function to get meta content by name or property
// Utility function to get meta content by name or property
function getMetaContent(attr, value) {
function getMetaContent(attr, value) {
var element = document.querySelector(`meta[${attr}='${value}']`);
var element = document.querySelector(`meta[${attr}='${value}']`);
return element ? element.getAttribute("content").trim() : "";
return element ? element.getAttribute("content").trim() : "";
}
}


// Fetch byline, meta author, property author, or site name
// Fetch byline, meta author, property author, or site name
var author = byline || getMetaContent("name", "author") || getMetaContent("property", "author") || getMetaContent("property", "og:site_name");
var author = byline || getMetaContent("name", "author") || getMetaContent("property", "author") || getMetaContent("property", "og:site_name");


// Check if there's an author and add brackets
// Check if there's an author and add brackets
var authorBrackets = author ? `"[[${author}]]"` : "";
var authorBrackets = author ? `"[[${author}]]"` : "";




/* Try to get published date */
/* Try to get published date */
var timeElement = document.querySelector("time");
var timeElement = document.querySelector("time");
var publishedDate = timeElement ? timeElement.getAttribute("datetime") : "";
var publishedDate = timeElement ? timeElement.getAttribute("datetime") : "";


if (publishedDate && publishedDate.trim() !== "") {
if (publishedDate && publishedDate.trim() !== "") {
var date = new Date(publishedDate);
var date = new Date(publishedDate);
var year = date.getFullYear();
var year = date.getFullYear();
var month = date.getMonth() + 1; // Months are 0-based in JavaScript
var month = date.getMonth() + 1; // Months are 0-based in JavaScript
var day = date.getDate();
var day = date.getDate();


// Pad month and day with leading zeros if necessary
// Pad month and day with leading zeros if necessary
month = month < 10 ? '0' + month : month;
month = month < 10 ? '0' + month : month;
day = day < 10 ? '0' + day : day;
day = day < 10 ? '0' + day : day;


var published = year + '-' + month + '-' + day;
var published = year + '-' + month + '-' + day;
} else {
} else {
var published = ''
var published = ''
}
}


/* YAML front matter as tags render cleaner with special chars */
/* YAML front matter as tags render cleaner with special chars */
const fileContent =
const fileContent =
'---\n' +
'```yaml\n' +
'category: "[[Clippings]]"\n' +
'category: "[[Clippings]]"\n' +
'author: ' + authorBrackets + '\n' +
'author: ' + authorBrackets + '\n' +
'title: "' + title + '"\n' +
'title: "' + title + '"\n' +
'source: ' + document.URL + '\n' +
'source: ' + document.URL + '\n' +
'clipped: ' + today + '\n' +
'clipped: ' + today + '\n' +
'published: ' + published + '\n' +
'published: ' + published + '\n' +
'topics: \n' +
'topics: \n' +
'tags: [' + tags + ']\n' +
'tags: [' + tags + ']\n' +
'---\n\n' +
'```\n\n' +
markdownBody;
markdownBody;


document.location.href = "obsidian://new?" +
document.location.href = "craftdocs://createdocument?" +
"file=" + encodeURIComponent(folder + fileName) +
"spaceId=9ace8fc2-cf15-b788-1bed-e378ede7ce78" +
"&content=" + encodeURIComponent(fileContent) +
"&title=" + encodeURIComponent(fileName) +
vaultName;
"&content=" + encodeURIComponent(fileContent);


})
})