[英]Request.js not working properly with Browserify
I'm current building a js chrome extension, and to do so, I need to scrap data from some sites. 我目前正在构建js chrome扩展程序,为此,我需要从某些站点中抓取数据。
So, based in this SO question I found that I could achieve that using request with Browserify . 因此,基于这个SO问题,我发现我可以使用带有Browserify的 请求来实现这一点 。
I installed both using npm
and created a browserify.js
snippet to create my bundle.js
file( because for permissions reasons running terminal commands is not working ), so I can run Node js
require
's in the client, my browser. 我安装使用都
npm
并创建了一个browserify.js
片断创建我bundle.js
文件( 因为运行终端命令无法正常工作权限的原因 ),所以我可以运行Node js
require
的客户端,我的浏览器。
Ok, so I finally managed to create the bundle.js
file and tried to run it in my local server, but it keeps giving me the CORS error and don't return a desired response: 好的,所以我终于设法创建了
bundle.js
文件,并尝试在本地服务器上运行它,但是它一直给我CORS错误,并且没有返回所需的响应:
Fetch API cannot load https://somesite/index.html .
提取API无法加载https://somesite/index.html 。 No 'Access-Control-Allow-Origin' header is present on the requested resource.
所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin ' http://localhost:8080 ' is therefore not allowed access.
因此,不允许访问源' http:// localhost:8080 '。 If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
如果不透明的响应满足您的需求,请将请求的模式设置为“ no-cors”,以在禁用CORS的情况下获取资源。
One strange thing is that if I run the "unbundled" file direct from terminal using node
: 一件奇怪的事是,如果我使用
node
直接从终端运行“ unbundled”文件:
$ node myFileWithRequires.js
It works as intended, returned the scrapped data. 它按预期工作,返回了报废的数据。
What am I doing wrong? 我究竟做错了什么? How can I scrap data in client using
request
and browserify
? 如何使用
request
和browserify
在客户端中browserify
?
CODE: 码:
myBrowserifySnippet.js myBrowserifySnippet.js
var browserify = require('browserify');
var b = browserify();
b.add('myrequest.js');
const fs = require('fs');
const writable = fs.createWriteStream('bundle.js');
b.bundle().pipe(writable);
myFileWithRequires.js myFileWithRequires.js
var request = require('request');
request('http://www.google.com', function (error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body); // Print the HTML for the Google homepage.
});
By default, XHR and fetch
requests are bound by CORS, which means they cannot access resources on other domains unless those domains whitelist the 'origin' (the current page's domain). 默认情况下,XHR和
fetch
请求受CORS约束,这意味着它们无法访问其他域上的资源,除非这些域将“来源”(当前页面的域)列入白名单。 request
in the browser uses XHR, so it's also bound by CORS. 浏览器中的
request
使用XHR,因此它也受CORS约束。
In Chrome extensions, it's a bit different--you can configure your extension so that CORS doesn't apply to some domains. 在Chrome扩展程序中,这有点不同-您可以配置扩展程序,以使CORS不适用于某些域。 See Requiesting cross-origin permissions in the chrome extension documentation.
请参阅chrome扩展文档中的“ 要求跨域权限 ”。
You need to add a permissions
field to your extension manifest.json
: 您需要在扩展名
manifest.json
添加一个permissions
字段:
{
"permissions": [
"http://www.google.com/"
]
}
If you're not sure beforehand which domain you'll be scraping, you can use a wildcard: 如果您事先不确定要抓取哪个域,则可以使用通配符:
{
"permissions": [
"http://*/",
"https://*/"
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.