是否可以使用Chrome扩展程序在浏览器上运行Node.js模块 [英] Is it possible to run Node.js modules on browser using Chrome extension

查看:524
本文介绍了是否可以使用Chrome扩展程序在浏览器上运行Node.js模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的大学项目,我正在创建一个Chrome扩展程序,以对用户在其浏览器上访问的每个URL进行某些检查和验证.我的目的是在使用Chrome扩展程序检测到可疑网站时提醒用户.因此,为此,我需要使用某些NPM模块,例如 whois-json get-ssl-certificate ,依此类推.因此,由于Node.js无法直接在浏览器上运行,因此我尝试使用 Browserify 模块来转换此Node.js代码例如require和模块执行到纯Javascript捆绑文件中.因此,当我尝试使用Chrome扩展程序运行此Javascript捆绑包时,出现以下错误:

For my college project, I'm creating a Chrome extension to perform certain checks and validations for each URL the user visits on his browser. My aim is to alert the user when a suspicious site is detected using Chrome extension. So for this purpose, I need to use certain NPM modules such as whois-json, get-ssl-certificate and so on. So, since Node.js cannot run on the browser directly, I tried using Browserify module to convert this Node.js code such as require and module execution into plain Javascript bundle file. So, when I try to run this Javascript bundle using the Chrome extension, I get an error as:

"net.connect"不是功能

"net.connect" is not a function

有没有可能解决此错误的方法?或者我可以使用其他解决方案替代Browserify在Chrome扩展程序上运行Node.js模块.

Is there any possible way to fix this error? Or can I use another solution as an alternative to Browserify to run my Node.js modules on the Chrome Extension.

推荐答案

否,您将无法使用在Chrome扩展程序中使用服务器特定代码的Node.js模块.不过,您这里有一些选择.

No, you won't be able to use Node.js modules that use server-specific code in a Chrome extension. You have a few options here though.

  1. 您可以查找使用浏览器API的替代模块,而改用这些模块.

  1. You could look for alternative modules that use browser APIs and use those instead.

您可以寻找实现您的Node.js模块正在执行的公共REST API.

You could look for public REST APIs that achieve what your Node.js modules are doing.

您可以使用现有的Node.js代码创建自己的REST API,然后通过客户端Chrome扩展程序与之交互.

You could create your own REST API with the existing Node.js code you have, and then interface with that from your client-side Chrome extension.

这篇关于是否可以使用Chrome扩展程序在浏览器上运行Node.js模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆