Skip to content

Commit

Permalink
migrated extra into main
Browse files Browse the repository at this point in the history
  • Loading branch information
RedMan13 committed Jul 1, 2023
1 parent 5967b39 commit d421688
Show file tree
Hide file tree
Showing 2 changed files with 224 additions and 233 deletions.
235 changes: 224 additions & 11 deletions extensions/godslayerakp/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,107 @@
'use strict';
if (!Scratch.extensions.unsandboxed) throw 'can not load out side unsandboxed mode';

const pathRegex = /[^.]+/g;
const setType = (value, type) => {
switch (type) {
case 'string':
switch (typeof value) {
case 'string':
case 'boolean':
case 'number':
case 'function':
return String(value);
case 'object':
try {
return JSON.stringify(value);
} catch {
return '{}';
}
}
break;
case 'number':
switch (typeof value) {
case 'string':
return String(value);
case 'boolean':
return Boolean(value);
case 'number':
return value;
case 'function':
case 'object':
return NaN;
}
break;
case 'boolean':
switch (typeof value) {
case 'string':
case 'boolean':
case 'function':
case 'number':
return Boolean(value);
case 'object':
return false;
}
break;
case 'object':
switch (typeof value) {
case 'string':
try {
const parsed = JSON.parse(value);
if (typeof parsed === 'object') return parsed;
return {};
} catch {
return {};
}
case 'boolean':
case 'function':
case 'number':
return {};
case 'object':
return value;
}
break;
}
};
const parseType = text => {
// this isnt text and we just pass it down as what ever it is
if (typeof text !== 'string') return text;
if (!isNaN(Number(text))) {
return Number(text);
} else {
try {
const parsed = JSON.parse(text);
if (typeof parsed === 'object') return parsed;
if (typeof parsed === 'boolean') return parsed;
return text;
} catch {
return text;
}
}
};
const getPathArray = path => {
const names = path.match(pathRegex);
for (let index = 0; index < names.length; index++) {
let name = names[index];
name = name.replaceAll(/(?<!\\)&dot/g, '.');
}
return names;
};
const getValueAtPath = (object, path) => {
for (const name of path) {
object = object[name];
if (typeof object !== 'object') return;
}
return object;
};
const setValueAtPath = (object, path, value) => {
for (const name of path) {
object = object[name];
if (typeof object !== 'object') return;
}
return object = value;
};

const {vm} = Scratch;
const {runtime} = vm;

Expand Down Expand Up @@ -105,6 +206,7 @@
*/
constructor() {
this.clearAll();
this.showingExtra = false
}
getInfo() {
return {
Expand Down Expand Up @@ -262,9 +364,85 @@
}
},
text: 'send request to [url]'
},
{
func: 'showExtra',
blockType: BlockType.BUTTON,
text: 'show extra',
hideFromPalette: this.showingExtra
},
{
func: 'hideExtra',
blockType: BlockType.BUTTON,
text: 'hide extra',
hideFromPalette: !this.showingExtra
},
{
opcode: 'setUnkownProperty',
blockType: BlockType.COMMAND,
arguments: {
path: {
type: ArgumentType.STRING,
defaultValue: 'path.to.item'
},
value: {
type: ArgumentType.STRING,
defaultValue: 'your mom :trel:'
}
},
text: 'set [path] to [value] in request options',
hideFromPalette: !this.showingExtra
},
{
opcode: 'setUnkownPropertyType',
blockType: BlockType.COMMAND,
arguments: {
path: {
type: ArgumentType.STRING,
defaultValue: 'path.to.item'
},
type: {
type: ArgumentType.STRING,
menu: 'jsTypes'
}
},
text: 'set [path] to type [type] in request options',
hideFromPalette: !this.showingExtra
},
{
opcode: 'getUnkownProperty',
blockType: BlockType.REPORTER,
arguments: {
path: {
type: ArgumentType.STRING,
defaultValue: 'path.to.item'
}
},
text: 'get [path] in request options',
hideFromPalette: !this.showingExtra
},
{
opcode: 'getUnkownPropertyType',
blockType: BlockType.REPORTER,
arguments: {
path: {
type: ArgumentType.STRING,
defaultValue: 'path.to.item'
}
},
text: 'get type of [path] in request options',
hideFromPalette: !this.showingExtra
}
],
menus: {
jsTypes: {
items: [
'string',
'number',
'boolean',
'object'
]
},
method: {
items: [
'GET',
Expand Down Expand Up @@ -306,15 +484,6 @@
"video/webm"
],
acceptReporters: true
},
jsTypes: {
items: [
'string',
'number',
'boolean',
'object'
],
acceptReporters: true
}
}
};
Expand Down Expand Up @@ -451,11 +620,55 @@
this.request.events.activate('reqFail');
});
}

/* extra stuff for when its missing something */

showExtra() {
this.showingExtra = true;
vm.extensionManager.refreshBlocks();
}

hideExtra() {
this.showingExtra = false;
vm.extensionManager.refreshBlocks();
}

setUnkownProperty(args) {
const name = Cast.toString(args.name);
const text = Cast.toString(args.value);

const path = getPathArray(name);
const value = parseType(text);
setValueAtPath(this.request.options, path, value);
}

setUnkownPropertyType(args) {
const name = Cast.toString(args.name);
const type = Cast.toString(args.type);
const path = getPathArray(name);

const oldValue = getValueAtPath(this.request.options, path);
const newValue = setType(oldValue, type);
setValueAtPath(this.request.options, path, newValue);
}

getUnkownProperty(args) {
const name = Cast.toString(args.name);
const path = getPathArray(name);

return getValueAtPath(this.request.options, path);
}

getUnkownPropertyType(args) {
const name = Cast.toString(args.name);
const path = getPathArray(name);
const value = getValueAtPath(this.request.options, path);

return typeof value;
}
}

const instance = new WebRequests();
Scratch.extensions.register(instance);
// @ts-ignore
runtime.ext_http = instance;
// @ts-ignore
})(Scratch);
Loading

0 comments on commit d421688

Please sign in to comment.