-
Notifications
You must be signed in to change notification settings - Fork 0
/
PageBuilder.lib.user.js
196 lines (163 loc) · 7.07 KB
/
PageBuilder.lib.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
var PageBuilder = (function () {
function info (){
const name = "PageBuilder.lib.user.js";
const version = "0.4.0";
const description = "A Simple Page builder for moodle.bbbaden.ch";
const author = "PianoNic";
const homepageURL = "";
return {
name: name,
version: version,
description: description,
author: author,
homepageURL: homepageURL
};
}
// Setup Custom Functions
Document.prototype.selectPageContent = function() {
return document.getElementById('page-content');
};
Document.prototype.clearPageContent = function() {
var pageContent = document.getElementById('page-content');
pageContent.innerHTML = "";
};
function prepare404Page(title, headerText){
// Checks if the page is a 404 error page
var keywordsContent = document.querySelector('meta[name="keywords"]').getAttribute('content');
if(!keywordsContent.includes("404")){
console.error(new Error("This function can only be called on the 404 page!"));
console.log("");
return
}
// Change Website Title
document.title = title;
// Change Page header
var pageHeader = document.getElementById('page-header');
var errorHeading = pageHeader.querySelector('h1.h2');
if (errorHeading) {
errorHeading.innerHTML = headerText;
}
// Clear Page Content
document.clearPageContent();
}
// function addElement(tag, text = null, className = null) {
// var pageContent = document.getElementsByClassName('custom-content')[0];
// var element = document.createElement(tag);
// if (text) {
// element.textContent = text;
// }
// if (className) {
// element.classList.add(className);
// }
// pageContent.appendChild(element);
// }
// function addButton(buttonText, jsCode, className = null) {
// const customContentDiv = document.querySelector('.custom-content');
// if (customContentDiv) {
// const button = document.createElement('button');
// button.innerText = buttonText || 'Click me!';
// button.classList.add("btn", "btn-outline-secondary", "btn-sm");
// if(className){
// button.classList.add(className);
// }
// button.addEventListener('click', function() {
// eval(jsCode);
// });
// customContentDiv.appendChild(button);
// }
// }
// function addTextField(id, className = null){
// var pageContent = document.getElementsByClassName('custom-content')[0];
// var inputElement = document.createElement('input');
// inputElement.type = 'text';
// inputElement.id = id;
// inputElement.classList.add("form-control", "mb-1");
// if(className){
// inputElement.classList.add(className);
// }
// pageContent.appendChild(inputElement);
// }
// function addLine() {
// var newDiv = document.createElement('div');
// newDiv.id = 'line';
// newDiv.className = 'nav-tabs h2';
// var pageContent = document.querySelector('.custom-content');
// if (pageContent) {
// pageContent.appendChild(newDiv);
// } else {
// console.error('.custom-content element not found.');
// }
// }
// function addHTML(html){
// var pageContent = document.getElementsByClassName('custom-content')[0];
// pageContent.innerHTML += html
// }
// function updateInstallationStatus(scriptName, scriptVersion) {
// // Find the table row with the matching script name
// var pageContent = document.getElementsByClassName('custom-content')[0];
// var table = pageContent.querySelector('table');
// if (table) {
// var bodyRows = table.querySelectorAll('tbody tr');
// bodyRows.forEach(row => {
// var installedScriptName = row.querySelector('td:nth-child(2)').textContent.trim();
// if (installedScriptName === scriptName) {
// // Update the status cell with "Installed"
// row.querySelector('td:last-child').textContent = 'Installed';
// return; // Break the loop since the script is found
// }
// });
// }
// }
function addExtensionInstallationTable() {
// Fetch the table from the given URL
fetch('https://raw.githubusercontent.com/BBBaden-Moodle-userscripts/BBBaden-Moodle/main/AllProjects.md')
.then(response => response.text())
.then(data => {
// Parse the markdown content into HTML
const parser = new DOMParser();
const doc = parser.parseFromString(data, 'text/html');
// Extract the table
const table = doc.querySelector('table');
// Set styles to make the table use the full width
table.style.width = '100%';
table.style.borderCollapse = 'collapse';
// Add space between each line (transparent border)
const tbody = table.querySelector('tbody');
if (tbody) {
const tableRows = tbody.querySelectorAll('tr');
tableRows.forEach(row => {
row.style.borderBottom = '4px solid transparent';
});
}
// Append the table to the specified div
var pageContent = document.getElementsByClassName('custom-content')[0];
// Add two new columns at the end of each row
const headerRow = table.querySelector('thead tr');
headerRow.innerHTML += '<th>Installed Status</th>';
const bodyRows = table.querySelectorAll('tbody tr');
bodyRows.forEach(row => {
// Convert all "Install" links to buttons
const installLink = row.querySelector('td:last-child a');
installLink.outerHTML = '<a href="' + installLink.href + '"><button class="btn btn-outline-secondary btn-sm text-nowrap h2 install-button">Install</button></a>';
// Add "Installed Status" column with default value "Not Installed"
row.innerHTML += '<td>Not Installed</td>';
});
// Append the table to the div
pageContent.appendChild(table);
})
.catch(error => {
console.error('Error fetching or appending table:', error);
});
}
return {
info: info,
prepare404Page: prepare404Page,
// addElement: addElement,
// addButton: addButton,
// addTextField: addTextField,
// addLine: addLine,
// addHTML: addHTML,
addExtensionInstallationTable: addExtensionInstallationTable,
// updateInstallationStatus: updateInstallationStatus,
};
})();