Skip to content

Instantly share code, notes, and snippets.

@Quorafind
Created February 19, 2023 11:20
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Quorafind/c70c6c698feeed66465d59efc39e4e1c to your computer and use it in GitHub Desktop.
Save Quorafind/c70c6c698feeed66465d59efc39e4e1c to your computer and use it in GitHub Desktop.
Using Quickadd to ask current selection In ChatGPT
// 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");
}
}
@temberature
Copy link

image
const customPrompt = await params.quickAddApi.suggester([
"",
"总结一下:",
"请根据以下的内容推荐几种不同的笔记方向:",
], [
"",
"总结一下:",
"请根据以下的内容推荐几种不同的笔记方向:",
]);

@yarub123
Copy link

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:
image

@Quorafind
Copy link
Author

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: image

@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