regex to replace javascript content using powershell script - javascript

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";

Related

Compressing Base64 string through LZString gives SyntaxErrors

https://pieroxy.net/blog/pages/lz-string/guide.html
I have some Base64 data url strings which need to be compressed into shorter strings. I have downloaded the LZString library and tried to use Base64String.compress(base64) and Base64String.decompress(compressedBase64) as demonstrated in above official guide.
While the data urls are used for displaying images in offline page, the page is expected to be in compressed size, so each string has to be decompressed within the offline page. Here is a simplified code sample:
var compressed = Base64String.compress(dataUrl);
var script = document.createElement("script");
script.innerHTML = "var dataUrl = Base64String.decompress(" + compressed + ");";
offlineHtml.querySelector("body").appendChild(script);
However, when I download the page and open it, it shows various forms of SyntaxErrors occurred, including missing ) after argument list Invalid or unexpected token Unexpected identifiers. It seems that the string is compressed in a way that creates problematic syntax. Is there any solution, or any other suitable library for base64 string compression?
Instead of trying to concatenate to create a string containing valid Javascript syntax, save the compressed string somewhere else from which static Javascript can retrieve and parse it. You could use <script type="application/json">, or an invisible textarea, or a data attribute:
const compressed = Base64String.compress(dataUrl);
const script = document.createElement("script");
script.dataset.compressed = compressed;
script.textContent = `
var dataUrl = Base64String.decompress(document.currentScript.dataset.compressed);
`;
offlineHtml.querySelector("body").appendChild(script);
(or, preferably, rather than writing the Javascript inline like that, attach a src to the script tag instead, which contains the same content)

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 handle the pound sign ('#') in filenames?

I have a problem when it comes to filenames in Node.js where some incoming files have the pound sign # which causes problems when working with the file system and then using a URL spawned from the filename to reaccess it in an <img> tag.
var fileDir = "/foo/bar#s.png/";
var dirContents = fs.readdirSync(fileDir);
document.getElementById("id1").src = encodeURIComponent(path.join('cache', fileDir, dirContents[0]))
// %2Fcache%2Ffoo%2Fbar%23s.png%2Fbar%23s%2001.png :: Broken img
document.getElementById("id2").src = path.join('cache', fileDir, dirContents[1])
// /cache/foo/bar#s.png/bar#s 02.png :: Also broken img
I have control over the directory names, but the final file .pngs are up to the user who loaded the file. How do I go about handling these characters?
You would escape these characters:
document.getElementById("id1").src = path.join('cache',
...fileDir.split("/").map(encodeURIComponent),
encodeURIComponent(dirContents[0]));
However, you will need to make sure that your server properly decodes them again when receiving the image request, not all implementations agree on what to do with percent encoding in file paths. An easier an possibly better approach is to prevent these characters from occurring in file names in the first place.

eval javascript text from html body

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
});

Get / Read Text File from URL - Javascript for After Effects

I'm used to selecting, opening and reading a text file (CSV, JSON) from my local directory. I now have access to our data feeds online but don't know how to do the same thing with a URL instead of a local file.
For a local file I simply use something like this:
var myFile = File.openDialog();
myFile.open();
myFile.read();
Is there a way to do this with a feed from a URL in javascript for AE?
Here is one of the feeds: feeds.nfl.com/feeds-rs/schedules.json
No need to save file, just one line do the trick =)
data = JSON.parse(system.callSystem('curl -s "https://path_to_json"'));
I think in After Effects the easiest way is to do a system call.
The Socket object from ExtendScript is complicated. Look at GetURLs.jsx by Rorohiko.
You need to allow your script to access the network. See this stackoverflow
# Mac Osx
var curlcmd = "curl feeds.nfl.com/feeds-rs/schedules.json > ~/Desktop/test/schedules.json";
var stdout = system.callSystem(curlcmd);
$.writeln(stdout);
var file = File("~/Desktop/test/schedules.json");
file.open();
var content = file.read();
file.close();
$.writeln(content);

Categories

Resources