Call Perl script from Thunderbird Javascript - javascript

I wrote a perl script that handles some data automatically. However, I face a problem when I try to call the script from my thunderbird extension that is naturally written in javascript.
var file = Components.classes["#mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
file.initWithPath("/usr/bin/perl");
// create an nsIProcess
var process = Components.classes["#mozilla.org/process/util;1"]
.createInstance(Components.interfaces.nsIProcess);
process.init(file);
// Run the process.
// If first param is true, calling thread will be blocked until
// called process terminates.
// Params are used to pass command-line arguments
// to the process
var args = ["package/myperlscript.pl", "some arguments];
process.run(true, args, args.length);
I guess I have the perl script placed at the wrong location. I tried various ones, but I could not get it work. If that is my major mistake, where is the base path that the javascript file expects?

Related

Is it possible to run a JavaScript file from a C# file?

When I say JavaScript file, I mean the whole file. Not a function. I've seen ways to run a JavaScript function from c# but nothing about running a file.
According to this question: https://stackoverflow.com/a/1469790/13105088, one could run the command with any normal shell.
string strCmdText;
strCmdText= "/C node myscript.js"; // the command to run from the command prompt
System.Diagnostics.Process.Start("CMD.exe",strCmdText);
Note that this will show the Command Prompt on Windows.
This following script prevents that.
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C node myscript.js";
process.StartInfo = startInfo;
process.Start();
(these example scripts were both provided by the answer I linked to above.)
In addition, you should probably also note:
Important is that the argument begins with /C otherwise it won't work. How Scott Ferguson said: it "Carries out the command specified by the string and then terminates."
Note: this is all assuming you are referring to NodeJS when you are saying a "JavaScript file", but any other interpreter (eg. /C python3 myfile.py) should also work.

How to pass a dynamic JS file into PhantomJS

I am trying to take the value of am input, use AJax to submit these variables into a php function, call PhantomJS from said PHP function WITH these arguments passed from AJax, and return the result back to the HTML page. I am passing the variables to the PHP file perfectly fine, the problem arises from calling PhantomJS with my script followed by the three arguments.
This is the script on my PHP page to call PhantomJS
echo json_encode(array("abc" => shell_exec('/Applications/XAMPP/htdocs/scripts/phantom/bin/phantomjs /Applications/XAMPP/htdocs/scripts/phantom/examples/test.js 2>&1',$website)));
This is the script referenced in the shell script:
var args = require('system').args;
args.forEach(function(arg, i) {
console.log(i+'::'+arg);
});
var page = require('webpage').create();
var address = args[1];
page.open(address, function () {
console.log("Done")
});
As you can see it should be a relatively simple process, except nothing at all is being echo'd. Permissions for each file are more than adequate, and I am sure these files are executing because if I change the shell script to run hello.jsEverything echo's and logs perfectly.
ALSO NOTE This script is executing on my web server, so I am not 100% certain there IS a system variable.
Any ideas?
First issue, shell_exec() takes a single argument (Documentation). However your example is passing the shell argument ($website) as a second argument on shell_exec().
Corrected Example:
$shellReturn = shell_exec("/Applications/XAMPP/htdocs/scripts/phantom/bin/phantomjs /Applications/XAMPP/htdocs/scripts/phantom/examples/test.js " . $website);
echo json_encode(array("abc" => $shellReturn));
For simplicity i excluded the redirect of the error pipe. In addition i would suggest you pass the arguments as JSON wrapped in base64 encoding. This eliminates URL spacing resulting in multiple arguments. Once PhantomJS receives the system args use atob() to bring the JSON back and iterate over the JSON obj rather than the raw string arguments.
I would also point you towards this project: https://github.com/merlinthemagic/MTS, Under the hood is an instance of PhantomJS, the project just wraps the functionality of PhantomJS.
$myUrl = "http://www.example.com"; //replace with content of your $website variable
$windowObj = \MTS\Factories::getDevices()->getLocalHost()->getBrowser('phantomjs')->getNewWindow($myUrl);
//if you want the DOM or maybe screenshot and any point run:
$dom = $windowObj->getDom();
$imageData = $windowObj->screenshot();

Execute an external .js file using selenium webdriver

How can we execute an external .js file using selenium webdriver file using java selenium. I got some reference "Call external javascript functions from java code", however invoke function is able to accept the function inside that file. I want to just execute the whole file as a whole.
It's as simple as this to run an external JavaScript from your server upon the client:
// Assume Guava, but whatever you use to load files...
String externalJS = Files.toString( new File("external.js"), Charset.forName("utf-8"));
// Execute, assume no arguments, and no value to return
Object ignore = ((JavascriptExecutor) driver).executeScript(externalJS);
The link you provided isn't useful, because it's about executing JavaScript upon the server (within the Java VM) rather than upon the browser/device client.
If rather than executing, you're interested in injecting JavaScript into the page for other scripts etc. to interact with (i.e. rather than a one-off execution), see this question.
Here is the code for nodeJS calling external JS and executing a function within the JS:
var fs = require('fs');
var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until;
var driver = new webdriver.Builder()
.forBrowser('phantomjs')
.build();
var axeSource = fs.readFileSync('lib/axe.js', 'utf8');
driver
.get('http://www.google.com/ncr')
driver.executeScript(axeSource)
.then(function(){
driver.switchTo().defaultContent();
driver.executeAsyncScript(function() {
var callback = arguments[arguments.length - 1];
window.axe.a11yCheck(document, null, function (results) {
callback(results);
});
}).then(function(str) {
var viola = processResults(str);
console.log(viola);
});
})
driver.quit();

Emscripten code for image data download using emscripten_wget or emscripten_async_wget

I'm writting a web app that has to execute some c code so I went for emscripten. The workflow I need is the following:
Download an image from a server and save it's data into the File System.
Call some c functions to do some processing.
Call some WebCrypto API functions to decrypt info.
Call again some other c functions to do some processing.
Show the result in the browser.
As it involves a lot of Javascript and C mixing I have no idea how to handle the run loop, the async, etc as I'm completely new to emscripten.
My thoughts are using emscripten_wget for the first points and save it using a FS of type MEMFS. The problem I have is loading the image synchronously.
So, in my php file I have this:
<script type="text/javascript" src="a.out.js"></script>
<script>
P3Func = Module.cwrap('p3stuff', 'number', ['string', 'string']);
P3Func("<?php echo($imgurl); ?>", "<?php echo($wrappedkey); ?>");
</script>
And in my c file I have this:
int p3stuff(char *imgUrl, unsigned char *wrappedkey)
{
// create a directory "working" in the virtual file system and mount it to the current Posix file system.
EM_ASM(
FS.mkdir('/working');
FS.mount(MEMFS, {}, '/working');
);
char *path = "/working/myImage.jpg";
printf("Downloading...\n");
//Download image and save it to the File System
emscripten_wget(imgUrl, path);
printf("Download completed\n");
FILE *img;
img=fopen(path,"r");
if(img==NULL) {
printf("ERROR: Can't open file!\n");
exit (2);
}
}
Actually I get a weird behavior: with a wrappedkey param lenght of 24 characters I get the image downloaded and read properly and if the lenght is instead 22 I get the can't open file error wich for me it doesn't make any sense:
exit(2) called, but noExitRuntime, so halting execution but not exiting the runtime or preventing further async execution (you can use emscripten_force_exit, if you want to force a true shutdown)
And then:
still waiting on run dependencies:
a.out.js:139 dependency: cp /working/myImage.jpg
a.out.js:139 (end of list)
repeated many times.

How can I list websites on IIS7, from script, without using IIS6 compat pack (WMI veneer)

On IIS6, I can use WMI to list available websites, like this:
var iis = GetObject("winmgmts://localhost/root/MicrosoftIISv2");
var query = "SELECT * FROM IIsWebServerSetting"
// get the list of virtual servers
var results = iis.ExecQuery(query);
for(var e = new Enumerator(results); !e.atEnd(); e.moveNext()) {
var site = e.item();
// site.Name // W3SVC/1, W3SVC/12378398, etc
// site.Name.substr(6) // 1, 12378398, etc
// site.ServerComment) // "Default Web Site", "Site2", etc
// site.ServerBindings(0).Port // 80, 8080, etc
}
I know I can run this script on IIS7, if I have previously installed the IIS6 Compatibility Pack.
Is it possible to get the list of WebSites without requiring the compatibility pack as a pre-requisite?
I know I can run AppCmd to do this from the command line:
\Windows\system32\inetsrv\appcmd list sites
But... can I run that from a custom action in an MSI?
And... if not, how can I do the equivalent thing (list websites on IIS7) from javascript?
EDIT
Here's how I tried running the command from within Javascript.
function GetWebSites_IIS7()
{
var ParseOneLine = function(oneLine) {
...a bunch of regex parsing here....
};
LogMessage("GetWebSites_IIS7() ENTER");
var shell = new ActiveXObject("WScript.Shell");
var windir = shell.Environment("system")("windir");
// aka Session.Property("%WINDIR%")
var appcmd = windir + "\\system32\\inetsrv\\appcmd.exe list sites";
var oExec = shell.Exec(appcmd);
var sites = [];
while (!oExec.StdOut.AtEndOfStream) {
var oneLine = oExec.StdOut.ReadLine();
var line = ParseOneLine(oneLine);
LogMessage(" site: " + line.name);
sites.push(line);
}
return sites;
}
This works, but it briefly pops a visible console window, which then disappears. Doesn't look very polished. I think I can avoid the console window by using shell.Run() instead of shell.Exec(). But shell.Run() doesn't give access to the stdout, so I would have to redirect the output to a temporary file, then read the output. I haven't tried that yet. That may introduce some security issues; I'll have to see.
Related:
Where and how should my CustomAction create and read a temporary file?
Yes, you can run appcmd from the custom action the same way you do any custom action which runs exe. First off, you should author a DirectorySearch/FileSearch elements to find the full path to the executable. Next, add a custom action with ExeCommand attribute. You're probably trying to get feedback from a user, so leave it immediate. Also, think about using QuietExec in order not to show console window to your users.
By the way, if my guess is correct, you're trying to do something like this. Hope this helps.

Categories