如何使用Node.js修改Google Chrome中的HTTPS流量?
使用Node.js修改网页返回内容的实现方案
核心思路
通过Node.js发起请求获取目标页面的HTML内容,对字符串进行拼接修改后输出结果。以下是两种实用实现方式:
方式一:用Node.js原生HTTP模块
无需额外安装依赖,直接用内置模块处理:
const https = require('https'); const targetUrl = 'https://example.com/inj.php'; https.get(targetUrl, (res) => { let rawHtml = ''; res.on('data', (chunk) => { rawHtml += chunk; }); res.on('end', () => { // 去除原内容末尾空格后拼接新标签 const modifiedHtml = rawHtml.trim() + '\n<h2>inject</h2>'; console.log(modifiedHtml); }); }).on('error', (err) => { console.error('请求出错:', err.message); });
方式二:用axios库简化请求
先安装依赖:
npm install axios
实现代码:
const axios = require('axios'); async function modifyContent() { try { const { data: rawHtml } = await axios.get('https://example.com/inj.php'); const modifiedHtml = rawHtml.trim() + '\n<h2>inject</h2>'; console.log(modifiedHtml); } catch (error) { console.error('请求失败:', error.message); } } modifyContent();
说明
- 两种方案都是基于字符串拼接实现内容修改,针对当前静态HTML场景完全适用
trim()用于清理原内容末尾的多余空格,保证输出格式整洁
内容的提问来源于stack exchange,提问作者IIU
相关产品推荐
相关产品推荐

