2

I don't expect many people to know this, but is there a way to check if a Scratch project ID is of a project that is shared? For instance, the project ID 3 is an actual project, but is not shared - while the ID 399293697 is shared. So how can I see if these are shared or not using JavaScript?

Tilier
  • 62
  • 7

1 Answers1

1

I searched the wiki article about the Scratch API, but that didn't help. The only useful endpoint mentioned there would require you to know the owner of the project.

By experimenting, I found these end points to work.

The first one returns HTTP 404 (not found), the second one HTTP 200 (OK) plus details about the project.

Notes:

  • I don't think there is a way to tell the difference between a non-shared project and a nonexistent project; they both yield 404.
  • I don't think you can directly access these endpoints from JavaScript running in a web browser, due to same-origin policy. On your own browser, you could temporarily turn that off. For a more structural solution, use a CORS proxy.

Below is a JavaScript sample that tests a single project ID for existence. It uses yacdn.org, one of the many free CORS proxies.

This is just a proof of concept. I cannot guarantee it will work for mass querying; many free proxies will throttle your traffic. It's probably better to host your own proxy.

function TestProjectId() {
    const id = document.getElementById('projectId').value;
    const url = 'https://yacdn.org/proxy/https://api.scratch.mit.edu/projects/' + id;
    fetch(url, {method: 'HEAD'})
        .then(response => { document.getElementById('answer').innerHTML = response.ok; });
}
<input type="text" id="projectId" value="399293697" />
<button onclick="TestProjectId()">Test it!</button>
<div id="answer">(answer will appear here)</div>
Ruud Helderman
  • 9,064
  • 1
  • 19
  • 39
  • How can I use this to help? – Tilier Jul 03 '20 at 21:51
  • @Tilier It depends on what exactly you are trying to achieve. Please edit your question to clarify this. What have you tried so far? What trouble have you run into? It often helps to share code you have already written. Anyway, I edited my answer, adding a code sample that may help as a proof of concept. Please let me know if anything is unclear. – Ruud Helderman Jul 04 '20 at 14:05
  • Oh!! Thank you so so so so much for your help!!! Your sample is extremely helpful. I seriously can not thank you enough – Tilier Jul 04 '20 at 21:38
  • 1
    Oh yes, sorry - I was going to but forgot – Tilier Jul 05 '20 at 14:19