Last active
April 3, 2023 11:43
-
-
Save eoureo/3d362603c8c4ee4166d8fc2bfdb6079f to your computer and use it in GitHub Desktop.
Combination of "Obsidian Web Clipper Bookmarklet" and "Local REST API". Obsidian Web Clipper Bookmarklet과 Local REST API의 결합.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 'Local REST API' Port number */ | |
const REST_API_PORT = 27124; | |
/* Optional 'Local REST API' key */ | |
const REST_API_KEY = "Put your 'Local REST API' key value here."; | |
/* Optional vault name */ | |
/* const vault = ""; */ | |
/* Optional folder name such as "Clippings/" */ | |
const folder = "Scraps/"; | |
/* Optional tags */ | |
const tags = "#clippings"; | |
const URL = `https://127.0.0.1:${REST_API_PORT}/vault/${folder}`; | |
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 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 ; | |
*/ | |
const response = await fetch(URL + fileName + ".md", { | |
method: "POST", | |
headers: { | |
"Content-Type": "text/markdown", | |
"Authorization": "Bearer " + REST_API_KEY, | |
}, | |
body: fileContent, | |
}); | |
/* 에러가 나면 확인 */ | |
if(response.status != 204) { | |
const response_data = await response.text(); | |
console.log(response_data); | |
alert("에러(Error)가 나와 추가할 수 없슴니다.\n\n" + response_data); | |
} | |
else { | |
console.log("성공(Success)!") | |
} | |
}) | |
.catch((err) => { | |
alert("에러 발행\n\n" + err); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
제 블로그 글 Obsidian Web Clipper Bookmarklet과 Local REST API의 결합 - URI 글자 수 제한 넘기에서 내용을 볼 수 있습니다.