41 lines
1.6 KiB
JavaScript
41 lines
1.6 KiB
JavaScript
(function(root, factory) {
|
|
const api = factory();
|
|
if (typeof module === 'object' && module.exports) module.exports = api;
|
|
else root.sharedImageCapture = api;
|
|
})(typeof self !== 'undefined' ? self : this, function() {
|
|
'use strict';
|
|
|
|
const RECORD_ID = 'shared-image';
|
|
const TYPES = new Set(['image/png', 'image/jpeg', 'image/webp']);
|
|
|
|
async function consume({marker, store, restore, status = () => {}}) {
|
|
if (!marker) return false;
|
|
if (!['image', 'images'].includes(marker)) {
|
|
status('Share one PNG, JPEG, or WebP screenshot.');
|
|
return false;
|
|
}
|
|
const value = await store?.get(RECORD_ID);
|
|
const attachments = Array.isArray(value?.attachments) ? value.attachments : null;
|
|
if (marker === 'images') {
|
|
if (!attachments?.length || attachments.length > 5 || attachments.some(attachment =>
|
|
!attachment?.blob || !attachment.filename || !TYPES.has(String(attachment.contentType || '')))) {
|
|
status('The shared screenshots are unavailable. Share them again.');
|
|
return false;
|
|
}
|
|
restore(attachments);
|
|
await store.delete(RECORD_ID);
|
|
status(attachments.length + ' shared screenshots ready to file with this issue.');
|
|
return true;
|
|
}
|
|
if (!value?.blob || !value.filename || !TYPES.has(String(value.contentType || ''))) {
|
|
status('The shared screenshot is unavailable. Share it again.');
|
|
return false;
|
|
}
|
|
restore(value);
|
|
await store.delete(RECORD_ID);
|
|
status('Shared screenshot ready to file with this issue.');
|
|
return true;
|
|
}
|
|
|
|
return {consume, RECORD_ID};
|
|
}); |