Skip to content

Commit 3630c9d

Browse files
authored
Merge pull request #1011 from STForScratch/MaterArc-patch-16
Compare languages
2 parents cd3a5b5 + caadf78 commit 3630c9d

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

features/localized-explore/data.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"title": "Projects from Country",
3-
"description": "On the explore page, makes projects from other countries less visible. This is to only show projects in languages that you understand.",
3+
"description": "On the explore page, makes projects from countries that speak other languages less visible.",
44
"credits": [
5+
{ "username": "MaterArc", "url": "https://scratch.mit.edu/users/MaterArc/" },
56
{ "username": "KitsunLilly", "url": "https://scratch.mit.edu/users/KitsunLilly/" },
67
{ "username": "rgantzos", "url": "https://scratch.mit.edu/users/rgantzos/" }
78
],

features/localized-explore/script.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,27 @@ export default async function ({ feature, console }) {
99
).json()
1010
).profile.country;
1111

12-
ScratchTools.waitForElements("div.thumbnail.project", detectCountry);
12+
async function getLanguage(country) {
13+
try {
14+
const res = await fetch(
15+
`https://data.scratchtools.app/language/${encodeURIComponent(country)}?fullText=true`
16+
);
17+
if (!res.ok) return null;
18+
const data = await res.json();
19+
if (data && data[0] && data[0].languages) {
20+
return Object.values(data[0].languages)[0];
21+
}
22+
return null;
23+
} catch {
24+
return null;
25+
}
26+
}
1327

14-
async function detectCountry(element) {
28+
const myLanguage = await getLanguage(myCountry);
29+
30+
ScratchTools.waitForElements("div.thumbnail.project", detectLanguage);
31+
32+
async function detectLanguage(element) {
1533
let author = element.querySelector(".thumbnail-creator a");
1634

1735
let data = await (
@@ -23,7 +41,9 @@ export default async function ({ feature, console }) {
2341
)
2442
).json();
2543

26-
if (data.profile.country !== myCountry) {
44+
const authorLanguage = await getLanguage(data.profile.country);
45+
46+
if (authorLanguage !== myLanguage) {
2747
element.classList.add("ste-outside-country");
2848
if (feature.settings.get("hide-completely")) {
2949
element.classList.add("ste-country-hide");
@@ -35,7 +55,6 @@ export default async function ({ feature, console }) {
3555

3656
feature.settings.addEventListener("changed", function ({ key, value }) {
3757
if (key === "hide-completely") {
38-
console.log(value);
3958
if (value) {
4059
document
4160
.querySelectorAll(".ste-outside-country")
@@ -51,6 +70,6 @@ export default async function ({ feature, console }) {
5170
});
5271

5372
feature.addEventListener("enabled", function() {
54-
ScratchTools.waitForElements("div.thumbnail.project", detectCountry);
73+
ScratchTools.waitForElements("div.thumbnail.project", detectLanguage);
5574
})
5675
}

0 commit comments

Comments
 (0)