Reading file in PERL using ActiveX - javascript

I have been trying to read a file in PERL using ActiveX Control. I was successful in reading the file using ActiveX Controls in HTML. So I changed the HTML code to PERL cgi using "Print" Statement. After that I am not able to read the file the file using ActiveX, other JavaScript functions are working good.
print "<script src='/EnvelopeUtility/EnvelopeJS.js'></script>";
Above line of code is used to invoke the external JavaScript.
print "<td><input type='button' name='btn1' value='Process' onClick='getData(document.getElementById('sampleFile').value)'></td>";
Above line of code is an button in HTML, which onClick invokes the JavaScript method getdata()
var obj = new ActiveXObject('Scripting.FileSystemObject');
Above line of code is present in external JavaScript which creates an ActiveX Object to read the file.
readFile = obj.OpenTextFile(path, 1, false);
while(!readFile.AtEndOfStream)
{
readFile = obj.OpenTextFile(path, 1, false);
.................
Above line of code is the one which reads the file line be line.
The above code is working fine in HTML but in CGI after using print statement am not able to read the file. Please do suggest what should be changed in order to read the file.

You have a simple HTML error.
onClick='getData(document.getElementById('sampleFile').value)'
Your attribute value is delimited by ' characters, but you try to use literal ' characters as raw data inside the attribute.
Consequently the value of the attribute becomes getData(document.getElementById( which isn't valid JavaScript.
This would have been picked up if you had used a validator.
If you want to use a ' as data in an attribute value delimited by ' then you have to represent it as '.

Related

Call Javascript function from external file with Applescript

Is it possible to call a javascript file function in applescript? I can get it to work when written 100% in the applescript file or I can use "do javascript file some file". Is there a way to mix the two? Something like this:
tell application id "com.adobe.Photoshop"
tell current document
do javascript "function(Variable1,Variable1)" in file PathtoJSX
end tell
end tell
The function is located in the external file but I want to pass it variables from applescript.
Update:
I posted it when someone else ask but PathtoJSX is the "Path to Javascript file (.jsx)" so PathtoJSX = file "desktop:yourFile.jsx"
I think what you want to do is have the function part of the code in the file and send the arguments via AppleScript as a list (Array), which is pretty easy to do:
The contents of the jsx file could be:
testFunc(arguments);
function testFunc(t) {
alert('argument one: ' + t[0] + '\r' + 'argument two: ' + t[1]);
}
and the AppleScript could be:
tell application id "com.adobe.Photoshop"
activate
do javascript PathtoJSX with arguments {"foo", "bar"}
--I tested using:-- do javascript (choose file) with arguments {"foo", "bar")
end tell
Whatever you use for the JS code (text or file), you just make sure you use the arguments array reference in that code. Note that if you test this (as I did) by using a text variable instead of an alias (file reference), the return character would need a double escape.
Try this:
tell application "Adobe Photoshop CS4"
activate
do javascript (file "/Users/Michael/Desktop/somescript.jsx") with arguments {"foo", "bar"}
end tell
Regards.

How to update JavaScript variable using PHP

I have a JavaScript file named a pricing.js which contains this content in it:
var price_arr = new Array('$ 16.95','$ 30.95','$ 49.95','$ 70.95','$ 99.95','$ 109.95','$ 139.95','$ 155.95','$ 199.95','$ 460.95');
But I want to to update this part of JavaScript file using PHP so please help me in this how can I update this part of the content from JavaScript file using PHP?
'$ 16.95','$ 30.95','$ 49.95','$ 70.95','$ 99.95','$ 109.95','$ 139.95','$ 155.95','$ 199.95','$ 460.95'
Please understand that Javascript is Client Side code and PHP is server side code.
There can be 2 possible ways or scenarios which you want to achieve:
If you want to manipulate the JS file before sending it to client, you can rename the Javascript file to have .php extension and write php code to provide a variable. For Ex:
<?php
// write php code to create a string of values using a for loop
$price_array_string = "'$ 16.95','$ 30.95','$ 49.95',
'$ 70.95','$ 99.95','$ 109.95','$ 139.95','$ 155.95','$ 199.95','$ 460.95'";
// Javscript Code var price_arr = new Array(`<?`php echo $price_array_string ?>);
The other alternative is that you get the value of price array by making an Ajax Request to a PHP web service.
Simply give the pricing.js file a .php extension. You can then include PHP in the javascript file the way you would for any other page. Just be sure your HTML code specifies that it's still "text/javascript" when you load it. You'll probably want to set the content-type in the PHP file as well, like so: header("Content-type: text/javascript");
Using your PHP script you can put a value in an HTML5 data attribute of an element on your page and then read it from your JavaScript, e.g. <body data-array="'$ 16.95','$ 30.95'"> can be generated with PHP, and then read with JS: var are = new Array($('body').attr('data-array').split(',')). Here jQuery is used in order to read the attribute value, but you can also do it with pure JavaScript.

Transferring javascript from a view to a seperate JS file

I am working on a legacy application and I want to move some JS code onto a separate JS file.
I will have to refractor some of the code to do this. I can put #Url.Content statements into data attributes in the HTML.
But how would I replace this line of code?
var array = #Html.Raw(Json.Encode(ViewBag.JobList));
A separate JS file will not know what #Html.Raw means.
Server side code like that cannot run in a seperate javascript file. My solution for such problems is having a short javascript part in the head that runs on the onload event. There you can set variables that you can use in a seperate javascript file:
in the head:
array = #Html.Raw(Json.Encode(ViewBag.JobList));
in the seperate javascript file:
var array;
Then, in the seperate javascript file you can do with your array whatever is necessary.
The ViewBag.JobList data is only known at HTML page generation time. To include it in an external JavaScript file, you have to have another ASP.NET resource that recalculated ViewBag.JobList and then served as part of a dynamic JavaScript file. This is pretty inefficient.
Instead, do what you're doing with the URLs: pass the data through the DOM. If you're writing into normal DOM instead of a script block, you don't need the raw-output any more (*), normal HTML escaping is fine:
<script
id="do_stuff_script" src="do_stuff.js"
data-array="#Json.Encode(ViewBag.JobList)"
></script>
...
var array = $('#do_stuff_script').data('array');
// jQuery hack - equivalent to JSON.parse($('#do_stuff_script').attr('data-array'));
(Actually, the raw-output might have been a security bug, depending on what JSON encoder you're using and whether it chooses to escape </script to \u003C/script. Writing to HTML, with well-understood HTML-encoding requirements, is a good idea as it avoids problems like this too.)
I think you need to create action with JavaScriptResult
public ActionResult Test()
{
string script = "var textboxvalue=$('#name').val();";
return JavaScript(script);
}
But, before proceeding please go through following links
Beware of ASP.NET MVC JavaScriptResult
Working example for JavaScriptResult in asp.net mvc
I would also follow MelanciaUK's suggestion :
In your javascript file, put your code inside a function :
function MyViewRefactored( array ){
... your code ...
}
In your view, leave a minimal javascript bloc :
<script>
var array = #Html.Raw(Json.Encode(ViewBag.JobList));
MyViewRefactored( array );
</script>

Python and reading JavaScript variable value

I am very new to Python programming, so please bear with me.
I have an HTML file with several div layers. This file is opened in a webkit.WebView object. Each div layer saves a value in a global variable (JavaScript) when clicked upon.
How can I read the value of that global JavaScript variable from my Python script?
I found some answers but they don't seem to fit my situation (but I can be wrong, of course):
Passing JavaScript variable to Python
Parse JavaScript variable with Python
[EDIT]
I'm using webkit.WebView because I have to show this in an existing glade (libglade) application.
try this out. It uses the addToJavaScriptWindowObject method to add a QObject into the QWebView. This will enable communication between your python script and the HMTL/Javascript in your webview. The example below will let you change the value of the javascript global variable message to whatever value you want through a JavaScript prompt, then whenever you click on the Python Print Message link it will execute your python code that will take the javascript value and just print it to the console.
import sys
from PyQt4 import QtCore, QtGui, QtWebKit
HTML = """
<html><body onload="broker.print_msg(message)">
<script>var message = 'print_msg message'</script>
Change Message<br/>
Python Print Message
</body></html>
"""
class Broker(QtCore.QObject):
#QtCore.pyqtSlot(str)
def print_msg(self, data):
print data
app = QtGui.QApplication(sys.argv)
view = QtWebKit.QWebView()
view.page().mainFrame().addToJavaScriptWindowObject('broker', Broker(view))
view.setHtml(HTML)
window = QtGui.QMainWindow()
window.setCentralWidget(view)
window.show()
sys.exit(app.exec_())
I know this is old question, but still answering it with the hope that it will be useful to someone.
You can use alert handler and read the value in python side. http://webkitgtk.org/reference/webkitgtk/stable/webkitgtk-webkitwebview.html#WebKitWebView-script-alert
Example on button click you can have action that says
alert("Button Clicked");
On python side you will get the alert notification and you can parse the string. If the object is not a simple object, you will have to convert it to string format that can be parsed on python side. I have seen few examples of alert handler. https://github.com/nhrdl/notesMD/blob/master/notesmd.py is one that I wrote and uses alert handlers to pass lot of data between javascript and python

JMeter modifying output to file from XML Stream

I'm attempting to write a JMeter script which after receiving and XML response from a server, extracts a string from it on the fly (drops the first part of the response) and writes it to a file.
Currently I use a Save Response Data to write to ChannelData_UAT_1 (filename). All good, it writes happily.
Then I add a BSF PreProcessor BEFORE it, and use javascript to try and extract the string. It's a bunch of XML tags, I want everything from "<Markets>" onwards.
I use:
function extract_markets(str)
{
marketIndex = str.indexOf("<Markets");
__log(marketIndex);
length = str.length;
marketString = str.substring(markeIndex, length-1);
return str;
}
vars.put('ChannelData_UAT_1', extract_markets(vars.get('ChannelData_UAT_1')));
As far as I can tell, ChannelData_UAT_1 is the variable the data is in. However this is only mentioned in the Save Response Data. But I can't do it afterwards otherwise it'll have already written to the file.
The current performance is for it to receive the response and write to the file. No filtering is done - as if my javascript didn't exist.
Anything small or obvious that I've missed? Suggestions?
I believe the issue stems from the fact that ChannelData_UAT_1 is not a variable and how Save Response Data works.
ChannelData_UAT_1 is the file name, not the content of the file.
You need to modify the contents of the "Response". You can replace the value of the page response with the value of your function.
I think the code would look something like this:
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jmeter.samplers.SampleResult;
prev.setResponseData(extract_markets(vars.get('ChannelData_UAT_1')));
Source:
http://www.javadocexamples.com/java_examples/org/apache/jmeter/samplers/SampleResult/

Categories

Resources