|
| 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 | +} |
0 commit comments