Copy both the image and JSON object to the clipboard

I am attempting to utilize the clipboard API to write an image and JSON object to the window clipboard. I am working with Vue and Electron and have successfully written an image and plain text, but I encounter an error when trying to write a JSON object:

Uncaught (in promise) DOMException   localhost/:1

The code snippet I am using (this.object is a JSON object):

const textBlob = new Blob(['this.object.data','this.object.data2',...], {type: 'text/plain'});
const objBlob = new Blob([JSON.stringify(this.object)], {type: 'application/json'});

// defined a canvas with an image

canvas.toBlob(function(blob) {
  const item = new ClipboardItem({
    'image/png': blob,
    'text/plain': textBlob,
    'application/json': objBlob
  });
  navigator.permissions.query({name: 'clipboard-write'}).then((result) => {
    if (result.state === 'granted' || result.state === 'prompt') {
      navigator.clipboard.write([item]);
    }
  } 
}, 'image/png');

When including 'application/json': objBlob in the item, it generates an error. It seems that the clipboard does not support JSON objects. This behavior appears to be odd.

Despite this issue, I aim to copy an object to the clipboard alongside an image. Is there a workaround to address this obstacle? Should I consider using an alternative method instead of the clipboard API?

Thank you for your assistance!

Answer №1

Even though Chrome is making efforts to address this issue, currently they do not fully support writing "application/JSON"...

For now, it is recommended to stick with using "text/plain" (which should suffice for most applications).

const item = new ClipboardItem({
  'image/png': blob,
  'text/plain': objBlob
});

Answer №2

To better understand the error, I included a catch block: DOMException: Write type application/json not supported.

If you switch to plain/text or utf, you should be able to successfully convert the json object back into an object.

Using the clipboard for pasting objects into another app is ideal; however, if it's not necessary, local or session storage is a good alternative.

const objBlob = new Blob([JSON.stringify({'myobjLabel' : 'myObj'})], {type: 'text/plain'});

  const item = new ClipboardItem({
    'text/plain': objBlob
  });

  navigator.permissions.query({name: 'clipboard-write'}).then((result) => {
    if (result.state === 'granted' || result.state === 'prompt') {
      navigator.clipboard.write([item]).catch((ex) => { 
          console.log(ex) 
          } ); 
      }
    });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The error message "Property '$store' is not defined on type 'ComponentPublicInstance' when using Vuex 4 with TypeScript" indicates that the property '$store' is not recognized

I'm currently working on a project that involves using TypeScript and Vue with Vuex. I've encountered an error in VSCode that says: Property '$store' does not exist on type 'ComponentPublicInstance<{}, {}, {}, { errors(): any; } ...

My Gatsby website is being rendered in its HTML form on Netlify

The website build is located at . It appears that the javascript functionality is not working, and only the html version (usually meant for search engines) is being displayed. It seems like this issue is only affecting the home page. You can check out the ...

Converting a string representing time to a date object in Angular

Converting a string representation of time to a Date object var timeString = "09:56 AM"; this.audit_time = new Date(timeString); // Error: Invalid date I'm facing an issue with this conversion. Any suggestions on how to correct it? Please help me s ...

Sending emails to multiple recipients with different content using Nodemailer

I have been working on a method to send emails to multiple recipients while also passing the user attribute, which contains the name of the recipient, to the html template. (I AM UTILIZING NODEMAILER as a nodejs module) My current code looks like this: S ...

Sending data to mongodb using the fetch API and FormData method is a simple process that involves constructing

I have been trying to send data to mongoDB using the fetch API along with FormData, but encountering an error: POST https://xxxxxxxxxxxxxxxxxxxxxxxx/upload 500 (Internal Server Error) The form data in my EJS file appears as follows: <div id=" ...

Display the image regardless of whether the component is currently visible

I need help with my Vue.js web application that includes a side navigation menu component. This component uses conditional rendering to display only when necessary. Within the component, there is an image for the close button of the side menu. <transiti ...

Retrieving JSON data from R server

I am currently exploring ways to use R to send requests to a server. While I am familiar with packages that can help with converting data to and from JSON files, I am interested in requesting specific data from a server I have configured. The server would ...

Guide to retrieving information from an API and incorporating it into a fresh JSON structure

I am currently working on fetching data from an existing API endpoint and using a part of that data to create a new endpoint in Node.js with Express. Specifically, I am trying to retrieve the userId from https://jsonplaceholder.typicode.com/posts/1 and int ...

Using data across multiple instances in node.js EJS partials

I need some advice regarding my nodejs project. I have created a sidebar partial that requires user data to be displayed on it. This sidebar is utilized across multiple pages in the project. However, currently, I have to include the user data in every func ...

Conceal additional recipients in the "to" address field when sending emails with Node.js using Nodemailer

I am currently working on a project called EmailSender using node.js, and I have found that the nodemailer package is incredibly helpful. However, when sending emails to multiple contacts, all recipients are able to see each other's email addresses i ...

What is the best method to retrieve multiple values from a select dropdown and showcase them in an input field using ajax?

I am currently working on fetching multiple values from a database using AJAX and PHP. I have a select option which fetches values from the database, and when an option is selected, I want to display related data that matches the ID of the current option. ...

Learn the process of downloading a pdf file with vuejs and bootstrap-vue

Having trouble uploading a PDF file to my browser and then downloading it afterwards. The upload is fine, but the download isn't working. This is how I am uploading: <b-form-file v-model="form.file" :state="Boolean(form.file)" placeholder="Choose ...

How can I retrieve the PHP response once a successful upload has occurred using DropzoneJS?

I am currently in the process of integrating Dropzone into my website. My goal is to capture the "success" event and extract specific information from the server response to add to a form on the same page as the DropZone once the upload is finished. The k ...

How can I pass JSON data from a Java Swing application to a servlet? Should I use HTTP or sockets for this task?

Currently, I am using Eclipse Indigo and Tomcat 7.0 for my project. The project named "Controller" under the package "gui" is a Swing application aimed at creating a controller-tool for managing machine information settings. Within this application, there ...

The shared hosting environment encountered an error during the Next JS build process

When I execute the command "npm run build" on my shared hosting server, it throws an error message: spawn ENOMEM. Interestingly, this command runs perfectly fine on my localhost and has been running smoothly on the hosting server for a few weeks until yest ...

Sorting elements in an array using jQuery is a common task that can be easily accomplished

Query: I am faced with a challenge in handling a table where rows are dynamically added upon button clicks. To allow users to rearrange the rows, I have incorporated the jquery-ui sortable() function for sorting purposes. Consider the following scenario: ...

Tips for Utilizing CSS Overflow: Hidden to Prevent the Parent Container from Expanding

I have a fixed width column and a fluid column. In the fluid column, I want to display a string with nowrap so that it does not overflow the container. Here is an illustration of how I want the text to appear: --------------------------------------------- ...

The system encountered an issue with converting org.json.JSONObject to JSONArray

I am having an issue with a PHP file in my Android project. When I try to display the array data from my PHP code, I encounter an error stating "error parsing data org.json.JSONException ... error org.json.JSONObject cannot be converted to JSONArray." Bel ...

3.2+ Moodle communication and connections

I am facing a challenge with creating a new div @type@ in /message/templates/message_area_contact.mustache This is the code snippet: ... <div class="name"> {{fullname}} <div style="font-style:italic; font-weight:100;">{ ...

Is the behavior of undefined different in Chrome?

Upon examining my Asp masterpage, I noticed the following code snippet: <script> if (theForm !== undefined) { // <<== line 746: error theForm.onsubmit = ...bla bla... ; } </script> When checking the Chrome console, an er ...