微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 http-proxy 和harmon 将脚本标签注入到 HTML 正文中

如何解决如何使用 http-proxy 和harmon 将脚本标签注入到 HTML 正文中

我正在使用 http-proxyharmon 构建网络代理,并尝试将脚本标记注入 HTML 正文,但问题是它始终无法正常工作。我猜它是为较小的站点注入脚本,而不是为大型站点注入脚本,我不知道为什么会发生这种情况

代码

const httpProxy = require("http-proxy");
const express = require('express');


const app = express();

const proxy = httpProxy.createProxyServer({
    secure: false,changeOrigin: true,});

proxy.on('proxyRes',function(proxyRes,req,res) {
    proxyRes.headers['content-security-policy'] = undefined;
    proxyRes.headers['x-frame-options'] = undefined;
});



app.use(require('harmon')([],[{
    query: 'body',func: function(node) {
        // Soomething happening here...
        const out = `<script src="http://localhost:5000/sample.js"></script>`;

        const rs = node.createReadStream();
        const ws = node.createWriteStream({
            outer: false
        });

        // Read the node and put it back into our write stream,// but don't end the write stream when the readStream is closed.
        rs.pipe(ws,{
            end: false
        });
        // When the read stream has ended,attach our style to the end
        rs.on('end',function(data) {
            ws.end(out);
        });

    }
}]));

app.use((req,res) => {
    proxy.web(req,res,{ target: 'http://example.com' });

});

app.listen(3000,() => {
    console.log('Listening....')
})

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。