Skip to content

Instantly share code, notes, and snippets.

@raspberrypisig
Last active August 25, 2025 01:43
Show Gist options
  • Save raspberrypisig/99a273d5704fe4dabf3cb06834d834df to your computer and use it in GitHub Desktop.
Save raspberrypisig/99a273d5704fe4dabf3cb06834d834df to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name blockly
// @namespace Mohan Scripts
// @match https://groklearning.com/blockly/frame.html
// @grant none
// @version 1.0
// @author -
// @description 24/08/2025, 6:29:49 pm
// @inject-into page
// ==/UserScript==
async function saveXmlFile(xmlBlob, suggestedFileName = 'blockly.xml') {
try {
// Feature detection for File System Access API
if ('showSaveFilePicker' in window) {
const handle = await window.showSaveFilePicker({
suggestedName: suggestedFileName,
types: [{
description: 'XML Files',
accept: {
'application/xml': ['.xml'],
},
}],
});
const writableStream = await handle.createWritable();
await writableStream.write(xmlBlob);
await writableStream.close();
console.log('XML file saved successfully!');
} else {
// Fallback for browsers not supporting File System Access API
// Create a temporary anchor element and trigger a download
const url = URL.createObjectURL(xmlBlob);
const a = document.createElement('a');
a.href = url;
a.download = suggestedFileName;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
console.log('XML file downloaded (using fallback).');
}
} catch (error) {
console.error('Error saving XML file:', error);
}
}
function loadBlockly(xmlText) {
const workspace = Blockly.getMainWorkspace();
workspace.clear();
const xmlDom = Blockly.Xml.textToDom(xmlText);
Blockly.Xml.domToWorkspace(xmlDom, workspace);
}
function dummyinputfiletype() {
if (!document.getElementById('fileInput')) {
const fileInput = document.createElement('input');
fileInput.type = 'file';
fileInput.id = 'fileInput';
fileInput.style.display = 'none';
document.body.appendChild(fileInput);
fileInput.addEventListener('change', (event) => {
const fileList = event.target.files; // FileList object containing selected File objects
if (fileList.length > 0) {
const selectedFile = fileList[0]; // Access the first selected file
// You can now read the file content using FileReader API
const reader = new FileReader();
reader.onload = (e) => {
const fileContent = e.target.result;
loadBlockly(fileContent);
console.log('File content:', fileContent);
};
reader.readAsText(selectedFile); // Or readAsDataURL, readAsArrayBuffer, etc.
}
});
}
}
function load() {
console.log("boo");
dummyinputfiletype();
document.querySelector('#fileInput').click();
}
function save() {
const workspace = Blockly.getMainWorkspace();
//console.log(Blockly);
const xmlDom = Blockly.Xml.workspaceToDom(workspace);
const xmlText = Blockly.Xml.domToText(xmlDom);
const blob = new Blob([xmlText], { type: 'text/xml' });
saveXmlFile(blob);
// const a = document.createElement('a');
// a.href = URL.createObjectURL(blob);
// a.download = 'blockly.xml';
// document.body.appendChild(a);
// a.click();
// document.body.removeChild(a); // Clean up
// URL.revokeObjectURL(a.href); // Release object URL
//console.log(xmlText);
}
window.addEventListener("message", (event) => {
if (event.data && event.data.type === "signal") {
if (event.data.data === "load") {
load();
}
if (event.data.data === "save") {
save();
}
}
});
// ==UserScript==
// @name SaveLoadButtons
// @namespace Violentmonkey Scripts
// @match https://groklearning.com/learn/aca-dt-56-bk-cookie/playgrounds/1/
// @grant none
// @version 1.0
// @author -
// @description 24/08/2025, 7:26:03 pm
// ==/UserScript==
(function() {
function boo() {
const element = document.querySelector("#slide-menu-tabs-bar");
const saveButton = document.createElement("a");
saveButton.id = "saveButton";
saveButton.classList.add("tab", "btn");
saveButton.textContent = "Save";
element.insertBefore(saveButton, element.children[1]);
const loadButton = document.createElement("a");
loadButton.id = "loadButton";
loadButton.classList.add("tab", "btn");
loadButton.textContent = "Load";
element.insertBefore(loadButton, element.children[1]);
console.log(loadButton);
saveButton.addEventListener('click', function() {
const iframe = document.querySelector("iframe");
const iframeWindow = iframe.contentWindow;
iframeWindow.postMessage({ type: "signal", data: "save" });
});
loadButton.addEventListener('click', function() {
const iframe = document.querySelector("iframe");
const iframeWindow = iframe.contentWindow;
iframeWindow.postMessage({ type: "signal", data: "load" });
});
}
setTimeout(boo, 5000);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment