Last active
August 25, 2025 01:43
-
-
Save raspberrypisig/99a273d5704fe4dabf3cb06834d834df to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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(); | |
} | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment