Created
February 19, 2023 11:20
-
-
Save Quorafind/c70c6c698feeed66465d59efc39e4e1c to your computer and use it in GitHub Desktop.
Using Quickadd to ask current selection In ChatGPT
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
// Version 0.0.1 | |
module.exports = askChatWithPrompt; | |
function askChat(str, app) { | |
const prompt = encodeURI(str); | |
const chatViewEl = app.workspace.getLeavesOfType("surfing-view").find((item)=>{ return /chat\.openai\.com/.test(item?.view?.currentUrl)}).view.webviewEl; | |
chatViewEl.executeJavaScript(` | |
var htmlElement = document.querySelector("textarea"); | |
htmlElement.value = decodeURI("${ prompt }"); | |
htmlElement.parentElement.childNodes[1].click(); | |
`, true).then((result) => { | |
console.log(result); | |
}); | |
} | |
async function askChatWithPrompt(params) { | |
const {app, Notice} = params; | |
const view = app.workspace.activeLeaf.view; | |
if(!view) { | |
new Notice("Please focus on view"); | |
} | |
let selection = ""; | |
// Change prompt here: | |
const customPrompt = "请根据以下的内容推荐几种不同的笔记方向:"; | |
if(view.getViewType() === "markdown") { | |
selection = view.editor.getSelection(); | |
askChat(customPrompt + selection , app); | |
} else if (view.getViewType() === "surfing-view" || view.getViewType() === "surfing-file-view") { | |
view.webviewEl.executeJavaScript(` | |
document.getSelection().toString(); | |
`,true).then((result)=> { | |
selection = result; | |
askChat(customPrompt + selection , app); | |
}); | |
} else { | |
new Notice("Wrong View"); | |
} | |
} |
I am getting this error... Can you please show step by step how to add this? I installed Quick add, created the macro, saved this js script into the vault, selected it. But This is the error I keep getting:
@yarub123 Did you open a chatgpt view in Obsidian Surfing?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
const customPrompt = await params.quickAddApi.suggester([
"",
"总结一下:",
"请根据以下的内容推荐几种不同的笔记方向:",
], [
"",
"总结一下:",
"请根据以下的内容推荐几种不同的笔记方向:",
]);