From 6ae3a43da45dcd2c6ca1addc408765bbccd7b46d Mon Sep 17 00:00:00 2001 From: 1982FenceHopper Date: Fri, 3 Jan 2025 19:35:42 -0500 Subject: [PATCH 1/5] SNIPPET_ADD: JavaScript/Audio Manipulation/Web Audio API Starter --- .../web-audio-api-starter.md | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 snippets/javascript/audio-manipulation/web-audio-api-starter.md diff --git a/snippets/javascript/audio-manipulation/web-audio-api-starter.md b/snippets/javascript/audio-manipulation/web-audio-api-starter.md new file mode 100644 index 00000000..5db60f18 --- /dev/null +++ b/snippets/javascript/audio-manipulation/web-audio-api-starter.md @@ -0,0 +1,45 @@ +--- +title: Web Audio API Starter Example +description: Simple example for playing a tone with the Web Audio API +author: 1982FenceHopper +tags: audio,web +--- + +```js +/* + * "It's recommended to create one AudioContext and reuse it instead of initializing a new one each time, + * and it's OK to use a single AudioContext for several different audio sources and pipeline concurrently." + * + * https://developer.mozilla.org/en-US/docs/Web/API/AudioContext + */ +class AudioSingleton { + static ctx; + + static getInstance() { + if (!ctx) { + ctx = new AudioContext(); + } + return ctx; + } +} + +// Usage: +function startAudio() { + let ctx = AudioSingleton.getInstance(); + + oscNode = ctx.createOscillator(); + gainNode = ctx.createGain(); + + gainNode.gain.value = 0.01; // It can get really loud, so this helps :) + + oscNode.type = "square"; + oscNode.frequency.setValueAtTime(440, ctx.currentTime); + + oscNode.connect(gainNode); + gainNode.connect(ctx.destination); + + oscNode.start(); +} + +startAudio(); +``` \ No newline at end of file From 0e680630e0ef38eb13efaede835568345d72eac8 Mon Sep 17 00:00:00 2001 From: 1982FenceHopper Date: Fri, 3 Jan 2025 19:40:00 -0500 Subject: [PATCH 2/5] SNIPPET_CHANGE: Fixed comment in JavaScript/Audio Manipulation/Web Audio API Starter --- .../javascript/audio-manipulation/web-audio-api-starter.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/snippets/javascript/audio-manipulation/web-audio-api-starter.md b/snippets/javascript/audio-manipulation/web-audio-api-starter.md index 5db60f18..2eff7bb4 100644 --- a/snippets/javascript/audio-manipulation/web-audio-api-starter.md +++ b/snippets/javascript/audio-manipulation/web-audio-api-starter.md @@ -2,7 +2,7 @@ title: Web Audio API Starter Example description: Simple example for playing a tone with the Web Audio API author: 1982FenceHopper -tags: audio,web +tags: audio, web --- ```js @@ -41,5 +41,5 @@ function startAudio() { oscNode.start(); } -startAudio(); +startAudio(); // Output: A tone should be audible. ``` \ No newline at end of file From eafc574d1e9892d617d4b34616eb2e8f0425c8f0 Mon Sep 17 00:00:00 2001 From: 1982FenceHopper Date: Fri, 3 Jan 2025 19:47:45 -0500 Subject: [PATCH 3/5] SNIPPET_CHANGE: Fixed title in JavaScript/Audio Manipulation/Web Audio API Starter --- snippets/javascript/audio-manipulation/web-audio-api-starter.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/javascript/audio-manipulation/web-audio-api-starter.md b/snippets/javascript/audio-manipulation/web-audio-api-starter.md index 2eff7bb4..a6ef489a 100644 --- a/snippets/javascript/audio-manipulation/web-audio-api-starter.md +++ b/snippets/javascript/audio-manipulation/web-audio-api-starter.md @@ -1,5 +1,5 @@ --- -title: Web Audio API Starter Example +title: Web Audio API Starter description: Simple example for playing a tone with the Web Audio API author: 1982FenceHopper tags: audio, web From 8a09e7ed3b23e4961750d3008ef79cc774f97458 Mon Sep 17 00:00:00 2001 From: 1982FenceHopper Date: Sat, 4 Jan 2025 16:16:19 -0500 Subject: [PATCH 4/5] SNIPPET_ADD: JavaScript/Cookies/Set Cookie, JavaScript/Cookies/Get Cookie, JavaScript/Cookies/Delete Cookie --- public/consolidated/c.json | 2 +- public/consolidated/javascript.json | 54 ++++++++++++++++++++ snippets/javascript/cookies/delete-cookie.md | 21 ++++++++ snippets/javascript/cookies/get-cookie.md | 17 ++++++ snippets/javascript/cookies/set-cookie.md | 22 ++++++++ 5 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 snippets/javascript/cookies/delete-cookie.md create mode 100644 snippets/javascript/cookies/get-cookie.md create mode 100644 snippets/javascript/cookies/set-cookie.md diff --git a/public/consolidated/c.json b/public/consolidated/c.json index 1588cd69..a9258611 100644 --- a/public/consolidated/c.json +++ b/public/consolidated/c.json @@ -38,7 +38,7 @@ "numbers" ], "contributors": [], - "code": "#include\nvoid swap(int* num1,int* num2){\n *num1 = *num1 + *num2;\n *num2 = *num1 - *num2;\n *num1 = *num1 - *num2;\n}\n\n// Usage:\nint a = 3,b = 4;\nswap(&a,&b); // simply use printf after this to print swapped values\n" + "code": "#include\nvoid swap(int* num1,int* num2){\n *num1 = *num1 + *num2;\n *num2 = *num1 - *num2;\n *num1 = *num1 - *num2;\n}\n\n// Usage:\nint a = 3,b = 4;\nswap(&a,&b); // swaps the values of the a and b variables\n" } ] } diff --git a/public/consolidated/javascript.json b/public/consolidated/javascript.json index 2e8c8f97..d56e80ed 100644 --- a/public/consolidated/javascript.json +++ b/public/consolidated/javascript.json @@ -61,6 +61,22 @@ } ] }, + { + "categoryName": "Audio Manipulation", + "snippets": [ + { + "title": "Web Audio API Starter", + "description": "Simple example for playing a tone with the Web Audio API", + "author": "1982FenceHopper", + "tags": [ + "audio", + "web" + ], + "contributors": [], + "code": "/*\n * \"It's recommended to create one AudioContext and reuse it instead of initializing a new one each time,\n * and it's OK to use a single AudioContext for several different audio sources and pipeline concurrently.\"\n *\n * https://developer.mozilla.org/en-US/docs/Web/API/AudioContext\n */\nclass AudioSingleton {\n static ctx;\n\n static getInstance() {\n if (!ctx) {\n ctx = new AudioContext();\n }\n return ctx;\n }\n}\n\n// Usage:\nfunction startAudio() {\n let ctx = AudioSingleton.getInstance();\n\n oscNode = ctx.createOscillator();\n gainNode = ctx.createGain();\n\n gainNode.gain.value = 0.01; // It can get really loud, so this helps :)\n\n oscNode.type = \"square\";\n oscNode.frequency.setValueAtTime(440, ctx.currentTime);\n\n oscNode.connect(gainNode);\n gainNode.connect(ctx.destination);\n\n oscNode.start();\n}\n\nstartAudio(); // Output: A tone should be audible.\n" + } + ] + }, { "categoryName": "Basics", "snippets": [ @@ -93,6 +109,44 @@ } ] }, + { + "categoryName": "Cookies", + "snippets": [ + { + "title": "Delete Cookie", + "description": "Delete a browser cookie", + "author": "1982FenceHopper", + "tags": [ + "cookie", + "document" + ], + "contributors": [], + "code": "const deleteCookie = (name) => {\n try {\n document.cookie = `${encodeURIComponent(name)}=MARKED;max-age=0;path=/`;\n } catch (e) {\n console.error(e);\n }\n};\n\n// Usage:\nlet name = \"foo\";\n\ndeleteCookie(name);\nconsole.log(document.cookie) // Expected: Cookie foo should not be a part of the output.\n" + }, + { + "title": "Get Cookie", + "description": "Get a browser cookie", + "author": "1982FenceHopper", + "tags": [ + "cookie", + "document" + ], + "contributors": [], + "code": "const getCookie = (name) => {\n return document.cookie.split(\";\").find((cookie) => cookie.trim().split(\"=\")[0] == name)\n};\n\n// Usage:\nlet name = \"foo\";\n\nconsole.log(getCookie(name)); // Output: foo=bar\n" + }, + { + "title": "Set Cookie", + "description": "Set a browser cookie", + "author": "1982FenceHopper", + "tags": [ + "cookie", + "document" + ], + "contributors": [], + "code": "const setCookie = (name, value, max_age = 3600, expires = undefined) => {\n\ttry {\n \tdocument.cookie = `${encodeURIComponent(name)}=${encodeURIComponent(value)};max-age=${max_age};path=/;${expires ? `expires=${encodeURIComponent(expires)}` : \"\"}`;\n } catch (e) {\n console.error(e);\n }\n};\n\n// Usage:\nlet name = \"foo\";\nlet value = \"bar\";\n\nsetCookie(name, value);\nconsole.log(document.cookie) // Expected: foo=bar should be a part of the output.\n" + } + ] + }, { "categoryName": "Date And Time", "snippets": [ diff --git a/snippets/javascript/cookies/delete-cookie.md b/snippets/javascript/cookies/delete-cookie.md new file mode 100644 index 00000000..125db025 --- /dev/null +++ b/snippets/javascript/cookies/delete-cookie.md @@ -0,0 +1,21 @@ +--- +title: Delete Cookie +description: Delete a browser cookie +author: 1982FenceHopper +tags: cookie, document +--- + +```js +const deleteCookie = (name) => { + try { + document.cookie = `${encodeURIComponent(name)}=MARKED;max-age=0;path=/`; + } catch (e) { + console.error(e); + } +}; + +// Usage: +let name = "foo"; + +deleteCookie(name); +``` \ No newline at end of file diff --git a/snippets/javascript/cookies/get-cookie.md b/snippets/javascript/cookies/get-cookie.md new file mode 100644 index 00000000..31601708 --- /dev/null +++ b/snippets/javascript/cookies/get-cookie.md @@ -0,0 +1,17 @@ +--- +title: Get Cookie +description: Get a browser cookie +author: 1982FenceHopper +tags: cookie, document +--- + +```js +const getCookie = (name) => { + return document.cookie.split(";").find((cookie) => cookie.trim().split("=")[0] == name) +}; + +// Usage: +let name = "foo"; + +console.log(getCookie(name)); // Output: foo=bar +``` \ No newline at end of file diff --git a/snippets/javascript/cookies/set-cookie.md b/snippets/javascript/cookies/set-cookie.md new file mode 100644 index 00000000..d9bc7a1d --- /dev/null +++ b/snippets/javascript/cookies/set-cookie.md @@ -0,0 +1,22 @@ +--- +title: Set Cookie +description: Set a browser cookie +author: 1982FenceHopper +tags: cookie, document +--- + +```js +const setCookie = (name, value, max_age = 3600, expires = undefined) => { + try { + document.cookie = `${encodeURIComponent(name)}=${encodeURIComponent(value)};max-age=${max_age};path=/;${expires ? `expires=${encodeURIComponent(expires)}` : ""}`; + } catch (e) { + console.error(e); + } +}; + +// Usage: +let name = "foo"; +let value = "bar"; + +setCookie(name, value); +``` \ No newline at end of file From 2a6ba8891f6058adb07cce28920845e2617d39eb Mon Sep 17 00:00:00 2001 From: 1982FenceHopper Date: Sat, 4 Jan 2025 16:19:10 -0500 Subject: [PATCH 5/5] SNIPPET_REMOVE: JavaScript/Audio manipulation/Web Audio API Starter --- .../web-audio-api-starter.md | 45 ------------------- 1 file changed, 45 deletions(-) delete mode 100644 snippets/javascript/audio-manipulation/web-audio-api-starter.md diff --git a/snippets/javascript/audio-manipulation/web-audio-api-starter.md b/snippets/javascript/audio-manipulation/web-audio-api-starter.md deleted file mode 100644 index a6ef489a..00000000 --- a/snippets/javascript/audio-manipulation/web-audio-api-starter.md +++ /dev/null @@ -1,45 +0,0 @@ ---- -title: Web Audio API Starter -description: Simple example for playing a tone with the Web Audio API -author: 1982FenceHopper -tags: audio, web ---- - -```js -/* - * "It's recommended to create one AudioContext and reuse it instead of initializing a new one each time, - * and it's OK to use a single AudioContext for several different audio sources and pipeline concurrently." - * - * https://developer.mozilla.org/en-US/docs/Web/API/AudioContext - */ -class AudioSingleton { - static ctx; - - static getInstance() { - if (!ctx) { - ctx = new AudioContext(); - } - return ctx; - } -} - -// Usage: -function startAudio() { - let ctx = AudioSingleton.getInstance(); - - oscNode = ctx.createOscillator(); - gainNode = ctx.createGain(); - - gainNode.gain.value = 0.01; // It can get really loud, so this helps :) - - oscNode.type = "square"; - oscNode.frequency.setValueAtTime(440, ctx.currentTime); - - oscNode.connect(gainNode); - gainNode.connect(ctx.destination); - - oscNode.start(); -} - -startAudio(); // Output: A tone should be audible. -``` \ No newline at end of file