Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature: localized-explore #645

Merged
merged 2 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions api/feature.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,30 @@ class Feature {
console.error("ScratchTools feature event not found.");
}
};
var id = this.data.id || this.data.file
var options = this.data.options || []
this.settings = {
get: function (key) {
var settings = {};
options.forEach(function (el) {
if (ScratchTools.Storage[el.id]) {
settings[el.id] = ScratchTools.Storage[el.id];
}
});
if (key) {
return settings[key];
} else {
return settings;
}
},
addEventListener: function (event, callback) {
if (event === "changed") {
allSettingChangeFunctions[id] = callback;
} else {
console.error("ScratchTools feature event not found.");
}
}
}
this.getSettings = function (key) {
var settings = {};
this.data.options?.forEach(function (el) {
Expand Down
3 changes: 2 additions & 1 deletion extras/popup/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -726,8 +726,9 @@ async function getFeatures() {
world: "MAIN",
});
function updateSettingsFunction(feature, name, value) {
ScratchTools.Storage[name] = value
if (allSettingChangeFunctions[feature]) {
allSettingChangeFunctions[feature](name, value);
allSettingChangeFunctions[feature]({key: name, value});
}
}
} catch (err) {
Expand Down
5 changes: 5 additions & 0 deletions features/features.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
[
{
"version": 2,
"id": "localized-explore",
"versionAdded": "v3.2.0"
},
{
"version": 2,
"id": "project-bar",
Expand Down
2 changes: 1 addition & 1 deletion features/hide-advertisements/three.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if (ScratchTools.Storage.colorComment) {
}

var hideAds = new Feature({ id: "hide-advertisements" });
hideAds.addEventListener("settingChange", function (name, value) {
hideAds.settings.addEventListener("changed", function ({key: name, value}) {
if (name === "colorComment") {
if (value) {
document.body.classList.add("colorComment");
Expand Down
2 changes: 1 addition & 1 deletion features/hide-advertisements/two.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if (ScratchTools.Storage.colorComment) {
}

var hideAds = new Feature({ id: "hide-advertisements" });
hideAds.addEventListener("settingChange", function (name, value) {
hideAds.settings.addEventListener("changed", function ({key: name, value}) {
if (name === "colorComment") {
if (value) {
document.body.classList.add("colorComment");
Expand Down
2 changes: 1 addition & 1 deletion features/hide-block-category-names/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if (ScratchTools.Storage.displayAsCircle) {
document.body.classList.remove("st-category-circle")
}

hideBlockCategoryNames.addEventListener("settingChange", function(name, value) {
hideBlockCategoryNames.settings.addEventListener("changed", function({key: name, value}) {
console.log(name)
console.log(value)
if (name === "displayAsCircle") {
Expand Down
20 changes: 20 additions & 0 deletions features/localized-explore/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"title": "Projects from Country",
"description": "On the explore page, makes projects from other countries less visible. This is to only show projects in languages that you understand.",
"credits": [
{ "username": "EiramC", "url": "https://scratch.mit.edu/users/EiramC/" },
{ "username": "rgantzos", "url": "https://scratch.mit.edu/users/rgantzos/" }
],
"type": ["Website"],
"tags": ["New", "Featured"],
"dynamic": true,
"scripts": [
{ "file": "script.js", "runOn": "/explore/projects/*" },
{ "file": "script.js", "runOn": "/search/projects*" }
],
"styles": [
{ "file": "style.css", "runOn": "/explore/projects/*" },
{ "file": "style.css", "runOn": "/search/projects*" }
],
"options": [{ "id": "hide-completely", "name": "Hide Projects", "type": 1 }]
}
58 changes: 58 additions & 0 deletions features/localized-explore/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
export default async function ({ feature, console }) {
let myCountry = (
await (
await fetch(
`https://api.scratch.mit.edu/users/${feature.redux
.getState()
.session.session.user.username.replace("*", "")}/`
Comment on lines +5 to +7

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding High

This replaces only the first occurrence of "*".
)
).json()
).profile.country;

window.feature = feature;

ScratchTools.waitForElements("div.thumbnail.project", detectCountry);

async function detectCountry(element) {
let author = element.querySelector(".thumbnail-creator a");

let data = await (
await fetch(
`https://api.scratch.mit.edu/users/${author.textContent
.replaceAll("\n", "")
.replace("*", "")
Comment on lines +21 to +23

Check failure

Code scanning / CodeQL

Incomplete string escaping or encoding High

This replaces only the first occurrence of "*".
.replaceAll(" ", "")}/`
)
).json();

if (data.profile.country !== myCountry) {
element.classList.add("ste-outside-country");
if (feature.settings.get("hide-completely")) {
element.classList.add("ste-country-hide");
}
} else {
element.classList.add("ste-inside-country");
}
}

feature.settings.addEventListener("changed", function ({ key, value }) {
if (key === "hide-completely") {
console.log(value);
if (value) {
document
.querySelectorAll(".ste-outside-country")
.forEach(function (el) {
el.classList.add("ste-country-hide");
});
} else {
document.querySelectorAll(".ste-country-hide").forEach(function (el) {
el.classList.remove("ste-country-hide");
});
}
}
});

feature.addEventListener("enabled", function() {
ScratchTools.waitForElements("div.thumbnail.project", detectCountry);
})
}
7 changes: 7 additions & 0 deletions features/localized-explore/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.ste-outside-country {
opacity: .5;
}

.ste-country-hide {
display: none;
]