eval javascript text from html body - javascript

I have a bash script using curl that download a page, then use grep and sed to extract javascript inside the html block to a file, so after it I use node to evaluate and use javascript downloaded.
is something like:
curl 'http://...' ... | grep -E "(varxpto\(|fnxpto)" | sed 's|<[/]\?script[^>]*>||g' > fn.js
x="$(node -pe "var fs = require('fs'); eval( fs.readFileSync('fn.js')+'' );
var val=fnxpto('${PW}'); val;")"
it works like a charm using bash. but I need to expose it as a service, so I trying to do it in nodejs.
My problem is... how to do it? I tried xpath but seems it needs xmldoc as prereq and xmldoc do not parse my html (it think it's exclusive for xml, not html).
Not what I want, but I trying to exec the grep/sed too as workarround for my problem.
NOTE: I have the html text recovered using require('http') I don't need help here. Only on extract javascript from the html and import/evaluate it.
Anyone has any idea of how can I extract javascript text script from a html and evaluate it in node?

You could use something like cheerio to parse the HTML and then query the document for script tags:
// `data` is the entire string response from `http.request()`
var cheerio = require('cheerio'),
$ = cheerio.load(data);
$('script').each(function(i, elem) {
console.dir($(this).text());
// do eval() or whatever else here
});

Related

regex to replace javascript content using powershell script

I am using powershell script to replace some content of the text which is in a javascript file
var i = {
production: !0,
myBaseURL: "https://www.sample.com/api/",
}
(Get-Content -literalPath $pathToJsFile -raw) -replace "https://www.sample.com",
"https://www.newserver.com"|
Out-File $jspath -encoding UTF8
I want to replace this using regex as it can be anything instead of "https://www.sample.com"
I tried
$file -replace "myBaseURL\s*: .*", "myBaseURL: $([char]34)https://www.newserver.com([char]34),"
is there a cleaner way to do this ?
This is generally a bad idea. Instead of replacing, consider generating a JS file with the attributes, and including it earlier than your code in the DOM.
Alternatively use your domain name to decide which environment you are in.
var env;
switch(window.location.hostname){
case "example.com":
env = "prod";
case "dev.example.com":
env = "dev";

How to wrap node express output so that it can be included as javascript?

Trying to modify node.js express to wrap the standard output in javascript (utilising document.write) so that I can include it into other HTML files. I want to encode the output at the express level and decode it at the client.
Edit: I want to create a standard node app that generates HTML output but wrap it just before sending with the javascript. This is so I can continue to utilise express-handlebars and other add ons without disruption to the output.
If this is already a thing in node please point me to documentation on how to achieve it? Otherwise I'm looking for where to change the code in express to wrap each output.
For clarity; to answer please include a reference the correct express file(s) and start with existing code to produce a modified version that achieves my goal.
The Express version I am looking at is 4.15.4. I've viewed the code and found the index.js and route.js files (https://github.com/expressjs/express/tree/master/lib/router) but don't understand enough to know where to modify the code.
Example
My initial goal would be for my node app to return encoded HTML wrapped in code similar to the following:
var str = "<encoded content goes here>";
document.write(decodeURI(str));
Example way to call my node from HTML:
<script src='http://localhost:3000/'></script>
Feel free to comment on the approach if you have an opinion.
Thanks!
I think I worked it out.
In file https://github.com/expressjs/express/blob/master/lib/response.js # line 116 I added the following code:
chunk = "str = \""+encodeURI(body)+"\";\ndocument.write(decodeURI(str));\n";
Seems to have worked.
So:
res.send = function send(body) {
var chunk = body;
var encoding;
var len;
var req = this.req;
var type;
// settings
var app = this.app;
<added my code here>
// allow status / body

Create a text file using JavaScript without using web interface

I'm looking for a method to create + write a text file using JavaScript but I only found method with browser. I don't need my browser to do that, I want to implement a method in my code that write data in a text file nothing to do with the browser.
So someone know how to do this ?
Use Node.Js:
Node.js Write a line into a .txt file
Something like:
var fs = require('fs')
var logger = fs.createWriteStream('log.txt', {
flags: 'a' // 'a' means appending (old data will be preserved)
})
logger.write('some data') // append string to your file
logger.write('more data') // again
logger.write('and more') // again
https://nodejs.org/api/fs.html

How to beautify/prettify a Json/JS file in a node.js script

I am searching a way to prettify Json files in a node.js script (not CLI). I found a lot of npm beautifier packages, but none that can simply beautify directly a file.
There is esbeautifier that do what I am searching to do, but the exemples only shows CLI commands: https://github.com/royriojas/esbeautifier Is there a way to use it in a Javascript?
You can prettyprint JSON easily by providing parameters to JSON.stringify().
Many people use this kind of call to prettyprint JSON output. It's still valid JSON, it just contains indentation and newlines.
JSON.stringify(myObject, null, 2);
you can use the tool esformatter.
edit by #jck: here is JS snippet that works using fs:
var esformatter = require('esformatter');
var fs = require('fs');
var filename = "./myFile.json";
var codeStr = fs.readFileSync(filename).toString();
var formattedCode = esformatter.format(codeStr);
fs.writeFile(filename, formattedCode);
Alternatively, check out prettyjson! It has been great for me!

How to replace the content of multiple tags in html-file? [Node.js]

We have html-file:
<div class="my1">MY1</div>
<h1 class="my2">MY2</h1>
<p class="my3">MY3</p>
<div class="my2">MY2</div>
and json-file with replace values:
{
"my1":"new my1",
"my2":"new my2",
"my3":"new my3"
}
In result must be new html-file with new values.
How to do this? Without DOM!
I don't understand why you are against DOM building as it is relativity easy to do. There are lightweight modules such as cheerio that fulfill your requirements and have a simple API (similar to JQuery).
Fully working example
This example does exactly what you want using the cheerio node module.
Files
replace.js
var fs = require('fs')
, cheerio = require('cheerio');
// html file we are working on
var htmlPath = __dirname + '/htmlFile.html';
// html file to output modified html to
var outPath = __dirname + '/htmlFixed.html';
// load in the json file with the replace values
var replaceValues = require('./replaceValues.json');
fs.readFile(htmlPath, {encoding: 'utf8'}, function(error, data) {
var $ = cheerio.load(data); // load in the HTML into cheerio
for (var key in replaceValues) {
// the keys are class names, use them to pick out what element
// we are going to modify & then replace the innerHTML content
// of that element
$('.' + key).html(replaceValues[key]);
}
fs.writeFile(outPath, $.html());
});
htmlFile.html
<div class="my1">MY1</div>
<h1 class="my2">MY2</h1>
<p class="my3">MY3</p>
<div class="my2">MY2</div>
replaceValues.json
{
"my1":"new my1",
"my2":"new my2",
"my3":"new my3"
}
Usage
node replace.js
Results
After running the script, you will have a new file in the same directory as the original HTML file named htmlFixed.html with the changes performed with the contents:
<div class="my1">new my1</div>
<h1 class="my2">new my2</h1>
<p class="my3">new my3</p>
<div class="my2">new my2</div>
Node.js does not have any notion of a Document Object Model (i.e. the DOM) -- "Why doesn't node.js have a native DOM?" -- and as such does not have access to a way to instantiate HTML Elements (which would be nice to have access to in order to parse your html).
As a result, you must import a library to use as a parser (just like any other server side implementation must when dealing with html - don't regex it!).
The current leader in doing this is jsDOM

Categories

Resources