plainheart / bing-translate-api

A simple and free API for Bing & Microsoft Translator for Node.js
https://github.com/plainheart/bing-translate-api
MIT License
161 stars 28 forks source link

Browser compatibility #6

Closed dhowe closed 11 months ago

dhowe commented 3 years ago

Thanks for the nice library - I'm wondering if there is a way to use it in the browser? I've tried browserify, but got seems not to be compatible. Any suggestions?

plainheart commented 3 years ago

Thanks but I'm afraid we cannot use it in the browser. It's by mocking browser request with Node, however, the browser doesn't allow us to add some unsafe headers for the security. We have to run it in a server.

dhowe commented 3 years ago

What is the header that is not allowed in the browser (just curious)?

plainheart commented 3 years ago

The referer header and user-agent header etc. We can't change it at will in the browser.

dhowe commented 3 years ago

Would not replacing got with something like ky-universal, support both node and the browser?

infinity0 commented 1 year ago

Actually, the referer doesn't matter, cookies don't matter, and the browser's own User Agent is fine. A few tweaks this can totally work inside a browser - but you do have to use a CORS anywhere proxy to get around the SOP. I have it working locally, I'll send a PR soon. You don't even need ky, plain browser fetch is sufficient.