In swf AS3, how do you extract string content from a website - actionscript-3

In swf AS3, how do you extract string content from a website?
In a html web page, they display a single line of text. In the swf, I want to use as3 to access that page by its URL and retrieve that line. Is the a way to access the content?
Thanks in advance for your help!

You simply load the file and read it. Say you have this HTML located at domain.com/test:
<!DOCTYPE html>
<html>
<body>
<div class="target">Get this text</div>
</body>
</html>
You would load it in using a URLLoader in Flash:
var l:URLLoader = new URLLoader();
l.addEventListener(Event.COMPLETE, completeHandler);
l.load(new URLRequest("domain.com/test"));
That will load the above HTML in as a String in the completeHandler via Event.target.data. You can then do two things: Search for your string via RegEx or search for it by setting the HTML as an XML object.
Using RegEx, you would do it like this:
function completeHandler(e:Event):void {
var s:String = e.target.data;
var targets:Array = s.match(/(?<=<div class="target">).*(?=<\/div>)/igm);
}
targets would be an array of matches for a div with class name "target".
XML would be a bit more difficult, but infinitely more flexible and easy to maintain (in my opinion, at least). I won't give an example for how to do that, since parsing XML is a very common thing in AS3 and there are tons of other questions out there about it.
Hopefully that helps

Related

How to parse html from javafx webview and transfer this data to Jsoup Document?

I am trying to parse sidebar TOC(Table of Components) of some documentation site.
Jsoup
I have tried Jsoup. I can not get TOC elements because the HTML content in this tag is not part of initial HTML but is set by JavaScript after the page is loaded.
You can see my previous question here:JSoup cannot parse child elements after depth 2
The suggested solution is to examine what connections are made manually from the Browser Dev Tools menu find the last version of the website. Parsing sidebar TOC of some documentation site is just one component of my java program so I cannot do this manually.
JavaFX Webview(not Android Webview)
I have tried JavaFX Webview because I need a browser that executes javascript code and fills Toc tag components.
WebView browser = new WebView();
WebEngine webEngine = browser.getEngine();
webEngine.load("https://learn.microsoft.com/en-us/ef/ef6/");
But I don't know how can I retrieve HTML code of the loaded website and transfer this data to Jsoup Document?
ANy advice appreciated.
WebView browser = new WebView();
WebEngine webEngine = browser.getEngine();
String url = "https://learn.microsoft.com/en-us/ef/ef6/";
webEngine.load(url);
//get w3c document from webEngine
org.w3c.dom.Document w3cDocument = webEngine.getDocument();
// use jsoup helper methods to convert it to string
String html = new org.jsoup.helper.W3CDom().asString(webEngine.get);
// create jsoup document by parsing html
Document doc = Jsoup.parse(url, html);
I can't promise this is the best way as I've not used Jsoup before and I'm not an expert on the XML API.
The org.jsoup.Jsoup class has a method for parsing HTML in String form: Jsoup.parse(String). This means we need to get the HTML from the WebView as a String. The WebEngine class has a document property that holds a org.w3c.dom.Document. This Document is the HTML content of the currently showing web page. We just need to convert this Document into a String, which we can do with a Transformer.
import java.io.StringWriter;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.jsoup.Jsoup;
public class Utils {
private static Transformer transformer;
// not thread safe
public static org.jsoup.nodes.Document convert(org.w3c.dom.Document doc)
throws TransformerException {
if (transformer == null) {
transformer = TransformerFactory.newDefaultInstance().newTransformer();
}
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
return Jsoup.parse(writer.toString());
}
}
You would call this every time the document property changes. I did some "tests" by browsing Google and printing the org.jsoup.nodes.Document to the console and everything seems to be working.
There is a caveat, though; as far as I understand it the document property does not change when there are changes within the same web page (the Document itself may be updated, however). I'm not a web person, so pardon me if I don't make sense here, but I believe that this includes things like a frame changing its content. There may be a way around this by interfacing with the JavaScript using WebEngine.executeStript(String), but I don't know how.

HTML from external file in MovieClip?

I have a client that is requesting an area be built on the stage of an existing .fla file (that I built as well) that loads html content from an external file. This area will serve as a dynamic ad banner that displays the contents of the external file. Ideally, the external file would contain links to different .html files that can easily be edited as the content needs change.
I have tried HTMLloader, but I am limited to a .swf file only and cannot use AIR.
Any thoughts on the best way to do this?
Thanks in advance!
If you are in fact needing to pull content from an external .html file, the following code showcases how to load external html content. In my example, I stored some content in a file named 'MyBannerAd.html', but you can point it to wherever you want and whatever filename you wish. Once the file has loaded, the externalFileLoadComplete function is called at which time I created a textfield and stored the contents of the file that was loaded from the external file as the htmlText property of the TextField. If you already have a textfield created, you can simply reference that one instead. The code shown below executed successfully using Flash CC 2014.
import flash.text.TextField;
function externalFileLoadComplete(e:Event):void
{
var myTextField:TextField = new TextField();
myTextField.htmlText = e.target.data;
addChild(myTextField);
}
var fileLoader:URLLoader = new URLLoader();
var fileRequest:URLRequest = new URLRequest("MyBannerAd.html");
fileLoader.addEventListener(Event.COMPLETE, externalFileLoadComplete);
fileLoader.load(fileRequest);

Is it possible to embed a HTML into a PDF?

I like to embed a HTML site into a PDF document. Are there any libraries or PDF creator that make that possible?
Update:
I am not looking for ways to convert a HTML to PDF. I actually want to use the HMTL as it is inside the PDF. So I am looking for something like iframe for PDF.
There are a few out there, depends if you need to build using PHP or another language. I have used MPDF before: http://www.mpdf1.com/mpdf/
Yes, its possible using xmlworker5.4.1.jar. The XML worker object allows you to embed html in your document. xmlString object below is your HTML content as HTMLWorker is deprecated so use XMLWorker only.
XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
String currentLine = "";
StringBuffer xmlString = new StringBuffer();
xmlString.append("<html><body>");
String str = htmlPages[i];
xmlString.append(htmlPages[i]);
xmlString.append("</body></html>");
worker.parseXHtml(pdfWriter, pdfDocument, new StringReader(xmlString.toString()));
Inorder to incorporate fonts mentioned in font face tag u need to register fonts using
FontFanctory.redisterDirectory("path of font files");
because itext doesnt scan system for fonts. u need to register it yourself this way

firefox does not load an xml into an swf called by a parameter in the html-code (works fine in IE)

I built an excercise in flash cs5/as3. It draws its content from an xml-file.
Because I want to make it easy for other people to create their own excercise based on this swf, I want the actionscript to load the xml, with the filename based on a parameter in the html code.
In this case the xml is called oef01.xml
The link would look like this: BoFlitsOefening.swf?id=oef01
And the actionscript like this:
public function Main ()
{
//myFile is a string I declared earlier
myFile = LoaderInfo(this.root.loaderInfo).parameters["id"];
myFile += ".xml";
loadXml ();
}
function loadXml ():void
{
xmlLoader = new URLLoader(new URLRequest(bestand));
xmlLoader.addEventListener (Event.COMPLETE,xmlLoaded);
}
function xmlLoaded (event:Event):void
{
myList = new XML(event.target.data);
myList.ignoreWhite = true;
}
The construction is working fine in Internet Explorer but not in Firefox. I have done internet research but I could not find an explanation or a solution I was able to apply, though the issue is known.
Are you testing it on the server or locally - the URL in your browser should start with http:// not file:///?
It should work fine on the internet, while locally the URLs containing ? may not resolve properly.
In that case you may use FlashVars instead - you don't need to change the AS code, just HTML/JS.
And on a side note: you may try to embed the SWF file using SWFObject - some crossbrowsers issues are caused by wrong/buggy embedding code.
Also FireFox likes to keep external sources cached, so there's a likelihood that it's loading an old file. Make sure you clear the cache after updating the xml.
there is a way of tricking it to load it fresh every time by adding some junk after, like
.../my.xml?rand=001820018
where you generate the number randomly every time, if I remember correctly

Attachment + Email + HTML + Play Framework

I'm using play framework in this project and I'm trying to send an E-mail with a Logo attached but I want to show this logo as part of my HTML code!
My Mailer:
EmailAttachment attachment = new EmailAttachment();
attachment.setDescription("Logo");
attachment.setName("logoMail.jpg");
attachment.setPath(Play.getFile("/public/images/email/logoMail.jpg").getPath());
addAttachment(attachment);
My HTML
The e-mail is sent, my Logo is attached there, but the image is never showed as a background on my DIV.
What am I doing wrong?
Thank you very much!
It depends on the e-mail client you are using to read your test e-mail. Most of them ignore or remove the background-image css property.
Take a look at the following:
http://www.email-standards.org/
http://www.campaignmonitor.com/design-guidelines/
I've been looking into embedding images into emails using MVC templates, and I think at the moment it's not supported.
As far as I can see, in order to use embedded images, the image attachment needs to have a Content-ID header on it. Attaching the image using addAttachment generates an attachment without this header.
The underlying email framework, apache commons email, allows you to embed images using the HtmlEmail.embed method, and there is an example of this in the Play documentation, but only when using Commons Email directly. addAttachment() will add an ordinary attachment, not an embedded one.
The problem is that HtmlEmail.embed returns the content id for the embedded image. The first problem is that there would need to be a mechanism for passing that content id forward into the template, so that you could reference it in the relevant link.
The second problem is that the way the Mailer.send() method is coded, the email itself is not created until after the template is rendered, and the result of attempting to render an html body is used to decide whether to create an HtmlEmail or a SimpleEmail. This method would need to be re-written to decide the type of email before rendering the template, and, if it was html, to create the HtmlEmail and attach the embedded images prior to rendering the template, so that it could pass the content ids to the renderer.
It certainly isn't impossible to make this change, and I might attempt it if I can find the time on my current project.
The solution could be to render HTML content manually and then put it into email. This code worked for me:
public static String test() throws EmailException, MalformedURLException {
HtmlEmail email = new HtmlEmail();
email.setHostName("smtp.server.com");
email.setAuthentication("username", "pwd");
email.setSubject("subject");
email.addTo("to#example.com");
email.setFrom("from#example.com");
URL url = new URL("https://example.com/image.png");
String cid = email.embed(url, "IMG1");
Template templateHtml = TemplateLoader.load("/Mails/test.html");
final Map<String, Object> templateHtmlBinding = new HashMap<String, Object>();
templateHtmlBinding.put("cid", cid);
String body = templateHtml.render(templateHtmlBinding);
email.setHtmlMsg(body);
return email.send();
}
I'm a bit late with my answer, but it is possible and integrates nicely with the MVC-Email tutorial. Assuming your mailer class is also notifiers.Mails, use this as a html template:
%{
String logoSrc = notifiers.Mails.getEmbedddedSrc("public/images/logo.png", "cool logo");
}%
<html>
<head>
</head>
<body>
Look at this cool image! <br>
<img src="${logoSrc}">
<br>
Amazing, no?
</body>
</html>