Comments (9)
Hi @liuxujim
It isn't clear what you are trying to achieve
It looks like your are trying to hand off the initial request to the proxy to a backend server and have encryption all the way?
from harmon.
Thanks No9. The problem above has been resolved but I have a new question. How can I append(instead of replacing) a javascript reference(for example: <script type=text/javascript src="sample.js"/>) to the "head" element of the response using Harmon. Does the following look good? Any better solution?
var simpleselect2 = {};
simpleselect2.query = 'head';
simpleselect2.func = function (node) {
var inData = "<script>alert();</script>";
var readStream = node.createReadStream();
readStream.on('data', function(chunk) {
inData += chunk;
node.createWriteStream({ outer: false }).end(inData)
});
}
selects.push(simpleselect2);
from harmon.
This is the error I got
c:\nodejs\node_modules\harmon\node_modules\trumpet\lib\tokenize.js:76
tokenize.queue([ 'tag-begin', Buffer(m[0]), arg ]);
^
TypeError: Cannot read property '0' of null
at makeFn (c:\nodejs\node_modules\harmon\node_modules\trumpet\lib\tokenize.j
s:76:51)
at SAXStream.parser.(anonymous function) (c:\nodejs\node_modules\harmon\node
_modules\trumpet\lib\tokenize.js:26:50)
at SAXStream.EventEmitter.emit (events.js:95:17)
at Object.me.parser.(anonymous function) [as onopentag](c:nodejsnode_mod
ulesharmonnode_modulestrumpetnode_modulessaxlibsax.js:245:15)
at emit (c:\nodejs\node_modules\harmon\node_modules\trumpet\node_modules\sax
\lib\sax.js:615:33)
at emitNode (c:\nodejs\node_modules\harmon\node_modules\trumpet\node_modules
\sax\lib\sax.js:620:3)
at openTag (c:\nodejs\node_modules\harmon\node_modules\trumpet\node_modules
sax\lib\sax.js:801:3)
at Object.write (c:\nodejs\node_modules\harmon\node_modules\trumpet\node_mod
ules\sax\lib\sax.js:1191:29)
at SAXStream.write (c:\nodejs\node_modules\harmon\node_modules\trumpet\node
modules\sax\lib\sax.js:227:16)
at Stream. (c:\nodejs\node_modules\harmon\node_modules\trumpet\li
b\tokenize.js:18:27)
from harmon.
Hi @liuxujim
You are heading in the right direction.
This is more of a trumpet implementation detail and here is a sample of something that you can start working with
index.js
var trumpet = require('trumpet');
var through = require('through');
var tr = trumpet();
tr.selectAll('.b span', function (node) {
var s = node.createStream();
s.pipe(through(function (data) {
s.write(data + " data");
}))
});
var fs = require('fs');
fs.createReadStream(__dirname + '\\test.html').pipe(tr).pipe(process.stdout);
test.html
<html>
<head>
<title>beep</title>
</head>
<body>
<div class="a">¡¡¡</div>
<div class="b">
<span>tacos</span>
<span> y </span>
<span>burritos</span>
</div>
<div class="a">!!!</div>
</body>
</html>
If you have time I would suggest going through the following tutorial on streams.
It has more details on through
which is required
in the sample above and is also a very handy utility function.
http://nodeschool.io/#stream-adventure
from harmon.
Hi Anton, thank you for the response.
The sample you pasted can implement replacing the content of an element, but what I am looking for is appending a new element behind a selected element.
For example, my previous HTML is
What I want to do is add a javascript behind the <title> element of the . So it should be updated like this:
<title>This is the title</title> \<script .....HERE IS SOME JAVASCRIPT CODE.........\>
........
I tried the sample code in comment 3, but failed. Can you please advise?
from harmon.
Hi
So here is something closer to what you are looking for.
It puts the script tag in before the closing head tag.
The stream seems to be firing per parsed token within the node so we need to
filter it out with an if statement.
The functionality works but my feeling is that when outer:true
is used the
data
argument should contain the whole node not an event for each token.
index.js
var trumpet = require('trumpet');
var through = require('through');
var tr = trumpet();
tr.selectAll('head', function (node) {
var s = node.createStream({outer : true });
s.pipe(through(function (data) {
//If test is a work around for the event firing
//for each token in the selected node.
//Uncomment the console.dir to reproduce
//console.dir('fired');
//Output is
//fired <head fired>fired <title fired>fired beep fired<title fired fired
if(data.toString().indexOf('</head>') > -1) {
s.write('<script>alert("hello");</script>');
}
s.write(data);
}))
});
var fs = require('fs');
fs.createReadStream(__dirname + '\\test.html').pipe(tr).pipe(process.stdout);
test.html
<html>
<head>
<title>beep</title>
</head>
<body>
<div class="a">¡¡¡</div>
<div class="b">
<span>tacos</span>
<span> y </span>
<span>burritos</span>
</div>
<div class="a">!!!</div>
</body>
</html>
from harmon.
Hi Anton, thank you very much for your immediate response. I think your comment should work. But my problem seems a little complicated than that.
What I am implementing
- Create a reveres proxy to proxy a remote server(https://:greenhouse.lotus.com/profiles, for example)
- Modify the response to inject a script like <script ....></script>
The problem is the response of this remote server is in gzip format, so it looks like if we need to modify the response, we have to (1) read response (2) decompress response (3) modify response (4) re-compress.(5) write response.
My problem is I can finish step 1,2,3,4,but don't know how to implement step5 after step 4. See my sample code below. Please check line 21(forgive me the format, don't know how to make it look better), Not sure if you can help? Or do you have other better solution?
------------------------------
var http = require('http'),
https = require('https'),
connect = require('connect'),
httpProxy = require('http-proxy');
fs = require('fs'),
path = require('path'),
zlib = require('zlib'),
fixturesDir = path.join(__dirname);
var app = connect();
app.use(function(req, res, next) {
var _write = res.write;
res.write = function (data) {
var zlib = require('zlib');
zlib.unzip(data, function(err, bufferNew) {
if (!err) {
var newContent = bufferNew.toString().replace("\<head\>", "\<head\>\\n\<script\>alert('test')\</script\>");
var buf = new Buffer(newContent, 'utf-8');
zlib.gzip(buf, function (_, result) {
/***********************************************************
Need to write back the response, how????
res.end(result);
_write.call(res, result);
***********************************************************/
});
}
});
//_write.call(res, data);
}
next();
});
var httpsOpts = {
//TODO this need to be changed accordingly
key: fs.readFileSync(path.join(fixturesDir, 'localhost-key.pem'), 'utf8'),
cert: fs.readFileSync(path.join(__dirname, 'localhost-cer.pem'), 'utf8')
};
var proxySSL = httpProxy.createProxyServer({
xfwd: true,
ssl: httpsOpts,
target: 'https://greenhouse.lotus.com',
secure: false
}
);
app.use(function(req, res) {
proxySSL.web(req, res);
});
http.createServer(app).listen(80);
https.createServer(httpsOpts, app).listen(443);
-------------------------------------------------------------------
from harmon.
Sorry but this is getting off topic for harmon.
You problem is now around connect library usage and I would suggest going to their project as they could give you clearer/better guidance on that.
https://github.com/senchalabs/connect
from harmon.
@liuxujim Gzip support and a sample has been added to the repo
https://github.com/No9/harmon/blob/master/examples/gzipped.js
from harmon.
Related Issues (20)
- how to use harmon on specify url HOT 1
- prepareRequestSelectors HOT 1
- Dealing with bad html HOT 9
- How to use in non-html? HOT 2
- Connection hangs for very generic selectors HOT 6
- doge.js is redirecting to original site with no injection HOT 7
- Request hangs
- gzip content length issue HOT 2
- Proxy a m3u8 file HOT 1
- Variable Targets
- Use with browsersync HOT 2
- js/css filter with query params HOT 2
- Doesn't work nowadays? Examples and tests fail HOT 9
- Replacing the value inside an element HOT 3
- write() getting called before writeHead() on gzip'd content causes failures HOT 2
- help w/ hello world, they don't require harmon? HOT 1
- got error on zlib HOT 1
- No selector callbacks being invoked for certain sites HOT 2
- How to modify a file from a response
- Content Length not deleted on HTML rewrite if HTML Only HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from harmon.