How to get variable that is storing current clipboard text ?

I am trying to write a transformer in JavaScript that takes a JSON, does some transformations and returns the manipulated JSON. I am not certain how to find the 2 variables

  1. text that is saved in clipboard currently
  2. or text that is selected by me with mouse.
    Is there some documentation or a method to find these?

Basically, I want to run the following function on clipboard text / selected text

function prettifyJSON(jsonString) {

    const object = JSON.parse(jsonString);
    const prettifiedJSON = JSON.stringify(object, null, 2);
    return prettifiedJSON;

}