|
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 = "Clippings/"; |
|
|
|
/* Optional tags */ |
|
let tags = "clippings"; |
|
|
|
/* Parse the site's meta keywords content into tags, if present */ |
|
if (document.querySelector('meta[name="keywords" i]')) { |
|
var keywords = document.querySelector('meta[name="keywords" i]').getAttribute('content').split(','); |
|
|
|
keywords.forEach(function(keyword) { |
|
let tag = ' ' + keyword.split(' ').join(''); |
|
tags += tag; |
|
}); |
|
} |
|
|
|
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); |
|
|
|
// Utility function to get meta content by name or property |
|
function getMetaContent(attr, value) { |
|
var element = document.querySelector(`meta[${attr}='${value}']`); |
|
return element ? element.getAttribute("content").trim() : ""; |
|
} |
|
|
|
// Fetch byline, meta author, property author, or 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 |
|
var authorBrackets = author ? `"[[${author}]]"` : ""; |
|
|
|
|
|
/* Try to get published date */ |
|
var timeElement = document.querySelector("time"); |
|
var publishedDate = timeElement ? timeElement.getAttribute("datetime") : ""; |
|
|
|
if (publishedDate && publishedDate.trim() !== "") { |
|
var date = new Date(publishedDate); |
|
var year = date.getFullYear(); |
|
var month = date.getMonth() + 1; // Months are 0-based in JavaScript |
|
var day = date.getDate(); |
|
|
|
// Pad month and day with leading zeros if necessary |
|
month = month < 10 ? '0' + month : month; |
|
day = day < 10 ? '0' + day : day; |
|
|
|
var published = year + '-' + month + '-' + day; |
|
} else { |
|
var published = '' |
|
} |
|
|
|
/* YAML front matter as tags render cleaner with special chars */ |
|
const fileContent = |
|
'---\n' |
|
+ 'category: "[[Clippings]]"\n' |
|
+ 'author: ' + authorBrackets + '\n' |
|
+ 'title: "' + title + '"\n' |
|
+ 'source: ' + document.URL + '\n' |
|
+ 'clipped: ' + today + '\n' |
|
+ 'published: ' + published + '\n' |
|
+ 'topics: \n' |
|
+ 'tags: [' + tags + ']\n' |
|
+ '---\n\n' |
|
+ markdownBody ; |
|
|
|
document.location.href = "obsidian://new?" |
|
+ "file=" + encodeURIComponent(folder + fileName) |
|
+ "&content=" + encodeURIComponent(fileContent) |
|
+ vaultName ; |
|
|
|
}) |
Hey @jordanwiseman , unfortunately none of the variations work very well for me. In Chrome, I've regressed to all extensions disabled, security set to 'no protection', AV disabled, local Defender firewall disabled... still, a particular page will work once for me, then fail for awhile, then work again. For example:
https://xkcd.com/327/
I see the obsidian launch in the console, Sometimes with an error, but often with no error and still does not clip.
Sometimes see: see unknown origin/cancelled. However when I retry I do not see this, but see: Not allowed to launch 'obsidian://new?file=Exploits%20of%20a%20Mom&content=---...' because a user gesture is required.
There is no CORS error in the console. Of course not, the page origin is not making the request.
Some pages never work, always get the '... user gesture required'.
Like this page: https://sportsmedicine-open.springeropen.com/articles/10.1186/s40798-021-00390-y#author-information
Some pages work again, randomly. I could not find a pattern for awhile. Opening another page in the browser makes no difference. However, if I type into another page, like this post, or gmail... or even notepad, it almost always makes it work again. And it fails until I use the keyboard on a page again.
No joy in Edge either. Have only tried a personal machine so far.
I've wasted most of my morning on this, need to get some things done today!