![NPM version](https://img.shields.io/npm/v/express-forward-html.svg?style=flat)
express-forward-html
Use express server to forward html, so that you can get the same origin page. Request it in an iframe, and then dom manipulation of the page is allowed in common broswer without proxy or plugin.
Installation
npm install express-forward-html -S
Usage
use it in express app. example:
var express = require('express');
var app = express();
var forward = require('express-forward-html');
forward({
prefix: '/forward'
})(app);
var port = 8888;
app.listen(port, () =>{
console.log('the mock server is listen on: ' + port);
});
then, you can visit other url by : http://127.0.0.1:8888/forward/html?url=https://github.com
, Note that if the target url includes query params, it must be encoded by encodeURIComponent
, like: http://127.0.0.1:8888/forward/html?url=https%3A%2F%2Fgithub.com
Configuration
options
interface Options {
prefix: string;
filterHtml: (html: string, req: Request) => string;
filterCookie: (cookie: string, req: Request) => string;
filterStatic: (content: string, req: Request) => string;
filterAjax: (body: any, req: Request) => string;
script: string | ((urlObj: CustomURL) => void);
isMobileUA: (url: string) => boolean | string;
needRedirect: (url: string, req: Request) => boolean;
}
interface CustomURL extends URL {
mobile: boolean;
serverUrlObj: URL;
}
An example:
{
prefix: '',
filterHtml: function(html, req) {
html = html.replace(/<script[\S]+?<\/script>/g, '');
return html;
},
filterCookie: function(cookie, req) {
cookie = cookie.replace(/id.*?=.+?;/gi, '');
return cookie;
},
filterStatic: function(content, req) {
content = content.replace(/top\.location/g, '{}');
return content;
},
script: function(urlObj) {
window.$pageUrl = urlObj.href;
}
isMobileUA: (url) => /[/.](m|wap)\./.test(url),
needRedirect: function(url, req) {
let mobile = /[/.](m|wap)\./.test(url);
let isSpecifedH5 = req.query.m === 'H5';
return (!isSpecifedH5 && mobile) || (!mobile && isSpecifedH5);
}
}
broswer request formation
http://${localServerAddress}:${localServerPort}/${prefixInOptions}/html?url=${encodedTargetUrl}
- localServerAddress: express server address, eg:
localhost
- localServerPort: express server port listened, eg:
8080
- prefixInOptions: prefix can be set in options, if not, there will add router(/html, /ajax, /static) in root path
- encodedTargetUrl: the target url you want visit.
TODO