Invoking javascript file with dom access - javascript

I have a javascript file that has code with DOM access
var a=document.getElementById("abc").value
I have html file, that contains all DOM information
<html>...<input id="abc" ...></html>
Is there anyway to get C# invoke the javascript file, and return the value of a, back to the c# program?
In reality the JavaScript can be much more complex, and I need to channel those "interested values" back to C#, but let's just consider the simple example mentioning here.
Possible directions I could think is using https://jint.codeplex.com/, or Web browser control. The challenge here is that it not only involves the JavaScript, it also involving the HTML file.
What I want to know is:
Is there a way to channel variable value from JavaScript back to C#?
How to get JavaScript evaluate DOM elements from a HTML file?

Using WebBrowser and Jint:
using (WebBrowser browser = new WebBrowser())
{
browser.ScriptErrorsSuppressed = true;
browser.DocumentText = #"<html><head/><body><input id=""abc"" value=""this is the value in input""></body></html>";
// Wait for control to load page
while (browser.ReadyState != WebBrowserReadyState.Complete)
Application.DoEvents();
dynamic d = (dynamic)browser.Document.DomDocument;//get de activex dom
var jengine = new Jint.Engine();
jengine.SetValue("document", d);
try
{
string val=jengine.Execute(#"var a=document.getElementById('abc').value;").GetValue("a").ToString();
Console.WriteLine(val);
}
catch (Jint.Runtime.JavaScriptException je)
{
Console.WriteLine(je);
}
}

Related

Get http page after javascript has been loaded

I need to get HTML page when it is already with javascript results.
I use C++ and Qt
...
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, &QNetworkAccessManager::finished, this, &MainWidget::onFinished);
manager->get(QNetworkRequest(QUrl("http://website.com")));
...
void MainWidget::onFinished(QNetworkReply *reply)
{
qDebug() << QString(reply->readAll());
}
but I only able to get such a thing:
<script>
loadMap("mapContainer");
var baseStopId;
function imReady()
{
getMovie().addListener("MARKER_SHOW", "loadSurrounding");
...
</script>
and I need to get results.
Not a javascript but ready html page after proccessing javascript. Any advices? I have found something with Python, but I wish to know if there any C++ variants?

load webpage completely in C# (contains page-load scripts)

I'm trying to load a webpage in my application background. following code shows How I am loading a page:
request = (HttpWebRequest)WebRequest.Create("http://example.com");
request.CookieContainer = cookieContainer;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
Stream st = response.GetResponseStream();
StreamReader sr = new StreamReader(st);
string responseString = sr.ReadToEnd();
sr.Close();
st.Close();
}
as you know, the server responses HTML codes or some javascript codes, but there are many codes which added to the webpage by javascripts functions. so I have to interpret or compile the first HTTP response.
I tried to use System.Windows.Forms.WebBrowser object to load the webpage completely, but this is a weak engine to do this.
so I tried to use CEFSharp (Chromium embedded Browser), it's great and works fine but I have trouble with that. following is how I use CEFSharp to load a webpage:
ChromiumWebBrowser MainBrowser = new ChromiumWebBrowser("http://Example/");
MainBrowser.FrameLoadEnd+=MainBrowser.FrameLoadEnd;
panel1.Controls.Add(MainBrowser);
MainBrowser.LoadHtml(responseString,"http://example.com");
it works fine when I use this code in Form1.cs and when I add MainBrowser to a panel. but I want to use it in another class, actually ChromiumWebBrowser is part of another custom object and the custom object works in background. also it would possible 10 or 20 custom objects work in a same time. in this situation ChromiumWebBrowser doesn't work any more!
second problem is the threading issue, when I call this function MainBrowser.LoadHtml(responseString,"http://example.com");
it doesn't return any results, so I have to pause the code execution by using Semaphore and wait for the result at this event: MainBrowser.FrameLoadEnd
so I wish my code be some thing like this:
request = (HttpWebRequest)WebRequest.Create("http://example.com");
request.CookieContainer = cookieContainer;
string responseString="";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
Stream st = response.GetResponseStream();
StreamReader sr = new StreamReader(st);
responseString = sr.ReadToEnd();
sr.Close();
st.Close();
}
string FullPageContent = SomeBrowserEngine.LoadHtml(responseString);
//Do stuffs
Can you please show me how to do this? do you know any other web browser engines that work like what I want?
please tell me if I'm doing any things wrong with CEFSharp or other concepts.

PDF hostContainer callback

Following this SO solution here to notify clients of a click event in a PDF document, how is it possible to notify the client when the PDF gets submitted by the client using this.myPDF.submitForm("localhost/Handler.ashx?r=2) function?
The PDF File is created inside a user control then rendered into a HTML object:
string container = ("<object data='/myfile.pdf' type='application/pdf'></object>");
The JS file attached to the PDF is done like this:
var webClient = new WebClient();
string htmlContent = webClient.DownloadString(fileurl + "pdf_script.js");
PdfAction action = PdfAction.JavaScript(htmlContnent, pdfstamper.Writer);
pdfstamper.Writer.SetOpenAction(action);
And the content of the js file:
this.disclosed = true;
if (this.external && this.hostContainer) {
function onMessageFunc(stringArray) {
try {
this.myPDF.submitForm("http://localhost/Handler.ashx?EmpNo=12345" + "#FDF", false);
}
catch (e) {
}
}
function onErrorFunc(e) {
console.show();
console.println(e.toString());
}
try {
if (!this.hostContainer.messageHandler);
this.hostContainer.messageHandler = new Object();
this.hostContainer.messageHandler.myPDF = this;
this.hostContainer.messageHandler.onMessage = onMessageFunc;
this.hostContainer.messageHandler.onError = onErrorFunc;
this.hostContainer.messageHandler.onDisclose = function () { return true; };
}
catch (e) {
onErrorFunc(e);
}
}
When the submitForm call is made the PDF contents (form fields) get saved successfully and an alert is displayed in the PDF by doing this:
message = "%FDF-1.2
1 0 obj
<<
/FDF
<<
/Status("Success!")
>>
>>
endobj
trailer
<</Root 1 0 R>>
%%EOF");
return message;
What I'm trying to do is to get the PDF to callback the client after the form submit call sent from this client, a way to acknowledge the client that the form has been submitted, not in a form of an alert, but rather, a way to trigger a function in the host (the container, an iframe, object...etc).
The FDF response you used was unknown to me, so I've learned something new from your question. I've studied the AcroJS Reference and the FDF specification in the PDF Reference, and now I have a better understanding of what your code does. Thank you for that.
I assume that you already know how to trigger a JavaScript message in an HTML file using a JavaScript call from a PDF. See the createMessageHandler() in the JavaScript Communication between HTML and PDF article.
I interpret your question as: "How to I invoke this method after a successful submission of the data?"
If there's a solution to this question, it will involve JavaScript. I see that one can add JavaScript in an FDF file, but I'm not sure if that JavaScript can 'talk to' HTML. I'm not sure if you can call a JavaScript function in your initial PDF from the FDF response. If it's possible, you should add a JavaScript entry to your PDF similar to the /Status entry.
The value of this entry is a dictionary, something like:
<<
/Before (app.alert\("before!"\))
/After (app.alert\("after"\))
/Doc [/MyDocScript1, (myFunc1\(\)),
/MyDocScript2, (myFunc2\(\))
>>
In your case, I would remove the /Before and /Doc keys. I don't think you need them, I'd reduce the dictionary to:
<<
/After (talkToHtml\(\))
>>
Where talkToHtml() is a method already present in the PDF:
function talkToHtml() {
var names = new Array();
names[0] = "Success!";
try{
this.hostContainer.postMessage(names);
}
catch(e){
app.alert(e.message);
}
}
I don't know if this will work. I've never tried it myself. I'm basing my answer on the specs.
I don't know if you really need to use FDF. Have you tried adding JavaScript to your submitForm() method? Something like:
this.myPDF.submitForm({
cURL: "http://localhost/Handler.ashx?EmpNo=12345",
cSubmitAs: "FDF",
oJavaScript: {
Before: 'app.alert("before!")',
After: 'app.alert("after")',
Doc: ["MyDocScript1", "myFunc1()",
"MyDocScript2", "myFunc2()" ]
}
});
This will only work if you submit as FDF. I don't think there's a solution if you submit an HTML query string.
In case you're wondering what MyDocScript1 and MyDocScript2 are:
Doc defines an array defining additional JavaScript scripts to be
added to those defined in the JavaScript entry of the document’s name
dictionary. The array contains an even number of elements, organized
in pairs. The first element of each pair is a name and the second
is a text string or text stream defining the script corresponding
to that name. Each of the defined scripts is added to those already
defined in the name dictionary and then executed before the script
defined in the Before entry is executed. (ISO-32000-1 Table 245)
I'm not sure if all of this will work in practice. Please let me know either way.

Flash and External Javascript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
calling a Flash ExternalInterface with JavaScript
I have a flash file with AS code. I want to run Javascript that will run a function the AS. For example: In the AS I have a function called "loadXML". The object that holds the SWF file called "pawel" (The ID of the object). How can I run a Javascript code that will run on "pawel" the function "loadXML"? I'm using Flash 6 with AS 3.
You should use ExternalInterface.addCallback() method. See http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/external/ExternalInterface.html#addCallback() for details.
I suggest you check this http://www.redcodelabs.com/2012/04/calling-actionscript-method-from-javascript/ it has running sample and as3/js code.
I believe the goal here is to send the swf (which has compiled an external .as file) the location of an xml file somewhere on the server so the swf can compile, parse, and create something.
I've done this many times, using flash variables, which may be useful to you as well. When embedding the .swf in your webpage you can send it flash variables via js or html, or dynamically with php. Once i have my fla working perfectly with hardcoded variables (like the xml) i then add the flash variable code to the head and make a few minor adjustments to the fla – which usually breaks the fla from running "properly" within flash (because it's now dependent upon these external variables)
anyways, here's how i do my flash vars (there's other ways, worth a good google search)
import flash.net.*;
var flashVars:Object = new Object();
flashVars = this.loaderInfo.parameters;
var xmlVal;
for (var item:String in flashVars)
{
switch (item)
{
case "xmlLocation" :
xmlVal = String(flashVars[item]);
break;
}
}
Here's the javascript which sends the values:
<script type="text/javascript">
//flashObj
var flashvars = {};
flashvars.xmlLocation = "http://google.com/myXML.xml";
var params = {wmode:"transparent"};
var attributes = {};
swfobject.embedSWF("images/banner.swf", "yourSliderId", "175", "300", "9.0.0", false, flashvars, params, attributes);
</script>
this is using SWFObject (an open API flash embedding js library) to handle the swf embed. I prefer it because if you look at the code above you can read it and understand it, the default way is really hard to read, edit and understand.
If you simply need XML and then you're done, this will work for you. If you still need to say hit the 'next' or 'previous' button using javascript, refer to this web site article, i believe this may help you out further:
http://arrixlive.wordpress.com/2005/03/25/javascript-in-love-with-flash-control-swf-from-javascript/

How to Programmatically Inject JavaScript in PDF files?

How to Programmatically Inject JavaScript in PDF files?
Can it be done without Adobe Professional?
My goal is: I want to show up the print dialog immediately when I open the PDF.
I know that this can be done with JavaScript code embedded in the document.
If you're developing in Java have a look at iText: http://www.lowagie.com/iText/
I think it supports what you are looking for.
There are also some .Net versions around: http://www.ujihara.jp/iTextdotNET/en/
iText (and iText_Sharp_) are quite capable of adding JS to an existing PDF... page actions, links, document level script, you name it.
The JavaDoc can be found here.
This was written with Java in mind, but the C# code would look almost identical (if not exactly the same, with the exception handling stripped out like this).
PdfReader myReader = new PdfReader( myFilePath ); // throws IOException
PdfStamper myStamper = new PdfStamper( myReader, new FileOutputStream(outPath) ); // throws IOE, DocumentException
// add a document script
myStamper.addJavaScript( myScriptString );
// add a page-open script, 1 is the first page, not zero0
PdfAction jsAction = PdfAction.javaScript( someScriptString );
myStamper.setPageAction( PdfWriter.PAGE_OPEN, jsAction, myStamper.getWriter(), pageNumber ); // throws PdfException (for bad first param)
PdfFormField button = PdfFormField.createButton(myWriter, PdfFormField.FF_PUSHBUTTON);
button.setWidget( myRectangle, PdfAnnotation.HIGHLIGHT_INVERT );
// the important part, adding jsAction
jsAction = PdfAction.javaScript( buttonScriptString );
button.setAdditionalActions( PdfAnnotation.AA_DOWN, jsAction ); // mouse down
myStamper.addAnnotation( pageNum, button );
myStamper.close(); // write everything out, throws DocumentException, IOE
I've studied the PDF Specifications.
Turns out that the PDF file format isn't that hard.
It has a nice feature that allows you to modify the document just by appending new content in the end of the file.
If you are trying to do the same thing... don't be afraid! go and look at the specs.

Categories

Resources