Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions extensions/DevidBittFive/ramusage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Name: RAM Monitor
// ID: bitterRamUsage
// Description: Monitors current RAM usage and percentage.
// By: DevidBittFive <https://scratch.mit.edu/users/Bitter_160/>
// License: EPL-2.0 OR GPL-2.0-or-later

/**!
* @license EPL-2.0 OR GPL-2.0-or-later
* All code contained in this extension is licensed under the Eclipse Public License v2.0.
*
* If you have not received a copy of this license, you may access it
* at the following link: https://www.eclipse.org/legal/epl-2.0/
*/

(function (Scratch) {
"use strict";

// Cache once outside the class
const mem =
globalThis.performance && globalThis.performance.memory

Check warning on line 20 in extensions/DevidBittFive/ramusage.js

View workflow job for this annotation

GitHub Actions / type-warnings

Type warning - may indicate a bug - ignore if no bug

Property 'memory' does not exist on type 'Performance'.
? globalThis.performance.memory

Check warning on line 21 in extensions/DevidBittFive/ramusage.js

View workflow job for this annotation

GitHub Actions / type-warnings

Type warning - may indicate a bug - ignore if no bug

Property 'memory' does not exist on type 'Performance'.
: null;

class RamUsageExtension {
getInfo() {
return {
id: "bitterRamUsage",
name: Scratch.translate("RAM Monitor"),
blocks: [
{
blockType: Scratch.BlockType.XML,
xml: mem
? ""
: `
<sep gap="-12" />
<label text="${Scratch.translate("Your browser does not")}" /><sep gap="-12" />
<label text="${Scratch.translate("support the &quot;performance.memory&quot;")}" /><sep gap="-12" />
<label text="${Scratch.translate("API")}" />
`,
},
{
opcode: "getRamUsageMB",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate("get RAM usage in MB"),
},
{
opcode: "getRamUsagePercent",
blockType: Scratch.BlockType.REPORTER,
text: Scratch.translate("get RAM usage percent"),
},
],
};
}

getRamUsageMB() {
// Check the cached mem variable directly
if (!mem) return NaN;

return mem.usedJSHeapSize / 1024 / 1024;
}

getRamUsagePercent() {
// Simplified check
if (!mem) return NaN;

// Calculate percentage as a raw number
return (mem.usedJSHeapSize / mem.jsHeapSizeLimit) * 100;
}
}

Scratch.extensions.register(new RamUsageExtension());
})(Scratch);
1 change: 1 addition & 0 deletions extensions/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"shreder95ua/resolution",
"XmerOriginals/closecontrol",
"navigator",
"DevidBittFive/ramusage",
"battery",
"PwLDev/vibration",
"TheShovel/CustomStyles",
Expand Down