Skip to content

Commit f4e87a1

Browse files
authored
Update script.js
1 parent c55cad7 commit f4e87a1

File tree

1 file changed

+35
-86
lines changed

1 file changed

+35
-86
lines changed

Wiki/script.js

Lines changed: 35 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,40 @@
11
// DO NOT DELETE OR EDIT
2-
// Firebase configuration
3-
const firebaseConfig = {
4-
apiKey: "AIzaSyC_fh1xWlHdVJVjhHr-ZUy2sKWy9jilUHM",
5-
authDomain: "wiki-storage-51c29.firebaseapp.com",
6-
projectId: "wiki-storage-51c29",
7-
storageBucket: "wiki-storage-51c29.firebasestorage.app",
8-
messagingSenderId: "607647989206",
9-
appId: "1:607647989206:web:e838a6fe33fe5fc47b3877",
10-
measurementId: "G-MS4Y95QN1M"
11-
};
12-
13-
// Initialize Firebase
14-
firebase.initializeApp(firebaseConfig);
15-
const db = firebase.firestore();
16-
17-
// Fetch and display all wikis
18-
async function fetchWikis() {
19-
const wikiList = document.getElementById('wiki-list');
20-
wikiList.innerHTML = '';
21-
22-
const snapshot = await db.collection('wikis').get();
23-
snapshot.forEach(doc => {
24-
const wiki = doc.data();
25-
const li = document.createElement('li');
26-
li.innerHTML = `
27-
<strong>${wiki.title}</strong>
28-
<button onclick="viewWiki('${doc.id}')">📝 View</button>
29-
<button onclick="editWiki('${doc.id}', '${wiki.title}')">
30-
<img src="images.png" width="20" alt="Edit">
31-
</button>
32-
<button onclick="deleteWiki('${doc.id}')">
33-
<img src="delete-page.png" width="20" alt="Delete">
34-
</button>
35-
`;
36-
wikiList.appendChild(li);
37-
});
2+
// Fetch and display wikis from a static JSON file hosted on GitHub
3+
function fetchWikis() {
4+
const wikiList = document.getElementById('wiki-list');
5+
wikiList.innerHTML = '';
6+
7+
fetch('https://raw.githubusercontent.com/scratch-coding-hut/Scratch-Coding-Hut.github.io/main/wikis.json')
8+
.then(response => response.json())
9+
.then(wikis => {
10+
wikis.forEach((wiki, index) => {
11+
const li = document.createElement('li');
12+
li.innerHTML = `
13+
<strong>${wiki.title}</strong>
14+
<button onclick="viewWiki(${index})">📝 View</button>
15+
`;
16+
wikiList.appendChild(li);
17+
});
18+
})
19+
.catch(error => {
20+
console.error('Error loading wikis:', error);
21+
wikiList.innerHTML = 'Error loading wikis.';
22+
});
3823
}
3924

40-
// Create a new wiki
41-
async function createWiki() {
42-
const title = prompt('Enter wiki title:');
43-
const content = prompt('Enter HTML/CSS/JS content for this wiki:');
44-
45-
if (title && content) {
46-
await db.collection('wikis').add({ title, content });
47-
fetchWikis();
48-
}
49-
}
50-
51-
// Edit an existing wiki
52-
async function editWiki(id, title) {
53-
const content = prompt('Edit HTML/CSS/JS content:', '');
54-
55-
if (content) {
56-
await db.collection('wikis').doc(id).update({ title, content });
57-
fetchWikis();
58-
}
59-
}
60-
61-
// Delete a wiki
62-
async function deleteWiki(id) {
63-
if (confirm('Are you sure you want to delete this wiki?')) {
64-
await db.collection('wikis').doc(id).delete();
65-
fetchWikis();
66-
}
67-
}
68-
69-
// View a wiki’s content
70-
async function viewWiki(id) {
71-
const doc = await db.collection('wikis').doc(id).get();
72-
const wiki = doc.data();
73-
74-
const viewer = document.getElementById('wiki-viewer');
75-
viewer.innerHTML = `
76-
<h2>${wiki.title}</h2>
77-
<div>${wiki.content}</div>
78-
`;
79-
80-
// Execute any inline scripts in the content
81-
const scriptTags = viewer.querySelectorAll('script');
82-
scriptTags.forEach(script => {
83-
const newScript = document.createElement('script');
84-
newScript.textContent = script.textContent;
85-
document.body.appendChild(newScript);
86-
script.remove();
87-
});
25+
// View a specific wiki’s content
26+
function viewWiki(index) {
27+
fetch('https://raw.githubusercontent.com/scratch-coding-hut/Scratch-Coding-Hut.github.io/main/wikis.json')
28+
.then(response => response.json())
29+
.then(wikis => {
30+
const wiki = wikis[index];
31+
const viewer = document.getElementById('wiki-viewer');
32+
viewer.innerHTML = `
33+
<h2>${wiki.title}</h2>
34+
<div>${wiki.content}</div>
35+
`;
36+
});
8837
}
8938

90-
// Initial fetch
91-
fetchWikis();
39+
// Call fetchWikis when the page loads
40+
window.onload = fetchWikis;

0 commit comments

Comments
 (0)