read local file in phantomjs - javascript

I'm attempting to write a script which is able to take a screenshot given an url using phantom.js. This works great when I supply the url(s). However, I would like to be able to read urls from a file in much the same way as in:
here and here. However, all methods return with
unable to open file 'filename.txt'
Am I missing something here?
Working from a win7 installation.
EDIT:
var fs = require('fs');
var file_h = fs.open('urls.txt', 'r');
var line = file_h.readLine();
while(line) {
console.log(line);
line = file_h.readLine();
}
file_h.close();
The output I get is:
unable to open file 'urls.txt'
phantomjs://platform/fs.js:79 in open

Related

How do I load antlr/index into ace worker js file?

I am following the instruction given here to integrate antlr4 with ace editor and I have trouble at the step var antlr4 = require('antlr4/index');. The author mentions that here we should use require for nodejs. However, ACE has another require that may cause problems. Thus he loaded another script for nodejs require and load antlr4/index with require in that script.
I tried that one, too. But it always cannot find the script. From the console of my browser, I can see the path it loads the script is:
localhost:4200/./antlr4/index.js and it fails to load it.
I am using Angular 7, and the structure of my project is as follows:
Also, when loading some local javascript file using importScripts, I always fails by giving the local path, however, giving URL from CDN will always work. But importScripts should support local file importing.
Where should I make changes and what else methods should I try?
Here are some of my code:
var ace_require = require;
window.require = undefined;
var Honey = { 'requirePath': ['..'] };
//importScript{"require.js"}
//the script can't be imported through importSctipt so I pasted the
//whole script file under...(not shown here)
var antlr4_require = window.require; // antlr4_require:antlr using nodejs require;
window.require = require = ace_require; // require:ace using its own require
var antlr4, LPMLNLexer, LPMLNParser;
try {
window.require = antlr4_require;
antlr4 = antlr4_require('antlr4/index');
//the browser stuck here reporting error...
} finally {
window.require = ace_require;
}

Creating Photoshop Plugin - trying to find simpler way to source file in javascript function

I'm creating a plugin I would like to be used for both Mac and Windows.
As the file trees are different, I would like to find a simpler way to source a file in a function contained in my /host/index.jsx file.
My file is located at /files/thisismyfile.psd
Currently I can only successfully source it by entering the full file tree from the main hard drive:
var fileRef = new File("/Library/Application Support/Adobe/CEP/extensions/com.my.panel/files/thisismyfile.psd");
I would much prefer to use something like:
var fileRef = new File("./files/thisismyfile.psd");
I've also tried testing having the file in each other folder and simply searching for:
var fileRef = new File("thisismyfile.psd");
With no luck! Any ideas?
Failing that, is it possible to code it so that it says:
"If this is mac, then search for the file here. If this is windows, then search for the file here."?
I ended up using this script to determine the location of the file depending on whether the system being used is mac or windows.
function isMacOS() {
return ($.os.toLowerCase().indexOf('mac') >= 0);
}
var fileRef = isMacOS()
? new File("/Library/Application Support/Adobe/CEP/extensions/my.panel/files/filename.psd")
: new File("C:\Program Files\Common Files\Adobe\CEP\extensions\my.panel\files\filename.psd");
var docRef = app.open(fileRef);
};

How to read and write to a file (Javascript) in ui automation?

I want to identify few properties during my run and form a json object which I would like to write to a ".json"file and save it on the disk.
var target = UIATarget.localTarget();
var properties = new Object();
var jsonObjectToRecord = {"properties":properties}
jsonObjectToRecord.properties.name = "My App"
UIALogger.logMessage("Pretty Print TEST Log"+jsonObjectToRecord.properties.name);
var str = JSON.stringify(jsonObjectToRecord)
UIALogger.logMessage(str);
// -- CODE TO WRITE THIS JSON TO A FILE AND SAVE ON THE DISK --
I tried :
// Sample code to see if it is possible to write data
// onto some file from my automation script
function WriteToFile()
{
set fso = CreateObject("Scripting.FileSystemObject");
set s = fso.CreateTextFile("/Volumes/DEV/test.txt", True);
s.writeline("HI");
s.writeline("Bye");
s.writeline("-----------------------------");
s.Close();
}
AND
function WriteFile()
{
// Create an instance of StreamWriter to write text to a file.
sw = new StreamWriter("TestFile.txt");
// Add some text to the file.
sw.Write("This is the ");
sw.WriteLine("header for the file.");
sw.WriteLine("-------------------");
// Arbitrary objects can also be written to the file.
sw.Write("The date is: ");
sw.WriteLine(DateTime.Now);
sw.Close();
}
But still unable to read and write data to file from ui automation instruments
Possible Workaround ??
To redirect to the stdout if we can execute a terminal command from my ui automation script. So can we execute a terminal command from the script ?
Haven't Tried :
1. Assuming we can include the library that have those methods and give it a try .
Your assumptions are good, But the XCode UI Automation script is not a full JavaScript.
I don't think you can simply program a normal browser based JavaScript in the XCode UI Automation script.
set fso = CreateObject("Scripting.FileSystemObject");
Is not a JavaScript, it is VBScript which will only work in Microsoft Platforms and testing tools like QTP.
Scripting.FileSystemObject
Is an ActiveX object which only exists in Microsoft Windows
Only few JavaScript functions like basic Math, Array,...etc..Are provided by the Apple JavaScript library, so you are limited to use only the classes provided here https://developer.apple.com/library/ios/documentation/DeveloperTools/Reference/UIAutomationRef/
If you want to do more scripting then Try Selenium IOS Driver http://ios-driver.github.io/ios-driver/
Hey so this is something that I was looking into for a project but never fully got around to implementing so this answer will be more of a guide of what to do than step by step copy and paste.
First you're going to need to create a bash script that writes to a file. This can be as simple as
!/bin/bash
echo $1 >> ${filename.json}
Then you call this from inside your Xcode Instruments UIAutomation tool with
var target = UIATarget.localTarget();
var host = target.host();
var result = host.performTaskWithPathArgumentsTimeout("your/script/path", ["Object description in JSON format"], 5);
Then after your automation ends you can load up the file path on your computer to look at the results.
EDIT: This will enable to write to a file line by line but the actual JSON formatting will be up to you. Looking at some examples I don't think it would be difficult to implement but obviously you'll need to give it some thought at first.

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

Javascript: debug stack trace with source maps

This may be a bit of an odd question, I can't seem to search the right phrase to pull up any relevant answers.
We have an app that runs on clients machines and is minified. We generate source maps, but they are not exposed to production versions.
I have a window.onerror that I use a catch all for sloppy code that finds it's way in unbeknownst to me. Thankfully, this is almost never utilized. Recently, I've been getting an undefined error popping up occasionally so someone has found a way to do something not intended. Chrome does a nice job recording row and column number in the stack trace which we log to a logging server when onerror catches one of these buggers, but that's all I have to debug with and looking through a min file is less than appealing. And undefined is not a function is not very helpful either :)
Question: is there a tool out there - maybe in nodejs - that can take a min file, source map, and a stack trace string and produce relevant files, line numbers, and column numbers?
I realize that the browser does this for you at runtime, but in this case I don't have that luxury as I'm trying to figure out what the error actually is after the fact.
Found this: https://github.com/thlorenz/stack-mapper
I use uglify which seems to produce the correct mapping that this needs and it looks like it will work for the case I suggested above.
Edit
Actually, this one works a lot better and is much simpler to use https://github.com/mozilla/source-map/.
Example Usage:
var fs = require('fs');
var smc = require('source-map');
var stack = "TypeError: undefined is not a function\r\nat h/min/min.js?1404839824760:9:23048";
stack = stack.split(/\r\n/g);
var error = stack.shift(); // First line is the actual error
var errors = [];
var file = null;
stack.forEach(function(line){
var _trace = line.split('/').pop();
var trace = {};
trace.filename = _trace.split('?').shift();
_trace = _trace.split(':');
trace.line = parseInt(_trace[1], 10);
trace.column = parseInt(_trace[2], 10);
errors.push(trace);
if(!file)
file = trace.filename.split('.').shift();
trace.filename = __dirname + '/../min/' + trace.filename;
});
// This does not account for multiple files in stack trace right now
var map = fs.readFileSync(__dirname + '/../src/' + file + '.js.map');
map = JSON.parse(map);
var sm = new smc.SourceMapConsumer(map);
console.log(sm.originalPositionFor(errors[0]));
stacktrace.js looks to be another useful tool to achieve this.
Example from their website:
var error = new Error('BOOM!');
StackTrace.fromError(error).then(callback).catch(errback)
=> Promise(Array[StackFrame], Error);
There is a very popular project called source-map-support which does this easily.
Client-side JavaScript:
<script src="browser-source-map-support.js"></script>
<script>sourceMapSupport.install();</script>
NodeJS (programmatic)
require('source-map-support').install();
NodeJS (CLI)
node -r source-map-support/register compiled.js
I found https://github.com/thlorenz/stack-mapper easy to use to do this automatically. If you are looking to do this interactively, you can use stack-mapper in a web browser with a tool like this one: https://github.com/Jimbly/stackwalker - just load your .map file, paste the (minified) callstack or single error location and then you can click through the stack and see the original code on the right.

Categories

Resources