Skip to content

Instantly share code, notes, and snippets.

View sabesansathananthan's full-sized avatar
:octocat:
Work

Sathananthan Sabesan sabesansathananthan

:octocat:
Work
View GitHub Profile
@sabesansathananthan
sabesansathananthan / Mixin.js
Created April 22, 2021 11:14
How React Components Are Reused
var SetIntervalMixin = {
componentWillMount: function() {
this.intervals = [];
},
setInterval: function() {
this.intervals.push(setInterval.apply(null, arguments));
},
componentWillUnmount: function() {
this.intervals.forEach(clearInterval);
}
@sabesansathananthan
sabesansathananthan / PasteEvent.js
Created March 5, 2021 08:27
Clipboard Operation in JavaScript
document.addEventListener('paste', async (e) => {
e.preventDefault();
const text = await navigator.clipboard.readText();
console.log('Pasted text: ', text);
});
@sabesansathananthan
sabesansathananthan / SpecifiedContentCopyEvent.js
Created March 5, 2021 08:17
Clipboard Operation in JavaScript
const clipboardItems = [];
document.addEventListener("copy", async (e) => {
e.preventDefault();
try {
let clipboardItems = [];
for (const item of e.clipboardData.items) {
if (!item.type.startsWith("image/")) {
continue;
}
@sabesansathananthan
sabesansathananthan / CopyEvent.js
Created March 5, 2021 07:33
Clipboard Operation in JavaScript
const source = document.querySelector('.source');
source.addEventListener('copy', (event) => {
const selection = document.getSelection();
event.clipboardData.setData('text/plain', selection.toString().toUpperCase());
event.preventDefault();
});
@sabesansathananthan
sabesansathananthan / Copy.js
Created March 5, 2021 07:24
Clipboard Operation in JavaScript
function copy() {
const image = await fetch('kitten.png');
const text = new Blob(['Cute sleeping kitten'], {type: 'text/plain'});
const item = new ClipboardItem({
'text/plain': text,
'image/png': image
});
await navigator.clipboard.write([item]);
@sabesansathananthan
sabesansathananthan / ClipboardWrite.js
Created March 5, 2021 06:30
Clipboard Operation in JavaScript
try {
const imgURL = 'https://dummyimage.com/300.png';
const data = await fetch(imgURL);
const blob = await data.blob();
await navigator.clipboard.write([
new ClipboardItem({
[blob.type]: blob
})
]);
console.log('Image copied.');
@sabesansathananthan
sabesansathananthan / ClipboardWriteTextTryCatch.js
Created March 5, 2021 06:25
Clipboard Operation in JavaScript
async function copyPageUrl() {
try {
await navigator.clipboard.writeText(location.href);
console.log('Page URL copied to clipboard');
} catch (err) {
console.error('Failed to copy: ', err);
}
}
@sabesansathananthan
sabesansathananthan / ClipboardWriteText.js
Created March 5, 2021 06:20
Clipboard Operation in JavaScript
document.body.addEventListener(
'click',
async (e) => {
await navigator.clipboard.writeText('Sabesan')
}
)
@sabesansathananthan
sabesansathananthan / ClipboardRead.js
Created March 5, 2021 06:04
Clipboard Operation in JavaScript
async function getClipboardContents() {
try {
const clipboardItems = await navigator.clipboard.read();
for (const clipboardItem of clipboardItems) {
for (const type of clipboardItem.types) {
const blob = await clipboardItem.getType(type);
console.log(URL.createObjectURL(blob));
}
}
} catch (err) {
@sabesansathananthan
sabesansathananthan / ClipboardReadTextTryCatch.js
Created March 5, 2021 05:59
Clipboard Operation in JavaScript
async function getClipboardContents() {
try {
const text = await navigator.clipboard.readText();
console.log('Pasted content: ', text);
} catch (err) {
console.error('Failed to read clipboard contents: ', err);
}
}