-
Notifications
You must be signed in to change notification settings - Fork 364
add extension: ShareSheet #2269
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
Open
CreeperNewGamer
wants to merge
15
commits into
TurboWarp:master
Choose a base branch
from
CreeperNewGamer:CreeperNewGamer-Add-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.
+258
−0
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e0e0185
Create ShareSheet.js
CreeperNewGamer 294e12f
Add files via upload
CreeperNewGamer 21ff49a
Rename images/Untitled design.png to images/GamerCreeper12/ShareSheet…
CreeperNewGamer 7d25bd4
Update sharesheet.js
PPPDUD c3822bf
[Automated] Format code
DangoCat 40806a3
Fix lint, add translate and some casting
Brackets-Coder 5678928
Oops
Brackets-Coder 80cf8fb
Add GamerCreeper12/ShareSheet extension to extensions.json
Brackets-Coder 81037d1
Update ShareSheet.js to fix bug
PPPDUD fd75851
Delete images/GamerCreeper12/ShareSheet.png
CreeperNewGamer 3ac2dfc
Added ShareSheet.svg for better thumbnail and format
CreeperNewGamer 3e06b64
Fix error message for unsandboxed requirement, and added icons
CreeperNewGamer 8171270
[Automated] Format code
DangoCat c62d67a
Apply suggestions from code review
CreeperNewGamer 1b4c111
Fix Registration in ShareSheet
CreeperNewGamer 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 hidden or 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,256 @@ | ||
| // Name: ShareSheet | ||
| // ID: gamerCreeperShare | ||
| // Description: Share data from your browser. | ||
| // By: GamerCreeper12 <https://scratch.mit.edu/users/GamerCreeper12/> | ||
| // License: MPL-2.0 | ||
|
|
||
| (function (Scratch) { | ||
| "use strict"; | ||
|
|
||
| if (!Scratch.extensions.unsandboxed) { | ||
| throw new Error("ShareSheet must run unsandboxed"); | ||
| } | ||
|
|
||
| const Cast = Scratch.Cast; | ||
|
|
||
| class GamerCreeperShareExtension { | ||
| constructor() { | ||
| this.dataType = "text"; | ||
| this.title = ""; | ||
| this.data = null; | ||
| this.shareData = {}; | ||
| this.cancelled = false; | ||
| this.hasError = false; | ||
| this.errorMsg = ""; | ||
| } | ||
| getInfo() { | ||
| return { | ||
| id: "gamercreepernoobssharesheetextension", | ||
| name: Scratch.translate("ShareSheet"), | ||
| color1: "#742DEB", | ||
| color3: "#a176e8", | ||
| blockIconURI: | ||
| "data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KCjwhLS0gVXBsb2FkZWQgdG86IFNWRyBSZXBvLCB3d3cuc3ZncmVwby5jb20sIEdlbmVyYXRvcjogU1ZHIFJlcG8gTWl4ZXIgVG9vbHMgLS0+Cjxzdmcgd2lkdGg9IjgwMHB4IiBoZWlnaHQ9IjgwMHB4IiB2aWV3Qm94PSIwIDAgMjQgMjQiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CiAgPGcgaWQ9IkNvbW11bmljYXRpb24gLyBTaGFyZV9pT1NfRXhwb3J0Ij4KICAgIDxwYXRoIGlkPSJWZWN0b3IiIGQ9Ik05IDZMMTIgM00xMiAzTDE1IDZNMTIgM1YxM003LjAwMDIzIDEwQzYuMDY4MzUgMTAgNS42MDI0MSAxMCA1LjIzNDg2IDEwLjE1MjJDNC43NDQ4MSAxMC4zNTUyIDQuMzU1MjMgMTAuNzQ0OCA0LjE1MjI0IDExLjIzNDlDNCAxMS42MDI0IDQgMTIuMDY4MSA0IDEzVjE3LjhDNCAxOC45MjAxIDQgMTkuNDc5OCA0LjIxNzk5IDE5LjkwNzZDNC40MDk3MyAyMC4yODM5IDQuNzE1NDcgMjAuNTkwNSA1LjA5MTggMjAuNzgyMkM1LjUxOTIgMjEgNi4wNzg5OSAyMSA3LjE5NjkxIDIxSDE2LjgwMzZDMTcuOTIxNSAyMSAxOC40ODA1IDIxIDE4LjkwNzkgMjAuNzgyMkMxOS4yODQyIDIwLjU5MDUgMTkuNTkwNSAyMC4yODM5IDE5Ljc4MjIgMTkuOTA3NkMyMCAxOS40ODAyIDIwIDE4LjkyMSAyMCAxNy44MDMxVjEzQzIwIDEyLjA2ODEgMTkuOTk5OSAxMS42MDI0IDE5Ljg0NzcgMTEuMjM0OUMxOS42NDQ3IDEwLjc0NDggMTkuMjU1NCAxMC4zNTUyIDE4Ljc2NTQgMTAuMTUyMkMxOC4zOTc4IDEwIDE3LjkzMTkgMTAgMTcgMTAiIHN0cm9rZT0iI0ZGRkZGRiIgc3Ryb2tlLXdpZHRoPSIyIiBzdHJva2UtbGluZWNhcD0icm91bmQiIHN0cm9rZS1saW5lam9pbj0icm91bmQiIC8+CiAgPC9nPgo8L3N2Zz4=", | ||
| blocks: [ | ||
| { | ||
| opcode: "issupported", | ||
| blockType: Scratch.BlockType.BOOLEAN, | ||
| text: Scratch.translate("is share sheet supported?"), | ||
| }, | ||
| { | ||
| opcode: "settitleandtextto", | ||
| blockType: Scratch.BlockType.COMMAND, | ||
| text: Scratch.translate("set title [TITLE]"), | ||
| arguments: { | ||
| TITLE: { | ||
| type: Scratch.ArgumentType.STRING, | ||
| defaultValue: Scratch.translate("Check out this file!"), | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| opcode: "setdatatotxt", | ||
| blockType: Scratch.BlockType.COMMAND, | ||
| text: Scratch.translate("set data to text [TXT]"), | ||
| arguments: { | ||
| TXT: { | ||
| type: Scratch.ArgumentType.STRING, | ||
| defaultValue: Scratch.translate("Hello, World!"), | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| opcode: "setdatatourl", | ||
| blockType: Scratch.BlockType.COMMAND, | ||
| text: Scratch.translate("set data to url [URL]"), | ||
| arguments: { | ||
| URL: { | ||
| type: Scratch.ArgumentType.STRING, | ||
| defaultValue: Scratch.translate("https://example.com"), | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| opcode: "setdatatoimageurl", | ||
| blockType: Scratch.BlockType.COMMAND, | ||
| text: Scratch.translate("set data to image url [URL]"), | ||
| arguments: { | ||
| URL: { | ||
| type: Scratch.ArgumentType.STRING, | ||
| defaultValue: Scratch.translate("https://example.com/myimage/"), | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| opcode: "setdatatocostume", | ||
| blockType: Scratch.BlockType.COMMAND, | ||
| text: Scratch.translate("set data to costume [COSTUME]"), | ||
| arguments: { | ||
| COSTUME: { | ||
| type: Scratch.ArgumentType.COSTUME, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| opcode: "share", | ||
| blockType: Scratch.BlockType.COMMAND, | ||
| text: Scratch.translate("share data"), | ||
| }, | ||
| { | ||
| opcode: "isdatatosharesupported", | ||
| blockType: Scratch.BlockType.BOOLEAN, | ||
| text: Scratch.translate("is last data to share supported?"), | ||
| }, | ||
| { | ||
| opcode: "iscancelled", | ||
| blockType: Scratch.BlockType.BOOLEAN, | ||
| text: Scratch.translate("is sharing cancelled?"), | ||
| }, | ||
| { | ||
| opcode: "gotaerrortwhilesharing", | ||
| blockType: Scratch.BlockType.BOOLEAN, | ||
| text: Scratch.translate("got a error while sharing?"), | ||
| }, | ||
| { | ||
| opcode: "lasterrormsg", | ||
| blockType: Scratch.BlockType.REPORTER, | ||
| text: Scratch.translate("last error message"), | ||
| }, | ||
| ], | ||
| }; | ||
| } | ||
| // helpers | ||
| isShareSheetSupported() { | ||
| return !!(navigator.share && navigator.canShare); | ||
| } | ||
|
|
||
| isShareSheetDataSupported(shareData) { | ||
| if (!this.isShareSheetSupported()) return false; | ||
| return !!navigator.canShare(shareData); | ||
| } | ||
|
|
||
| async shareDataFunc(shareData) { | ||
| if (!this.isShareSheetDataSupported(shareData)) return; | ||
| await navigator.share(shareData); | ||
| } | ||
|
|
||
| async shareDataForShareSheet() { | ||
| if (!this.isShareSheetSupported()) return; | ||
| this.shareData = {}; | ||
| this.cancelled = false; | ||
| this.hasError = false; | ||
| try { | ||
| switch (this.dataType) { | ||
| case "text": | ||
| if (this.title) this.shareData["title"] = this.title; | ||
| if (this.data) this.shareData["text"] = this.data; | ||
| await this.shareDataFunc(this.shareData); | ||
| break; | ||
|
|
||
| case "url": | ||
| if (this.title) this.shareData["title"] = this.title; | ||
| if (this.data) this.shareData["url"] = this.data; | ||
| await this.shareDataFunc(this.shareData); | ||
| break; | ||
|
|
||
| case "image": | ||
| if (this.title) this.shareData["title"] = this.title; | ||
| if (this.data) this.shareData["files"] = [this.data]; | ||
| await this.shareDataFunc(this.shareData); | ||
| break; | ||
|
|
||
| default: | ||
| return; | ||
| } | ||
| } catch (err) { | ||
| if (err.name === "AbortError") { | ||
| this.cancelled = true; | ||
| } else { | ||
| this.hasError = true; | ||
| this.errorMsg = err; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // blocks | ||
| issupported() { | ||
| return Cast.toBoolean(this.isShareSheetSupported()); | ||
| } | ||
|
|
||
| settitleandtextto({ TITLE: title }) { | ||
| this.title = Cast.toString(title) || "Share"; | ||
| } | ||
|
|
||
| setdatatotxt({ TXT: text }) { | ||
| this.dataType = "text"; | ||
| this.data = Cast.toString(text); | ||
| } | ||
|
|
||
| setdatatourl({ URL: url }) { | ||
| this.dataType = "url"; | ||
| this.data = Cast.toString(url); | ||
| } | ||
|
|
||
| async setdatatoimageurl({ URL: url }) { | ||
| this.hasError = false; | ||
| try { | ||
| const response = await Scratch.fetch(url); | ||
| const blob = await response.blob(); | ||
|
|
||
| const imageFile = new File([blob], "image.png", { | ||
| type: blob.type, | ||
| }); | ||
|
|
||
| this.dataType = "image"; | ||
| this.data = imageFile; | ||
| } catch (err) { | ||
| this.hasError = true; | ||
| this.errorMsg = err.message; | ||
| } | ||
| } | ||
|
|
||
| async setdatatocostume({ COSTUME: costumeName }, util) { | ||
| const target = util.target; | ||
| const costumeIndex = target.getCostumeIndexByName(costumeName); | ||
| const costume = target.sprite.costumes[costumeIndex]; | ||
| const asset = costume.asset.encodeDataURI(); | ||
|
|
||
| this.hasError = false; | ||
| try { | ||
| const response = await Scratch.fetch(asset); | ||
| const blob = await response.blob(); | ||
|
|
||
| const imageFile = new File([blob], "image.png", { | ||
| type: blob.type, | ||
| }); | ||
|
|
||
| this.dataType = "image"; | ||
| this.data = imageFile; | ||
| } catch (err) { | ||
| this.hasError = true; | ||
| this.errorMsg = err.message; | ||
| } | ||
| } | ||
|
Comment on lines
+212
to
+233
Member
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. I was only able to get this block to work with bitmap costumes. Many costumes will be vector. Either specify "set data to bitmap costume" or, better, convert the costume to bitmap before setting the data. |
||
|
|
||
| async share() { | ||
| await this.shareDataForShareSheet(); | ||
| } | ||
|
|
||
| isdatatosharesupported() { | ||
| return this.isShareSheetDataSupported(this.shareData); | ||
| } | ||
|
|
||
| iscancelled() { | ||
| return this.cancelled; | ||
| } | ||
|
|
||
| gotaerrortwhilesharing() { | ||
| return this.hasError; | ||
| } | ||
|
|
||
| lasterrormsg() { | ||
| return this.errorMsg; | ||
| } | ||
| } | ||
| Scratch.extensions.register(new GamerCreeperShareExtension()); | ||
|
CreeperNewGamer marked this conversation as resolved.
|
||
| })(Scratch); | ||
This file contains hidden or 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.
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.
This assumes the image is a PNG?
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.
Maybe it should detect the type of the asset