URL <<
Previous Next >> Homework
Parameters
https://mdewcm2025.github.io/hw-scrum-1/reeborg/?lang=en&mode=python&menu=/worlds/menus/select_collection_en.json&name=Harvest1&url=/hw-scrum-1/reeborg/worlds/tutorial_en/harvest1.json&editor=/python/harvest1.py
lang=en
mode=python
menu=/reeborg/worlds/menus/select_collection_en.json
name=Harvest1
url=/hw-scrum-1/reeborg/worlds/tutorial_en/harvest1.json
add editor=/python/harvest1.json
Harvest2
Harvest3
function set_editor() {
"use strict";
const urlParams = new URLSearchParams(window.location.search);
const editorFilePath = urlParams.get("editor");
if (editorFilePath) {
// Fetch the content of the file specified in the `editor` parameter
fetch(editorFilePath)
.then(response => {
if (!response.ok) {
throw new Error("Failed to load file: " + response.statusText);
}
return response.text();
})
.then(fileContent => {
editor.setValue(fileContent);
})
.catch(error => {
console.error(error);
// Fallback to localStorage or default value if file fetch fails
loadFromLocalStorageOrDefault();
});
} else {
// Fallback to loading from localStorage or default value
loadFromLocalStorageOrDefault();
}
function loadFromLocalStorageOrDefault() {
if (localStorage.getItem("editor")) {
editor.setValue(localStorage.getItem("editor"));
} else {
editor.setValue(RUR.translate("move") + "()");
}
}
}
在近端靜態網站測試上列 set_editor:
editor 內容取自 reeborg/python/harvest1.py
https://127.0.0.1:8444/reeborg/?lang=en&mode=python&menu=/reeborg/worlds/menus/select_collection_en.json&name=Alone&url=/reeborg/worlds/tutorial_en/harvest1.json&editor=/reeborg/python/harvest1.py
editor 內容取自 Gist URL
https://127.0.0.1:8444/reeborg/?lang=en&mode=python&menu=/reeborg/worlds/menus/select_collection_en.json&name=Alone&url=/reeborg/worlds/tutorial_en/harvest1.json&editor=https://gist.githubusercontent.com/mdecycu/71eea94e7cc0def9b3620fc8374d2e8b/raw/44a73f0b397ae67927306f602779c9b5c5e25df0/w10_reeborg1.py
若希望可以從 Gist 中的 url 取得 python 程式碼, 可以將 function set_editor 修改如下:
function set_editor() {
"use strict";
const urlParams = new URLSearchParams(window.location.search);
const editorFilePath = urlParams.get("editor");
if (editorFilePath) {
// Check if the URL is a Gist URL
if (editorFilePath.includes('gist.github.com')) {
// Extract Gist ID from the URL
const gistId = editorFilePath.split('/')[4]; // Gist URL is like "https://gist.github.com/user/gistId"
const gistApiUrl = `https://api.github.com/gists/${gistId}`;
// Fetch content from Gist
fetch(gistApiUrl)
.then(response => {
if (!response.ok) {
throw new Error("Failed to load Gist: " + response.statusText);
}
return response.json();
})
.then(gistData => {
const fileName = Object.keys(gistData.files)[0]; // Get the first file in the Gist
const fileContent = gistData.files[fileName].content;
// Set the content into the editor
editor.setValue(fileContent);
// Set the Gist URL into fm_m4 input field
document.getElementById('fm_m4').value = `Gist URL: ${gistData.html_url}`;
})
.catch(error => {
console.error(error);
loadFromLocalStorageOrDefault();
});
} else {
// If it's not a Gist, treat it as a file URL
fetch(editorFilePath)
.then(response => {
if (!response.ok) {
throw new Error("Failed to load file: " + response.statusText);
}
return response.text();
})
.then(fileContent => {
editor.setValue(fileContent);
})
.catch(error => {
console.error(error);
loadFromLocalStorageOrDefault();
});
}
} else {
// Fallback to loading from localStorage or default value
loadFromLocalStorageOrDefault();
}
function loadFromLocalStorageOrDefault() {
if (localStorage.getItem("editor")) {
editor.setValue(localStorage.getItem("editor"));
} else {
editor.setValue(RUR.translate("move") + "()");
}
}
}
將程式在 https://github.com/mdecycu/reeborg 倉儲進行修改後, 可以利用下列 URL 進行驗證:
https://mde.tw/reeborg/?lang=en&mode=python&menu=/worlds/menus/select_collection_en.json&name=Other%20worlds&url=/reeborg/worlds/tutorial_en/harvest1.json&editor=/reeborg/python/harvest1.py
URL <<
Previous Next >> Homework