Skip to content

Commit 09d1eef

Browse files
committed
Relevant forum posts
1 parent 75a9359 commit 09d1eef

6 files changed

Lines changed: 110 additions & 2 deletions

File tree

api/main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ ScratchTools.disable = function (feature) {
288288
el.self.enabled = false
289289
})
290290
ScratchTools.managedElements.filter((el) => el.feature === feature).forEach(function(el) {
291-
el.previousDisplay = el.element.style.display
291+
if (!el.element) return;
292+
el.previousDisplay = el.element?.style.display
292293
el.element.style.display = "none"
293294
})
294295
ste.console.log(`Disabled ${feature}.`, "ste-main");

api/module.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ ScratchTools.injectModule = async function (script) {
7474
el.self.enabled = true
7575
})
7676
ScratchTools.managedElements.filter((el) => el.feature === script.feature.id).forEach(function(el) {
77-
el.element.style.display = el.previousDisplay || null
77+
if (!el.element) return;
78+
el.element.style.display = el?.previousDisplay || null
7879
})
7980
allEnableFunctions[script.feature.id]?.();
8081
}

features/features.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
[
2+
{
3+
"version": 2,
4+
"id": "relevant-forum-posts",
5+
"versionAdded": "v3.4.0"
6+
},
27
{
38
"version": 2,
49
"id": "wrap-lists",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"title": "Show Relevant Forum Posts",
3+
"description": "When creating a new forum topic, shows other topics that may be similar to what you are creating.",
4+
"credits": [
5+
{ "username": "rgantzos", "url": "https://scratch.mit.edu/users/rgantzos/" }
6+
],
7+
"type": ["Forums"],
8+
"tags": ["New"],
9+
"dynamic": true,
10+
"scripts": [{ "file": "script.js", "runOn": "/discuss/*" }],
11+
"styles": [{ "file": "style.css", "runOn": "/discuss/*" }]
12+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
export default async function ({ feature, console }) {
2+
if (window.location.pathname.split("/")[4] !== "add") return;
3+
4+
let input = await ScratchTools.waitForElement("input#id_name");
5+
input.addEventListener("focusout", async function () {
6+
if (!feature.self.enabled) return;
7+
if (!input.value) return;
8+
let data = await fetchRelevantPosts(input.value);
9+
if (document.querySelector(".ste-relevant-posts")) {
10+
document.querySelector(".ste-relevant-posts").remove();
11+
}
12+
if (data.length !== 0) {
13+
data.length = 10;
14+
let div = document.createElement("div");
15+
div.className = "ste-relevant-posts";
16+
feature.self.hideOnDisable(div)
17+
18+
let postIds = [];
19+
20+
data.forEach(function (post) {
21+
if (!postIds.find((el) => el === post.topic.id)) {
22+
postIds.push(post.topic.id);
23+
let a = document.createElement("a");
24+
a.href = `/discuss/topic/${post.topic.id}/`;
25+
26+
let h3 = document.createElement("h3");
27+
h3.textContent = post.topic.title;
28+
29+
let p = document.createElement("p");
30+
p.innerHTML = post.content.html;
31+
p.textContent = p.textContent.slice(0, 100);
32+
33+
let category = document.createElement("p");
34+
category.textContent = post.topic.category;
35+
category.className = "ste-relevant-category"
36+
37+
a.appendChild(h3);
38+
a.appendChild(p);
39+
a.appendChild(category);
40+
41+
div.appendChild(a);
42+
}
43+
});
44+
45+
input.parentNode.parentNode.insertBefore(
46+
div,
47+
input.parentNode.nextSibling
48+
);
49+
}
50+
});
51+
52+
async function fetchRelevantPosts(content) {
53+
let data = await (
54+
await fetch(
55+
`https://scratchdb.lefty.one/v3/forum/search/?q=${content
56+
.replaceAll("?", "")
57+
.replaceAll("#", "")
58+
.replaceAll("&", "")}&o=relevance&page=0`
59+
)
60+
).json();
61+
return data.posts;
62+
}
63+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.ste-relevant-posts {
2+
display: flex;
3+
flex-wrap: nowrap;
4+
overflow-x: auto;
5+
margin-bottom: 1rem;
6+
}
7+
8+
.ste-relevant-posts > a {
9+
width: 20rem;
10+
display: inline-block;
11+
flex-shrink: 0;
12+
flex-grow: 0;
13+
margin-right: 0.8rem;
14+
padding: 0.5rem;
15+
border: 1px solid #ccc;
16+
border-radius: 3px;
17+
}
18+
19+
.ste-relevant-posts *:hover {
20+
text-decoration: none;
21+
}
22+
23+
.ste-relevant-category {
24+
color: #55474790;
25+
margin-top: .5rem;
26+
}

0 commit comments

Comments
 (0)