-
Notifications
You must be signed in to change notification settings - Fork 235
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
IndexedDB extension (omg this is like my 3rd try lol) #1341
Open
ffghdfh
wants to merge
24
commits into
TurboWarp:master
Choose a base branch
from
ffghdfh:IndexedDB-Extension
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
940820f
Create indexedDB.js
ffghdfh 0b3bd7a
Update indexedDB.js license
ffghdfh 72d4f1e
fixed dumb issues
ffghdfh 23d44ce
Create indexedDB.svg
ffghdfh 67e03c8
Update extensions.json
ffghdfh f5943a7
made indexedDB.svg's background non transparent
ffghdfh 969ac55
oops, lowercase i in extensions.json
ffghdfh 18f89a2
Update README.md, added indexedDB.svg attribution
ffghdfh 3ab7aa7
Update README.md, forgot to add my name and a /, i am so sorry
ffghdfh a8b4a7d
Update indexedDB.js, removed all the reject()-s
ffghdfh 6c89433
made indexedDB.js prettier :)
ffghdfh a1627df
Update indexedDB.js with discord username
ffghdfh 75616b8
Update indexedDB.js oops forgot punctuation
ffghdfh 9454702
Update indexedDB.js, even MORE fixes
ffghdfh c0cad27
Update indexedDB.js, added // By: and set undefined key resolve to ju…
ffghdfh fe89cb8
oh my god i missed the part about scratch profile links only. changed…
ffghdfh 9a13d72
Merge branch 'TurboWarp:master' into IndexedDB-Extension
ffghdfh 596c35e
Merge branch 'TurboWarp:master' into IndexedDB-Extension
ffghdfh ca36a42
Merge branch 'TurboWarp:master' into IndexedDB-Extension
ffghdfh fc5ef27
add new features and improve the extension
ffghdfh 9b1b2f0
Removed monitor checkbox
ffghdfh c6dd8d9
Made get key return nothing
ffghdfh 54eadf7
Merge branch 'TurboWarp:master' into IndexedDB-Extension
ffghdfh 8c0d73f
Merge branch 'TurboWarp:master' into IndexedDB-Extension
ffghdfh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,316 @@ | ||
(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) => { | ||
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) => { | ||
request.onsuccess = () => { | ||
const value = request.result ? request.result.value : undefined; | ||
resolve(value !== undefined ? value : ""); | ||
}; | ||
|
||
request.onerror = () => resolve(""); | ||
}); | ||
}); | ||
} | ||
|
||
// 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) => { | ||
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) => { | ||
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); | ||
}); | ||
}); | ||
} | ||
|
||
// Export the entire database | ||
_exportDB() { | ||
return this._initDB().then((db) => { | ||
const transaction = db.transaction(["data"], "readonly"); | ||
const store = transaction.objectStore("data"); | ||
const request = store.getAll(); | ||
|
||
return new Promise((resolve) => { | ||
request.onsuccess = () => { | ||
const result = {}; | ||
request.result.forEach((item) => { | ||
result[item.key] = item.value; | ||
}); | ||
resolve(JSON.stringify(result)); | ||
}; | ||
|
||
request.onerror = () => { | ||
resolve("{}"); | ||
}; | ||
|
||
request.onerror = () => { | ||
resolve("{}"); | ||
}; | ||
}); | ||
}); | ||
} | ||
|
||
// Import data into the database, replacing existing data | ||
_importDB(data) { | ||
return this._deleteAllKeys().then(() => { | ||
const parsedData = JSON.parse(data); | ||
const promises = Object.keys(parsedData).map(key => { | ||
return this._setValue(key, parsedData[key]); | ||
}); | ||
return Promise.all(promises); | ||
}); | ||
} | ||
|
||
// Merge data into the database, without replacing existing keys | ||
_mergeDB(data) { | ||
const parsedData = JSON.parse(data); | ||
const promises = Object.keys(parsedData).map(key => { | ||
return this._keyExists(key).then(exists => { | ||
if (!exists) { | ||
return this._setValue(key, parsedData[key]); | ||
} | ||
}); | ||
}); | ||
return Promise.all(promises); | ||
} | ||
|
||
// 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: "", | ||
}, | ||
}, | ||
}, | ||
{ | ||
opcode: "setValue", | ||
blockType: Scratch.BlockType.COMMAND, | ||
text: "set key [KEY] to [VALUE]", | ||
arguments: { | ||
KEY: { | ||
type: Scratch.ArgumentType.STRING, | ||
defaultValue: | ||
"", | ||
}, | ||
VALUE: { | ||
type: Scratch.ArgumentType.STRING, | ||
defaultValue: "", | ||
}, | ||
}, | ||
}, | ||
{ | ||
opcode: "getValue", | ||
blockType: Scratch.BlockType.REPORTER, | ||
text: "get key [KEY]", | ||
arguments: { | ||
KEY: { | ||
type: Scratch.ArgumentType.STRING, | ||
defaultValue: "", | ||
}, | ||
}, | ||
}, | ||
{ | ||
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: "", | ||
}, | ||
}, | ||
}, | ||
{ | ||
opcode: "keyExists", | ||
blockType: Scratch.BlockType.BOOLEAN, | ||
text: "key [KEY] exists?", | ||
arguments: { | ||
KEY: { | ||
type: Scratch.ArgumentType.STRING, | ||
defaultValue: "", | ||
}, | ||
}, | ||
}, | ||
{ | ||
opcode: "exportDB", | ||
blockType: Scratch.BlockType.REPORTER, | ||
text: "export db", | ||
disableMonitor: true | ||
}, | ||
{ | ||
opcode: "importDB", | ||
blockType: Scratch.BlockType.COMMAND, | ||
text: "import db from [DATA]", | ||
arguments: { | ||
DATA: { | ||
type: Scratch.ArgumentType.STRING, | ||
defaultValue: "", | ||
}, | ||
}, | ||
}, | ||
{ | ||
opcode: "mergeDB", | ||
blockType: Scratch.BlockType.COMMAND, | ||
text: "merge db from [DATA]", | ||
arguments: { | ||
DATA: { | ||
type: Scratch.ArgumentType.STRING, | ||
defaultValue: "", | ||
}, | ||
}, | ||
}, | ||
], | ||
}; | ||
} | ||
|
||
setDBName(args) { | ||
const { NAME } = args; | ||
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); | ||
} | ||
|
||
exportDB() { | ||
return this._exportDB(); | ||
} | ||
|
||
importDB(args) { | ||
const { DATA } = args; | ||
return this._importDB(DATA); | ||
} | ||
|
||
mergeDB(args) { | ||
const { DATA } = args; | ||
return this._mergeDB(DATA); | ||
} | ||
} | ||
|
||
Scratch.extensions.register(new IndexedDBExtension()); | ||
})(Scratch); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Returning "undefined" as a string is not within typical conventions for Scratch/TurboWarp. Just return an empty string if it fails to get a key.
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.
Okie dokie, changed that too!