How do you check if a Scratch project is shared by its ID?

情到浓时终转凉″ 提交于 2020-07-09 17:12:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!