Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] generate a sharable image/png when copying #170

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

li-xin-yi
Copy link
Collaborator

@li-xin-yi li-xin-yi commented Dec 24, 2022

I use this API of carbon: https://github.com/petersolopov/carbonara

Notice that localhost doesn't allow CROS, we can't send a request of application/json payload to localhost:3099/api/cook, the actual request body is in text/plain format. So you may need to skip the content type checking by deleting this line https://github.com/petersolopov/carbonara/blob/master/src/utils.js#L118, and then run the deployment command of this api by:

git clone https://github.com/petersolopov/carbonara
cd carbonara
docker build -t local/carbonara .
docker run -p 3099:3000 local/carbonara -it

To test if the api running normally, run a cURL command:

curl -L http://127.0.0.1:3099/api/cook \
-X POST \
-H 'Content-Type: application/json' \
-d '{
        "code": "sum(range(114))"
    }' \
> test.png

image

Now, the fetch request can be viewed with correct preview in devtools, I just wonder how to take it out.

image

@li-xin-yi li-xin-yi marked this pull request as draft December 24, 2022 22:53
@li-xin-yi
Copy link
Collaborator Author

The API even works well with Python requests:

image

I noticed that in no-CORS mode, the request type is opaque, so I can't get the response body (see this answer). I forced no-CORS mode because the hostname localhost in the request URL seems to disable CORS (see this question and answers due to the ngnix setting for our CodePod running on localhost. A proxy or any other may need to add to our ngnix server to enable CORS.

@lihebi Do you have any idea or suggestion with it?

};
const res = await fetch("http://localhost:3099/api/cook", requesOptions);
const pic = await res.blob();
// navigator.clipboard.write([new ClipboardItem({ "image/png": pic })]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this navigator.clipboard.write works. I can paste the image to Google Slides:

Screenshot 2022-12-26 at 6 19 08 PM

method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ code: pod.content }),
mode: "no-cors" as RequestMode,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CORS seems problematic. Let's add an Nginx route.

# nginx.conf
location /carbon {
        rewrite ^/carbon(.*)$ $1 break;
        proxy_pass http://carbon:3000;
}

and in compose.yml, add a container:

carbon:
    image: local/carbonara

The request URL will be http://localhost/carbon/api/cook

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants