Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New extension: Webhooks (TurboHook replacement) #1873

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
33 changes: 33 additions & 0 deletions docs/CubesterYT/Webhooks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Webhooks

This extension allows you to use [Webhooks](https://en.wikipedia.org/wiki/Webhook). It provides a simple way to save multiple Webhooks to your project for easy management and easy data handling.

These blocks are made with simplicity in mind so that your project isn't messy when you implement Webhooks.

## Adding a Webhook

When loading the extension, you are provided with a button, `Add a Webhook`, to make a new Webhook. It will prompt you to type in a name, which the Webhook registers under, and then prompt you to provide the URL of the Webhook. Please make sure the Webhook URL is valid before making the Webhook, else the extension can't send anything.

## Deleting a Webhook

You can delete a specific Webhook by pressing the `Delete a Webhook` button, where you will be prompted to enter the name of the Webhook you wish to delete.

## Blocks

```scratch
(data of [Webhook v] :: #C73A63)
```

This block reports the data the Webhook chosen from the dropdown is currently assigned to.

```scratch
set data of [Webhook v] to [{"key":"value"}] and type to [application/json v] :: #C73A63
```

This block is used to set data to the chosen Webhook from the list. It only supports JSON inputs, but using the second dropdown, you can make the content type either `application/json` or `application/x-www-form-urlencoded`. The extension will automatically handle conversions for your ease.

```scratch
send data to [Webhook v] :: #C73A63
```

This block sends the data that is set for the selected Webhook.
225 changes: 225 additions & 0 deletions extensions/CubesterYT/Webhooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
// Name: Webhooks
// ID: cubesterWebhooks
// Description: A modern, very capable, Webhook extension.
// By: CubesterYT <https://scratch.mit.edu/users/CubesterYT/>
// License: MPL-2.0

// Version V.1.0.0

(function (Scratch) {
"use strict";

const icon = "";

let hideFromPalette = true;
let webhooks;
function setupStorage() {
CubesterYT marked this conversation as resolved.
Show resolved Hide resolved
if (!Scratch.vm.runtime.extensionStorage["cubesterWebhooks"]) {
Scratch.vm.runtime.extensionStorage["cubesterWebhooks"] =
Object.create(null);
Scratch.vm.runtime.extensionStorage["cubesterWebhooks"].webhooks =
Object.create(null);
}
webhooks = Scratch.vm.runtime.extensionStorage["cubesterWebhooks"].webhooks;
}
setupStorage();
Scratch.vm.runtime.on("PROJECT_LOADED", setupStorage);
CubesterYT marked this conversation as resolved.
Show resolved Hide resolved

function toForm(data) {
const parts = [];
const process = (name, value) => {
CubesterYT marked this conversation as resolved.
Show resolved Hide resolved
if (value === null || value === undefined) {
parts.push(`${encodeURIComponent(name)}=null`);
} else if (
typeof value === "string" ||
typeof value === "number" ||
typeof value === "boolean"
) {
parts.push(`${encodeURIComponent(name)}=${encodeURIComponent(value)}`);
} else if (Array.isArray(value)) {
value.forEach((elem, index) => process(`${name}[${index}]`, elem));
} else if (typeof value === "object") {
Object.keys(value).forEach((key) =>
process(`${name}[${key}]`, value[key])
);
}
};
Object.keys(data).forEach((key) => process(key, data[key]));
return parts.join("&");
}

class Webhooks {
getInfo() {
return {
id: "cubesterWebhooks",
name: Scratch.translate("Webhooks"),
color1: "#C73A63",
menuIconURI: icon,
docsURI: "https://extensions.turbowarp.org/CubesterYT/Webhooks",

blocks: [
"---",

{
func: "addWebhook",
text: Scratch.translate("Add a Webhook"),
blockType: Scratch.BlockType.BUTTON,
},
{
func: "deleteWebhook",
text: Scratch.translate("Delete a Webhook"),
blockType: Scratch.BlockType.BUTTON,
hideFromPalette,
},
{
opcode: "data",
text: Scratch.translate("data of [WEBHOOK]"),
blockType: Scratch.BlockType.REPORTER,
disableMonitor: true,
hideFromPalette,
arguments: {
WEBHOOK: {
type: Scratch.ArgumentType.STRING,
menu: "WEBHOOKS",
},
},
},
{
opcode: "setData",
text: Scratch.translate(
"set data of [WEBHOOK] to [JSON] and type to [TYPE]"
),
blockType: Scratch.BlockType.COMMAND,
hideFromPalette,
arguments: {
WEBHOOK: {
type: Scratch.ArgumentType.STRING,
menu: "WEBHOOKS",
},
JSON: {
type: Scratch.ArgumentType.STRING,
defaultValue: '{"key":"value"}',
},
TYPE: {
type: Scratch.ArgumentType.STRING,
menu: "TYPE",
},
},
},
{
opcode: "postData",
text: Scratch.translate("send data to [WEBHOOK]"),
blockType: Scratch.BlockType.COMMAND,
hideFromPalette,
arguments: {
WEBHOOK: {
type: Scratch.ArgumentType.STRING,
menu: "WEBHOOKS",
},
},
},
],
menus: {
WEBHOOKS: {
acceptReporters: false,
items: "_webhookMenu",
},
TYPE: {
acceptReporters: false,
items: ["application/json", "application/x-www-form-urlencoded"],
},
},
};
}

addWebhook() {
let name;
do {
name = prompt(Scratch.translate("Enter Webhook name:"));
if (name === null) {
return;
}
if (Object.keys(webhooks).includes(name)) {
alert(
Scratch.translate(
"This Webhook already exists! Please try a different name."
)
);
}
} while (Object.keys(webhooks).includes(name));
let URL = prompt(Scratch.translate("Enter Webhook URL:"));
webhooks[name] = { URL, DATA: {}, TYPE: "application/json" };
hideFromPalette = false;
Scratch.vm.extensionManager.refreshBlocks();
}
deleteWebhook() {
let name;
do {
name = prompt(Scratch.translate("Enter Webhook name:"));
if (name === null) {
return;
}
if (!Object.keys(webhooks).includes(name)) {
CubesterYT marked this conversation as resolved.
Show resolved Hide resolved
alert(
Scratch.translate(
"This Webhook does not exist. Please enter a valid webhook name."
)
);
}
} while (!Object.keys(webhooks).includes(name));
delete webhooks[name];
if (Object.keys(webhooks).length === 0) {
hideFromPalette = true;
Scratch.vm.extensionManager.refreshBlocks();
}
}
data(args) {
try {
switch (webhooks[args.WEBHOOK].TYPE) {
case "application/json":
CubesterYT marked this conversation as resolved.
Show resolved Hide resolved
return JSON.stringify(webhooks[args.WEBHOOK].DATA);
case "application/x-www-form-urlencoded":
return toForm(webhooks[args.WEBHOOK].DATA);
}
} catch (error) {
return "";
}
}
setData(args) {
try {
webhooks[args.WEBHOOK].DATA = JSON.parse(
Scratch.Cast.toString(args.JSON)
);
webhooks[args.WEBHOOK].TYPE = args.TYPE;
CubesterYT marked this conversation as resolved.
Show resolved Hide resolved
} catch (error) {
return;
}
}
postData(args) {
try {
Scratch.fetch(webhooks[args.WEBHOOK].URL, {
method: "POST",
headers: {
"Content-type": webhooks[args.WEBHOOK].TYPE,
},
body:
webhooks[args.WEBHOOK].TYPE === "application/json"
? JSON.stringify(webhooks[args.WEBHOOK].DATA)
: toForm(webhooks[args.WEBHOOK].DATA),
});
} catch (error) {
return;
}
}

_webhookMenu() {
if (Object.keys(webhooks).length > 0) {
CubesterYT marked this conversation as resolved.
Show resolved Hide resolved
return Object.keys(webhooks);
} else {
return [""];
}
}
}

Scratch.extensions.register(new Webhooks());
})(Scratch);
2 changes: 1 addition & 1 deletion extensions/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@
"vercte/dictionaries",
"godslayerakp/http",
"godslayerakp/ws",
"CubesterYT/Webhooks",
"Lily/CommentBlocks",
"veggiecan/LongmanDictionary",
"CubesterYT/TurboHook",
"Alestore/nfcwarp",
"steamworks",
"itchio",
Expand Down
88 changes: 88 additions & 0 deletions images/CubesterYT/Webhooks.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading