-
-
Notifications
You must be signed in to change notification settings - Fork 247
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
Indexed db extension #1340
Indexed db extension #1340
Changes from 12 commits
940820f
0b3bd7a
72d4f1e
23d44ce
67e03c8
f5943a7
969ac55
18f89a2
3ab7aa7
a8b4a7d
6c89433
a1627df
75616b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,222 @@ | ||
// Name: Indexed Database | ||
// ID: ffghdfhIndexedDB | ||
// Description: Lets you use an IndexedDB database, allowing for you to store more than 5MB of data locally. Made by chair_senpai :) | ||
// License: LGPL-3.0 | ||
(function (Scratch) { | ||
"use strict"; | ||
if (!Scratch.extensions.unsandboxed) { | ||
throw new Error("Indexed Database must be run unsandboxed"); | ||
} | ||
|
||
class IndexedDBExtension { | ||
constructor() { | ||
this.dbName = "MyScratchDB"; // Default database name | ||
this.db = null; | ||
} | ||
|
||
// Set the database name | ||
_setDBName(name) { | ||
this.dbName = name; | ||
} | ||
|
||
// Initialize the IndexedDB | ||
_initDB() { | ||
return new Promise((resolve, reject) => { | ||
const request = indexedDB.open(this.dbName, 1); | ||
|
||
request.onerror = (event) => { | ||
console.error("Database error:", event.target.error); | ||
resolve(); | ||
}; | ||
|
||
request.onsuccess = (event) => { | ||
this.db = event.target.result; | ||
resolve(this.db); | ||
}; | ||
|
||
request.onupgradeneeded = (event) => { | ||
const db = event.target.result; | ||
db.createObjectStore("data", { keyPath: "key" }); | ||
}; | ||
}); | ||
} | ||
|
||
// Set value in the IndexedDB | ||
_setValue(key, value) { | ||
return this._initDB().then((db) => { | ||
const transaction = db.transaction(["data"], "readwrite"); | ||
const store = transaction.objectStore("data"); | ||
const request = store.put({ key, value }); | ||
|
||
return new Promise((resolve, reject) => { | ||
request.onsuccess = () => resolve(); | ||
request.onerror = () => resolve(); | ||
}); | ||
}); | ||
} | ||
|
||
// Get value from the IndexedDB | ||
_getValue(key) { | ||
return this._initDB().then((db) => { | ||
const transaction = db.transaction(["data"], "readonly"); | ||
const store = transaction.objectStore("data"); | ||
const request = store.get(key); | ||
|
||
return new Promise((resolve, reject) => { | ||
request.onsuccess = () => | ||
resolve(request.result ? request.result.value : null); | ||
request.onerror = () => resolve("undefined"); | ||
}); | ||
}); | ||
} | ||
|
||
// Delete all keys in the IndexedDB | ||
_deleteAllKeys() { | ||
return this._initDB().then((db) => { | ||
const transaction = db.transaction(["data"], "readwrite"); | ||
const store = transaction.objectStore("data"); | ||
const request = store.clear(); | ||
|
||
return new Promise((resolve, reject) => { | ||
request.onsuccess = () => resolve(); | ||
request.onerror = () => resolve(); | ||
}); | ||
}); | ||
} | ||
|
||
// Delete a specific key in the IndexedDB | ||
_deleteKey(key) { | ||
return this._initDB().then((db) => { | ||
const transaction = db.transaction(["data"], "readwrite"); | ||
const store = transaction.objectStore("data"); | ||
const request = store.delete(key); | ||
|
||
return new Promise((resolve, reject) => { | ||
request.onsuccess = () => resolve(); | ||
request.onerror = () => resolve(); | ||
}); | ||
}); | ||
} | ||
|
||
// Check if a key exists in the IndexedDB | ||
_keyExists(key) { | ||
return this._initDB().then((db) => { | ||
const transaction = db.transaction(["data"], "readonly"); | ||
const store = transaction.objectStore("data"); | ||
const request = store.get(key); | ||
|
||
return new Promise((resolve) => { | ||
request.onsuccess = () => resolve(request.result !== undefined); | ||
}); | ||
}); | ||
} | ||
|
||
// Scratch blocks implementation | ||
getInfo() { | ||
return { | ||
id: "ffghdfhIndexedDB", | ||
name: "IndexedDB", | ||
color1: "#fd7f54", // Main block color | ||
color2: "#c26140", | ||
color3: "#b35b3d", // Slightly darker outline color | ||
blocks: [ | ||
{ | ||
opcode: "setDBName", | ||
blockType: Scratch.BlockType.COMMAND, | ||
text: "set database name [NAME]", | ||
arguments: { | ||
NAME: { | ||
type: Scratch.ArgumentType.STRING, | ||
defaultValue: "MyScratchDB", | ||
}, | ||
}, | ||
}, | ||
{ | ||
opcode: "setValue", | ||
blockType: Scratch.BlockType.COMMAND, | ||
text: "set key [KEY] to [VALUE]", | ||
arguments: { | ||
KEY: { | ||
type: Scratch.ArgumentType.STRING, | ||
defaultValue: "myKey", | ||
}, | ||
VALUE: { | ||
type: Scratch.ArgumentType.STRING, | ||
defaultValue: "myValue", | ||
}, | ||
}, | ||
}, | ||
{ | ||
opcode: "getValue", | ||
blockType: Scratch.BlockType.REPORTER, | ||
text: "get value for key [KEY]", | ||
arguments: { | ||
KEY: { | ||
type: Scratch.ArgumentType.STRING, | ||
defaultValue: "myKey", | ||
}, | ||
}, | ||
}, | ||
{ | ||
opcode: "deleteAllKeys", | ||
blockType: Scratch.BlockType.COMMAND, | ||
text: "delete all keys", | ||
}, | ||
{ | ||
opcode: "deleteKey", | ||
blockType: Scratch.BlockType.COMMAND, | ||
text: "delete key [KEY]", | ||
arguments: { | ||
KEY: { | ||
type: Scratch.ArgumentType.STRING, | ||
defaultValue: "myKey", | ||
}, | ||
}, | ||
}, | ||
{ | ||
opcode: "keyExists", | ||
blockType: Scratch.BlockType.BOOLEAN, | ||
text: "key [KEY] exists?", | ||
arguments: { | ||
KEY: { | ||
type: Scratch.ArgumentType.STRING, | ||
defaultValue: "myKey", | ||
}, | ||
}, | ||
}, | ||
], | ||
}; | ||
} | ||
|
||
setDBName(args) { | ||
const { NAME } = args; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could do this in the function argument as opposed to setting it retroactively, if you wanted to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. uhh i'm sorry, but i'm not quite sure what you mean by that 😅 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh i got it, one sec imma modify it |
||
this._setDBName(NAME); | ||
} | ||
|
||
setValue(args) { | ||
const { KEY, VALUE } = args; | ||
return this._setValue(KEY, VALUE); | ||
} | ||
|
||
getValue(args) { | ||
const { KEY } = args; | ||
return this._getValue(KEY); | ||
} | ||
|
||
deleteAllKeys() { | ||
return this._deleteAllKeys(); | ||
} | ||
|
||
deleteKey(args) { | ||
const { KEY } = args; | ||
return this._deleteKey(KEY); | ||
} | ||
|
||
keyExists(args) { | ||
const { KEY } = args; | ||
return this._keyExists(KEY); | ||
} | ||
} | ||
|
||
Scratch.extensions.register(new IndexedDBExtension()); | ||
})(Scratch); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Credit yourself here! You made the extension! :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done! i added my discord username!