-
Notifications
You must be signed in to change notification settings - Fork 0
/
obsidian_clipper.js
1 lines (1 loc) · 3.62 KB
/
obsidian_clipper.js
1
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 "MindVault"%3B%0A%0A %2F* Optional folder name such as "Clippings%2F" *%2F%0A const folder %3D "Sources/Articles"%3B%0A%0A %2F* Optional tags *%2F%0A const tags %3D "%23Articles %23WebClipper"%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 return yyyy %2B '-' %2B (mmChars%5B1%5D%3Fmm%3A"0"%2BmmChars%5B0%5D) %2B '-' %2B (ddChars%5B1%5D%3Fdd%3A"0"%2BddChars%5B0%5D)%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 '%2F' %2B fileName)%0A %2B "%26content%3D" %2B encodeURIComponent(fileContent)%0A %2B vaultName %3B%0A%7D)%7D)()%3B