How to access "document" in code.ts?

Document is undefined in code.ts. I can’t createElement(‘a’) for download some styles data I get from figma API.
How to access “document” in code.ts?
So user can download the json generate by the plugin?

function downloadObjectAsJson(exportObj, exportName){
var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(exportObj));
var downloadAnchorNode = document.createElement('a');
downloadAnchorNode.setAttribute("href", dataStr);
downloadAnchorNode.setAttribute("download", exportName + ".json");
document.body.appendChild(downloadAnchorNode); // required for firefox
downloadAnchorNode.click();
downloadAnchorNode.remove();
}

You can’t do that in code.ts, this needs to be done in the UI of the plugin.