Skip to content
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

Unified clipboard extension #667

Merged
merged 11 commits into from
Jul 9, 2023
Merged
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
118 changes: 118 additions & 0 deletions extensions/clipboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*!
* Copyright 2023 tomyo-code + AdamMady
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

(function(Scratch) {
'use strict';

if (!Scratch.extensions.unsandboxed) {
throw new Error('Clipboard must run unsandboxed');
}

const extensionicon = "data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHdpZHRoPSI4MC40NTQ1NCIgaGVpZ2h0PSI4MC40NTQ1NCIgdmlld0JveD0iMCwwLDgwLjQ1NDU0LDgwLjQ1NDU0Ij48ZyB0cmFuc2Zvcm09InRyYW5zbGF0ZSgtMTk5Ljc3MjcyLC0xMzkuNzcyNzIpIj48ZyBkYXRhLXBhcGVyLWRhdGE9InsmcXVvdDtpc1BhaW50aW5nTGF5ZXImcXVvdDs6dHJ1ZX0iIGZpbGwtcnVsZT0ibm9uemVybyIgc3Ryb2tlPSJub25lIiBzdHJva2UtbGluZ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YQXQ0MTZSUlRKMlRyNms0clBPcWxvcTNzR1hVTHlkUUdjamtwbmlad09IWFNrZHZCM3JaWkVjdklNdlJLQlFXRmY4dklNSGNCRHJlRVNodWtyWVR6a3VmNnRrT2N3VTcrQ1hON01sK0ZvV24zMU14Y3NXdTBXRTcrQ0xZTGVkSkx1dmtybWFkNmo0NHdzeE41K2RkUEFsNW1vQXlGZmhaUkZhS2NISi9IcXY3emJsTHF4bEgrL2dTeWcrdDJKbTNaK1o0bWNkVk83eU8vamNSSlgrSEx3U1ZHNHpCNSticU5LZmcxZUN5bTJXRTN6YW56M0d1Q1ozSWVmUlg4NDl5Ung4RDRYa0JKLzJhd2toSEVybGY5MmpISnVNS1lEVnJHeDZEeW1FMFBwVlQ4MmVaQTVlSVo4eHdFdFkyU3BLdm9HbktFNVZKbkdrMG9qZmJlaC9oU2FPUnZFT3ZwM2sxT0JGd1RMWnBXa3d0NW1ld0xwMHJRVGc0S2VIT0UzTzllRFRON2xEQ0JkTzQ4WHpxQW5JenEzcDYwRUxpbmZ3YW5hREREY0N2NUx1MGpQTEVNS3FRZTQ5ODRZRUJMaDhDVGw5UFdoQjhRNStYTEZNQlA4ZjVqR04yQ3N0cTkwQUFBQUFTVVZPUks1Q1lJST0iIGZpbGw9Im5vbmUiIHN0cm9rZS13aWR0aD0iMC41Ii8+PC9nPjwvZz48L3N2Zz4=";

let lastPastedText = '';

window.addEventListener('copy', (event) => {
Scratch.vm.runtime.startHats('clipboard_whenCopied') ;
});
window.addEventListener('paste', (event) => {
Scratch.vm.runtime.startHats('clipboard_whenPasted');
const clipboardData = event.clipboardData || window.clipboardData;
const pastedText = clipboardData.getData('Text');
lastPastedText = pastedText;
});

class Clipboard {
getInfo() {
return {
id: 'clipboard',
name: 'Clipboard',
blockIconURI: extensionicon,
color1: '#008080',
color2: '#006666',
blocks: [
{
opcode: 'whenCopied',
blockType: Scratch.BlockType.HAT,
text: 'when something is copied',
isEdgeActivated: false
},
{
opcode: 'whenPasted',
blockType: Scratch.BlockType.HAT,
text: 'when something is pasted',
isEdgeActivated: false
},
'---',
{
opcode: 'setClipboard',
blockType: Scratch.BlockType.COMMAND,
text: 'copy to clipboard: [TEXT]',
arguments: {
TEXT: {
type: Scratch.ArgumentType.STRING
}
}
},
{
opcode: 'resetClipboard',
blockType: Scratch.BlockType.COMMAND,
text: 'reset clipboard'
},
'---',
{
opcode: 'clipboard',
blockType: Scratch.BlockType.REPORTER,
text: 'clipboard',
disableMonitor: true
},
{
opcode: 'getLastPastedText',
blockType: Scratch.BlockType.REPORTER,
text: 'last pasted text',
disableMonitor: true
}
],
};
}

setClipboard(args) {
navigator.clipboard.writeText(args.TEXT);
}

resetClipboard() {
navigator.clipboard.writeText('');
}

clipboard() {
if (navigator.clipboard && navigator.clipboard.readText) {
return Scratch.canReadClipboard().then(allowed => {
if (allowed) {
return navigator.clipboard.readText();
}
return '';
});
}
return '';
}

getLastPastedText() {
return lastPastedText;
}
}

Scratch.extensions.register(new Clipboard());
})(Scratch);
3 changes: 3 additions & 0 deletions images/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,6 @@ All images in this folder are licensed under the [GNU General Public License ver

## Lily/LooksPlus.svg
- Created by [@LilyMakesThings](https://github.com/LilyMakesThings) in https://github.com/TurboWarp/extensions/pull/656

## clipboard.svg
- Created by [@AdamMady](https://github.com/AdamMady/)
1 change: 1 addition & 0 deletions images/clipboard.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions website/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,12 @@
<p>Clipping outside of a specified rectangular area and additive color blending. Created by <a href="https://scratch.mit.edu/users/Vadik1/">Vadik1</a>.</p>
</div>

<div class="extension">
<%- banner('clipboard') %>
<h3>Clipboard</h3>
<p>Read and write from the system clipboard.</p>
</div>

<div class="extension">
<%- banner('penplus') %>
<h3>Pen Plus</h3>
Expand Down