Closed matteomontipo closed 2 years ago
Can you please post the full error/stack trace and node version?
Node version is: v14.18.0 (also tried with latest version 16, same error)
It looks like you're using https-browserify
. Request options are not supported by https-browserify
so it breaks any request made by this package.
Related issue: https://github.com/substack/https-browserify/issues/9
You're not the first one to report this issue but unfortunately it looks like that package is not being updated. Your best solution would be to serve the search results through an API of your own.
@matteomontipo If you want to give 2.3.0
this should work better with https-browserify and other similar packages. I'll reopen the issue to give you some time to try it if you want, otherwise I'll close again after a week.
@matteomontipo If you want to give
2.3.0
this should work better with https-browserify and other similar packages. I'll reopen the issue to give you some time to try it if you want, otherwise I'll close again after a week.
Awesome thanks, tried with 2.3.1 and that error is gone still not sure what's loading the https-browserify mentioned (maybe webpack 5 which is included in next.js 12 ?)
Although I now receive a Failed fetch, but I bet is more about some parameters I'm missing in the options than a bug, should I post it here in the same thread? It throws error:
Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('<URL>') does not match the recipient window's origin ('<URL>').
And
Access to fetch at 'https://www.youtube.com/results?search_query=test&sp=EgIQAQ%253D%253D' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
This also happen when I tested it on vercel, not only on localhost
I tried to modify the options as:
const options = {
requestOptions: {
headers: {
Cookie: 'PREF=f2=8000000',
'Access-Control-Allow-Origin': 'localhost:3000'
},
host: 'http://localhost:3000'
}
};
But it doesn't change, I think it happens because the url _https://www.youtube.com/results?search_query=test&sp=EgIQAQ%253D%253D_ is missing the parameter "&origin=http://localhost:3000" but I'm not sure
There's not much I can do about a CORS error, that's controlled by YouTube's servers.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
You're welcome to try using the origin parameter but as far as I know that's only used in the API.
Another user mentioned they had some success with a reverse proxy like cors-anywhere (#6) but these methods are hacky and out of the scope of this package.
Again, my best recommendation would be a simple API to serve the results to your app.
const { youtube } = require('scrape-youtube');
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors({ origin: 'http://example.com' }));
// host:3000/search?query=Poets of the fall
app.get('/search', async (req, res) => {
res.json(await youtube.search(req.query.query));
});
app.listen(3000);
thanks using Next.js so I went for next-cors, and fetch, doing the api query returns the json correctly!
Glad to hear it. Let me know if you encounter any other issues and I'll be happy to help.
Getting error TypeError: The "listener" argument must be of type Function. Received type object
Any idea why?
Code is in react with a contentEditable div checking for keyboard esacpe command this way:
The react return is as follow: