问题
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?
回答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.
- https://api.scratch.mit.edu/projects/3
- https://api.scratch.mit.edu/projects/399293697
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>
来源:https://stackoverflow.com/questions/62707137/how-do-you-check-if-a-scratch-project-is-shared-by-its-id