Skip to content

Commit 9872d1f

Browse files
authored
Compare languages
Improve the feature to compare users by language instead of just the country, so users from different countries that share the same language (eg: US, UK, Australia) are included correctly.
1 parent 1224cac commit 9872d1f

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

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://restcountries.com/v3.1/name/${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)