// ==UserScript==
// @name WASM Editor
// @namespace http://tampermonkey.net/
// @version 2024-06-05-11:43:06
// @description acces it in diep.io settings button; when in editor, press F1 for info
// @author todp
// @match https://diep.io/*
// @match https://staging.diep.io/*
// @match https://mobile.diep.io/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=diep.io
// @grant none
// @run-at document-start
// @license todp
// ==/UserScript==
if (!localStorage['weloadingwasm??@@44-!2d2']) localStorage['weloadingwasm??@@44-!2d2'] = false;
if (localStorage['weloadingwasm??@@44-!2d'] === 'true') {
localStorage['weloadingwasm??@@44-!2d'] = false;
modWasm();
}
if (localStorage['weloadingwasm??@@44-!2d2'] === 'true') {
modWasm();
}
function modWasm() {
const instant = WebAssembly.instantiate;
const strea = WebAssembly.instantiateStreaming;
WebAssembly.instantiateStreaming = (r, i) => r.arrayBuffer().then(b => WebAssembly.instantiate(b, i));
var buffer = new Uint8Array(localStorage['wasm22233f_@s'].split(","));
WebAssembly.instantiate = function (buf, imports) {
return instant(buffer, imports).then(function (wasm) {
WebAssembly.instantiate = instant;
WebAssembly.instantiateStreaming = strea;
window.instance = wasm.instance;
return wasm;
}).catch(function (err) {
throw err;
});
};
}
//for some reason i cant use it in @require since greasyfork says its a unapproved script, so i had to make this
(async function() {
window.ok = await fetch("https://cdn.jsdelivr.net/gh/AssemblyScript/wabt.js/index.js").then(e=>e.text());
eval(window.ok);
//
let idThisTime = parseFloat(Math.random().toFixed(3)).toString(13).slice(2,8);
let wasm2wat = null;
let wat2wasm = null;
WabtModule().then(function(wabt) {
window.wabt = wabt;
function compileText(contents) {
if (!contents) {
return;
}
try {
var module = wabt.readWasm(contents, {generateNames: false, readDebugNames: false, foldExprs: false, inlineExport: false});
var result = module.toText({foldExprs: false, inlineExport: false});
return result;
} catch (e) {
console.log('error', e)
}
}
wasm2wat = compileText;
window.wasm2wat = wasm2wat;
function compileWasm(contents) {
if (!contents) {
return;
}
try {
var module = wabt.parseWat('todp', contents, {generateNames: false, readDebugNames: false, foldExprs: false, inlineExport: false});
var result = module.toBinary({foldExprs: false, inlineExport: false});
return result;
} catch (e) {
console.log('error', e)
}
}
wat2wasm = compileWasm;
window.wat2wasm = wat2wasm;
});
const buildFetch = async () => {
const res = await fetch(location.origin + '/', { cache: "no-cache" });
const text = await res.text();
const JS_PATH = text.slice(text.indexOf("src=\"/index.") + "src=\"/".length, text.indexOf("\">", text.indexOf("src=\"/index.")));
const wasm2js = await fetch(location.origin + '/' + JS_PATH).then(res => res.text());
const WASM_PATH = wasm2js.slice(wasm2js.lastIndexOf("\"", wasm2js.indexOf(".wasm\"")) + 1, wasm2js.indexOf(".wasm\"")) + ".wasm"
return {JS_PATH, WASM_PATH}
};
buildFetch().then(async function(a){
let wasm = await fetch(location.origin + '/' + a.WASM_PATH);
let dat = await wasm.arrayBuffer();
let data = new Uint8Array(dat);
window.wat = wasm2wat(data);
window.wasm = data;
})
function moveUp(element){
var upperSibling = element.previousElementSibling;
if(upperSibling === null) return;
upperSibling.insertAdjacentElement("beforebegin", element);
}
let load = setInterval(()=>{
if (window.ui?.screen === 'home') {
if (document.querySelector("body > d-base")) {
if (document.querySelector("body > d-base").shadowRoot.querySelector("d-home").shadowRoot.querySelector("#social-links > d-button:nth-child(2)").onclick !== loadButton) {
document.querySelector("body > d-base").shadowRoot.querySelector("d-home").shadowRoot.querySelector("#social-links > d-button:nth-child(2)").onclick = loadButton;
}
}
}
});
function loadButton() {
let btn = document.querySelector("body > d-base").shadowRoot.querySelector("d-home").shadowRoot.querySelector("#settings > d-button:nth-child(4)");
let copy = btn.cloneNode(true);
copy.bg = '#a742f5';
copy.textContent = 'Edit WASM*'
copy.trigger = openWasmEditor;
document.querySelector("body > d-base").shadowRoot.querySelector("d-home").shadowRoot.querySelector("#settings").appendChild(copy);
moveUp(copy);
}
//modified abap to wat lol
window.todp = function() {
window.define("ace/mode/abap_highlight_rules",["require","exports","module","ace/lib/oop","ace/mode/text_highlight_rules"],function(e,i,t){"use strict";var o=e("../lib/oop"),r=e("./text_highlight_rules").TextHighlightRules,n=function(){this.$rules={start:[{regex:/[+\-]?(?:nan(?::0x[0-9a-fA-F]+)?|infinity|inf|0x[0-9a-fA-F]+\.?[0-9a-fA-F]*p[+\/-]?\d+|\d+(?:\.\d*)?[eE][+\-]?\d*|\d+\.\d*|0x[0-9a-fA-F]+|\d+)/,token:"constant.numeric"},{regex:/funcref|mut|nop|block|if|then|else|loop|br_if|br|br_table|call_indirect|call|drop|end|return|align|local\.get|local\.set|local\.tee|global\.get|global\.set|i64\.load|f32\.load|f64\.load|i32\.store8|i32\.store|i64\.store|f32\.store|f64\.store|i32\.load8_s|i64\.load8_s|i32\.load8_u|i64\.load8_u|i32\.load16_s|i32\.load|i64\.load16_s|i32\.load16_u|i64\.load16_u|i64\.load32_s|i64\.load32_u|i64\.store8|i32\.store16|i64\.store16|i32\.const|i64\.const|f32\.const|f64\.const|i32\.eqz|i64\.eqz|i32\.clz|i64\.clz|i32\.ctz|i64\.ctz|i32\.popcnt|i64\.popcnt|f32\.neg|f64\.neg|f32\.abs|f64\.abs|f32\.sqrt|f64\.sqrt|f32\.ceil|f64\.ceil|f32\.floor|f64\.floor|f32\.trunc|f64\.trunc|f32\.nearest|f64\.nearest|i32\.add|i64\.add|i32\.sub|i64\.sub|i32\.mul|i64\.mul|i32\.div_s|i64\.div_s|i32\.div_u|i64\.div_u|i32\.rem_s|i64\.rem_s|i32\.rem_u|i64\.rem_u|i32\.and|i64\.and|i32\.or|i64\.or|i32\.xor|i64\.xor|i32\.shl|i64\.shl|i32\.shr_s|i64\.shr_s|i32\.shr_u|i64\.shr_u|i32\.rotl|i64\.rotl|i32\.rotr|i64\.rotr|f32\.add|f64\.add|f32\.sub|f64\.sub|f32\.mul|f64\.mul|f32\.div|f64\.div|f32\.min|f64\.min|f32\.max|f64\.max|f32\.copysign|f64\.copysign|i32\.eq|i64\.eq|i32\.ne|i64\.ne|i32\.lt_s|i64\.lt_s|i32\.lt_u|i64\.lt_u|i32\.le_s|i64\.le_s|i32\.le_u|i64\.le_u|i32\.gt_s|i64\.gt_s|i32\.gt_u|i64\.gt_u|i32\.ge_s|i64\.ge_s|i32\.ge_u|i64\.ge_u|f32\.eq|f64\.eq|f32\.ne|f64\.ne|f32\.lt|f64\.lt|f32\.le|f64\.le|f32\.gt|f64\.gt|f32\.ge|f64\.ge|i64\.extend_i32_s|i64\.extend_i32_u|i32\.wrap_i64|i32\.trunc_f32_s|i64\.trunc_f32_s|i32\.trunc_f64_s|i64\.trunc_f64_s|i32\.trunc_f32_u|i64\.trunc_f32_u|i32\.trunc_f64_u|i64\.trunc_f64_u|f32\.convert_i32_s|f64\.convert_i32_s|f32\.convert_i64_s|f64\.convert_i64_s|f32\.convert_i32_u|f64\.convert_i32_u|f32\.convert_i64_u|f64\.convert_i64_u|f64\.promote_f32|f32\.demote_f64|f32\.reinterpret_i32|i32\.reinterpret_f32|f64\.reinterpret_i64|i64\.reinterpret_f64|select|unreachable|memory\.size|memory\.grow|type|func|param|result|local|global|module|table|memory|start|elem|data|offset|import|export|i32|i64|f32|f64/,token:"keyword"},{regex:/\$([a-zA-Z0-9_`\+\-\*\/\\\^~=<>!\?@#$%&|:\.]+)/,token:"variable"},{regex:/"(?:[^"\\\x00-\x1f\x7f]|\\[nt\\'"]|\\[0-9a-fA-F][0-9a-fA-F])*"/,token:"string"},{regex:/\(;.*?/,token:"comment",next:"comment"},{regex:/;;.*$/,token:"comment"}],comment:[{regex:/.*?;\)/,token:"comment",next:"start"},{regex:/.*/,token:"comment"},{defaultToken:"string"}]}};o.inherits(n,r),i.AbapHighlightRules=n}),window.define("ace/mode/folding/coffee",["require","exports","module","ace/lib/oop","ace/mode/folding/fold_mode","ace/range"],function(e,i,t){"use strict";var o=e("../../lib/oop"),r=e("./fold_mode").FoldMode,n=e("../../range").Range,f=i.FoldMode=function(){};o.inherits(f,r),(function(){this.commentBlock=function(e,i){var t=/\S/,o=e.getLine(i),r=o.search(t);if(-1!=r&&"#"==o[r]){for(var f=o.length,l=e.getLength(),s=i,a=i;++i<l;){var c=(o=e.getLine(i)).search(t);if(-1!=c){if("#"!=o[c])break;a=i}}if(a>s){var d=e.getLine(a).length;return new n(s,f,a,d)}}},this.getFoldWidgetRange=function(e,i,t){var o=this.indentationBlock(e,t);if(o||(o=this.commentBlock(e,t)))return o},this.getFoldWidget=function(e,i,t){var o=e.getLine(t),r=o.search(/\S/),n=e.getLine(t+1),f=e.getLine(t-1),l=f.search(/\S/),s=n.search(/\S/);if(-1==r)return e.foldWidgets[t-1]=-1!=l&&l<s?"start":"","";if(-1==l){if(r==s&&"#"==o[r]&&"#"==n[r])return e.foldWidgets[t-1]="",e.foldWidgets[t+1]="","start"}else if(l==r&&"#"==o[r]&&"#"==f[r]&&-1==e.getLine(t-2).search(/\S/))return e.foldWidgets[t-1]="start",e.foldWidgets[t+1]="","";return -1!=l&&l<r?e.foldWidgets[t-1]="start":e.foldWidgets[t-1]="",r<s?"start":""}}).call(f.prototype)}),window.define("ace/mode/wast",["require","exports","module","ace/mode/abap_highlight_rules","ace/mode/folding/coffee","ace/range","ace/mode/text","ace/lib/oop"],function(e,i,t){"use strict";function o(){this.HighlightRules=r,this.foldingRules=new n}var r=e("./abap_highlight_rules").AbapHighlightRules,n=e("./folding/coffee").FoldMode,f=(e("../range").Range,e("./text").Mode);e("../lib/oop").inherits(o,f),(function(){this.lineCommentStart='"',this.getNextLineIndent=function(e,i,t){return this.$getIndent(i)},this.$id="ace/mode/wast"}).call(o.prototype),i.Mode=o}),window.require(["ace/mode/wast"],function(e){"object"==typeof module&&"object"==typeof exports&&module&&(module.exports=e)});
}
window.saveWat = function() {
if (wat2wasm(editor.getValue()) !== undefined) {
const url=URL.createObjectURL(new Blob([editor.getValue()])),element=document.createElement("a");element.download=`diepwat_${idThisTime}.wat`,element.href=url,element.click();
}
else alert("There's an error. Check console")
}
window.useWat = function() {
var input = document.createElement('input');
input.type = 'file';
input.onchange = e => {
var file = e.target.files[0];
var reader = new FileReader();
reader.readAsText(file,'UTF-8');
reader.onload = readerEvent => {
var content = readerEvent.target.result;
if (wat2wasm(content) !== undefined) {
alert('Succesfully loaded WAT ' + file.name)
editor.setValue(content)
}
else alert('Thats not a valid wat file')
}
}
input.click();
}
window.toggleAutoLoad = function() {
let ch = document.getElementById('checkboxAutoLoad');
localStorage['weloadingwasm??@@44-!2d2'] = ch.checked;
}
window.fixtd = function(a) {
if (a==='true') return true;
if (a==='false') return false;
}
window.loadCustomWasm = function() {
if (wat2wasm(editor.getValue()) !== undefined) {
if (confirm('Are you sure') === true) {
localStorage['wasm22233f_@s'] = wat2wasm(editor.getValue()).buffer;
localStorage['weloadingwasm??@@44-!2d'] = true;
location.reload();
}
}
else alert("There's an error. Check console");
}
window.saveWasm = function() {
if (wat2wasm(editor.getValue()) !== undefined) {
localStorage['wasm22233f_@s'] = wat2wasm(editor.getValue()).buffer;
alert('Saved');
}
else alert("There's an error. Check console");
}
function openWasmEditor() {
window.stop();
document.open();
document.write(`
<!DOCTYPE html>
<head>
<style>
#editor {
position: absolute;
top: 20px;
right: 0;
bottom: 0;
left: 0;
}
#buttonSaveWat {
position: absolute;
top: 0px;
left: 0px;
}
#buttonUseWat {
position: absolute;
top: 0px;
left: 110px;
}
#buttonSaveWasm {
position: absolute;
top: 0px;
left: 199px;
}
#buttonLoadDiep {
position: absolute;
top: 0px;
left: 293px;
}
</style>
<button type="button" id="buttonSaveWat" onclick="window.saveWat()">Download WAT</button>
<button type="button" id="buttonUseWat" onclick="window.useWat()">Import WAT</button>
<button type="button" id="buttonSaveWasm" onclick="window.saveWasm()">Save WASM</button>
<button type="button" id="buttonLoadDiep" onclick="window.loadCustomWasm()">Load Diep</button>
<label style="position: absolute; top: 0px; left: 370px"><input type="checkbox" name="checkbox" id="checkboxAutoLoad" onclick="window.toggleAutoLoad()">Auto Load</label>
<label style="position: absolute; top: 0px; left: 500px">Editing:<select id="editingSelect"><option value="saved">Saved WASM</option><option value="default">Default Diep</option></select></label>
</head>
<body>
<div id="editor"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.34.2/ace.min.js"></script>
<script>
var editor = ace.edit("editor");
editor.setTheme("ace/theme/github_dark");
document.querySelector("#editor > div.ace_scroller > div.ace_content > div.ace_layer.ace_print-margin-layer > div").remove();
window.todp();
editor.session.setMode('ace/mode/abap')
editor.setValue(window.wasm2wat(new Uint8Array(localStorage['wasm22233f_@s'].split(","))));
document.querySelector("#editingSelect").onchange = function(e) {
let selected = e.srcElement.options[e.srcElement.selectedIndex].textContent;
if (selected === 'Default Diep') editor.setValue(window.wat);
if (selected === 'Saved WASM') editor.setValue(window.wasm2wat(new Uint8Array(localStorage['wasm22233f_@s'].split(","))));
}
document.querySelector("#checkboxAutoLoad").checked = fixtd(localStorage['weloadingwasm??@@44-!2d2'])
</script>
</body>
</html>
`);
document.close();
}
})();