-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeelingua.user.js
84 lines (73 loc) · 3.13 KB
/
beelingua.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// ==UserScript==
// @name Beelingua Helper
// @namespace https://github.com/folfcoder/beelingua
// @downloadURL https://github.com/folfcoder/beelingua/raw/main/beelingua.user.js
// @updateURL https://github.com/folfcoder/beelingua/raw/main/beelingua.user.js
// @version 0.3
// @description Show answer keys for Beelingua.
// @author Kai Folf
// @match https://newbinusmaya.binus.ac.id/beelingua/*
// @grant GM_log
// ==/UserScript==
(function () {
"use strict";
// Constants
const ENDPOINT = "https://beelingua.folfcoder.workers.dev";
// Variables
let lang, course, unit, title, number;
// Function to extract information from the page
function extractInfo() {
const unitElem = document.querySelector("h5");
const unitText = unitElem.textContent;
title = document.querySelector("h1").textContent;
const [langPart, titlePart, unitPart] = unitText.split(" - ");
lang = langPart.split("-")[0].trim();
course = langPart.split("-")[1].trim();
unit = unitPart.trim();
GM_log("[DEBUG] Bahasa: " + lang);
GM_log("[DEBUG] Course: " + course);
GM_log("[DEBUG] Unit: " + unit);
GM_log("[DEBUG] Title: " + title);
}
// Function to fetch and highlight answers
function fetchAndHighlightAnswers() {
const elements = document.querySelectorAll(".secondary-color");
elements.forEach((element) => {
const newNum = parseInt(element.textContent);
if (!isNaN(newNum) && newNum !== number) {
number = newNum;
GM_log("[DEBUG] Nomor: " + number);
fetch(`${ENDPOINT}/${lang}/${course}/${unit}/${title}/${number}`)
.then((response) => response.json())
.then((data) => {
GM_log("[DEBUG] Jawaban: " + data);
// Multiple choice
const options = document.querySelectorAll(".bl-content-center");
options.forEach((option) => {
if (data.includes(option.textContent)) {
option.style.border = "0.3rem solid rgb(255, 0, 255)";
}
});
// Text
if (data[0].length > 1) {
const textarea = document.querySelector('textarea[placeholder="Type your answer here"]');
if (textarea) {
textarea.placeholder="Beelingua Helper: Answer copied to clipboard!";
}
navigator.clipboard.writeText(data[0]);
}
});
}
});
}
// Create a MutationObserver to watch for changes in the DOM
const observer = new MutationObserver(() => {
extractInfo();
fetchAndHighlightAnswers();
});
// Start observing changes to the entire document
observer.observe(document.documentElement, {
childList: true,
subtree: true,
});
})();