Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate with leetcode to populate completed problems #155

Open
veeral-patel opened this issue Feb 11, 2022 · 4 comments
Open

Integrate with leetcode to populate completed problems #155

veeral-patel opened this issue Feb 11, 2022 · 4 comments

Comments

@veeral-patel
Copy link

Another idea is integrating with Leetcode's API, if one exists, to mark already completed problems on LC as done.

@tobias-wilfert
Copy link

The closest thing to an API Leetcode has is this: https://leetcode.com/api/problems/algorithms/
Which allows you to retrieve a JSON containing data about the problems (including if the logged-in user solved them).

In the meantime
You can grab the names of the questions you solved and then run the following code in the console on the website, this will check the boxes of all the questions you have already solved on Leetcode:

var solved = ['Contains Duplicate' ... ];
var tr = document.getElementsByTagName("tr");

// Skip the first 2 rows as they are not question rows
for (var i = 2; i < tr.length; i++) {
    // Check if in solved
    if(solved.includes(tr[i].children[1].children[0].innerText) === true){
        tr[i].children[0].children[0].children[0].click(); // Simulate a toggle
    }
}

@seanprashad
Copy link
Owner

Relates to #179

@seanprashad
Copy link
Owner

seanprashad commented Aug 6, 2022

With the introduction of #210, we should be able to accomplish this. I was thinking that we could retrieve all completed questions for a user and populate the checkboxes as such.

https://leetcode.com/discuss/general-discussion/1297705/is-there-public-api-endpoints-available-for-leetcode might have a few leads for us!

@mlcivilengineer
Copy link

The closest thing to an API Leetcode has is this: https://leetcode.com/api/problems/algorithms/ Which allows you to retrieve a JSON containing data about the problems (including if the logged-in user solved them).

In the meantime You can grab the names of the questions you solved and then run the following code in the console on the website, this will check the boxes of all the questions you have already solved on Leetcode:

var solved = ['Contains Duplicate' ... ];
var tr = document.getElementsByTagName("tr");

// Skip the first 2 rows as they are not question rows
for (var i = 2; i < tr.length; i++) {
    // Check if in solved
    if(solved.includes(tr[i].children[1].children[0].innerText) === true){
        tr[i].children[0].children[0].children[0].click(); // Simulate a toggle
    }
}

if you go to the site https://leetcode.com/api/problems/algorithms/ and paste the following code on the browser console while logged in on leetcode on another tab you can retrieve a list of all the names of the completed algorithms. Just copy the list and substitute the var solved with it and execute the code on the console and it will work :)

fetch('https://leetcode.com/api/problems/algorithms/')
  .then(response => response.json())
  .then(data => {
    const questionList = [];

    const statStatusPairs = data.stat_status_pairs;
    for (const pair of statStatusPairs) {
      if (pair.status === 'ac') {
        const questionTitle = pair.stat.question__title;
        questionList.push(questionTitle);
      }
    }

    console.log(questionList);
  })
  .catch(error => console.error('Error:', error));

I tried using the following code directly so I could skip copying the list and substituting the solved var, but was not able to make it work because of cors policy. As I am not a javascript dev nor a web developer, I gave up. If anyone knows how to circunvent the cors policy, feel free to modify the code:

function get_solved_questions() {
  return fetch('https://leetcode.com/api/problems/algorithms/')
    .then(response => response.json())
    .then(data => {
      const questionList = [];

      const statStatusPairs = data.stat_status_pairs;
      for (const pair of statStatusPairs) {
        if (pair.status === 'ac') {
          const questionTitle = pair.stat.question__title;
          questionList.push(questionTitle);
        }
      }

      return questionList;
    })
    .catch(error => {
      console.error('Error:', error);
      return [];
    });
}

var tr = document.getElementsByTagName("tr");

get_solved_questions()
  .then(solved => {
    // Skip the first 2 rows as they are not question rows
    for (var i = 2; i < tr.length; i++) {
      // Check if in solved
      if (solved.includes(tr[i].children[1].children[0].innerText)) {
        tr[i].children[0].children[0].children[0].click(); // Simulate a toggle
      }
    }
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants