What I’m trying to do
I create a note for each problem that I solve on LeetCode for noting down any clever ways of solving it. I have already set up templater to prompt me for a title for the note.
- In the prompt, I want to enter the number of the leetcode problem.
- Once I hit enter, the title should change to a predetermined format of mine which will include the number, the difficulty and the name of the problem.
- If possible, I want to get the link to the problem as well. I already figured out obtaining the link though, with a few string manipulation, so its not really necessary.
Things I have tried
I have tried writing the following async script that queries LeetCode’s GraphQL endpoint for the information:
async function getDeets(problemNumber) {
const query = `
query questionTitle($num: Int!) {
question(titleSlug: getTitleSlugByNumber(num: $num)) {
title
difficulty
}
}`;
const variables = {
num: problemNumber
};
const targetUrl = 'https://leetcode.com/graphql';
const response = await fetch(targetUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query,
variables
}),
});
const data = await response.json();
if (data.errors) {
console.error('Error fetching data:', data.errors);
return null;
}
return {
title: data.data.question.title,
difficulty: data.data.question.difficulty
};
}
but I get the following error:
Access to fetch at 'https://leetcode.com/graphql' from origin 'app://obsidian.md' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
I tried a solution which involves querying the endpoint via a CORS proxy server, but that did not work either.