Haxe -- Embed files like in ActionScript? - actionscript-3

In ActionScript, you can do something like this:
[Embed(source = "src/myfile.xml", mimeType = "application/octet-stream")]
private var xml : Class;
and it will embed your file to be used in code. How can i do something similar in Haxe?

Things have changed since the time the question was asked. With a modern version of haxe one can do:
#:bitmap("test.png") class TestBMD extends BitmapData {}
var bm = new Bitmap(new TestBMD(100,100));

Haxe allows you to provide external resources info for embedding in hxml.
You may refer to the doc.

If specifying width/height annoys you, and if you don't mind not using the #:bitmap metatag, you could do:
import openfl.Assets;
...
var bm = new Bitmap(Assets.getBitmapData("test.png"));

XML is easy to use haxe to get. Add -resource myfile.xml#myxml. Then, in your code, to get the xml string, use haxe.Resource.getString("myxml"). You can then parse this string to xml.

Related

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

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

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

How to add image in the library

I have the image path manipulate from database with php. Now how can i have to import those image in the library so that i can use them as a symbol. Thanks in advance.
You cannot load it into library, but you can load this image via php and use it on stage with action script. Take a look at this answer to the similar question:
Load image dynamicly in flash
You can download it to the server and generate source file with Class that have field like this:
class Library
{
[Embed(source = 'path_to_folder/image_name.png')] public static var imageClass : Class;
}
Alsou you should refer this class somewhere to be sure it will be compiled into your swf and use it like this:
const image:Bitmap = new (Library.imageClass)();
In that case you need to recompile your swf every time the image is changed.
But I'm totaly agree with Bartek that it is better to use dynamic loading.

Get Current Browser URL - ActionScript 3

I'm trying to get current browser url. I have already tried with External Call, and it didn't work. And with loaderInfo.url I receive the current SWF url.
Give this a go:
import flash.external.ExternalInterface;
var url:String = ExternalInterface.call("window.location.href.toString");
if (url) textfield.text = url;
should do the trick.
There are a couple of ways to solve this problem, however all of them involve the use of JavaScript to query the browser directly.
My preferred way to solve this problem would be to provide the URL via a flashVar property, direct from the embed code (personally, I would reccomend using SWFObject to make this easier); don't forget you will need to URL Encode it to avoid markup issues.
var flashvars = {
browserURL: escape(location.href)
};
swfobject.embedSWF("myContent.swf", "myContent", "300", "120", "9.0.0", "expressInstall.swf", flashvars);
Now you will be able to access the Browser URL via the loaderInfo object:
trace(stage.loaderInfo.parameters["browserURL"]);
note that this will only work if you have control of generated HTML for your SWF file - if users are going to be grabbing the SWF and writing their own embed HTML, it's not going to work.
If you don't have control of the flash embed HTML, then you will need to get flash to query the browser at runtime using the ExternalInterface class; other people have suggested the use of "window.location.href.toString" however this can prove problematic in IE6, I find the following works reliably across all browsers
const browserURL : String = ExternalInterface.call("eval", "window.location.href");
Note that in order for this to work, you will need to grant JavaScript access to your Flash movie, this is done, again, via the HTML embed code and the allowScriptAccess param
var url:String = loaderInfo.loaderURL;
seems to work too.
I would try passing the required info in as a flashvar. Not the best out of the box solution I know, but it will work.
Flash: FlashVars in AS3
i think its posible to use the external interface an do it with javascript window.location
I have been using flash for a long time and never noticed this one. It only gives the domain though for security. It does work through loaded swfs as well. Not sure about iframes.
Security.pageDomain

How can I convert HTML to Textile?

I'm scraping a static html site and moving the content into a database-backed CMS. I'd like to use Textile in the CMS.
Is there a tool out there that converts HTML into Textile, so I can scrape the existing site, convert the HTML to Textile, and insert that data into the database?
I know this is an old question, but I found myself trying to do this the other day and not finding anything useful, until I found Pandoc. It can convert loads of other markup formats as well - it's quite brilliant.
Here is a c# lib converting html 2 textile. Though it is textile with their additions. Not pure textile.
Since there was no javascript implementation, I wrote one:
https://github.com/cmroanirgo/to-textile
It's a little primitive at the moment, as it's a blind port of the 'to-markdown' equivalent, but should get the job done.
This is a simple markup replacement, nothing a good regex could not fix.
I recommend Perl, LWP::Simple and some regexes to do the whole thing (spidering, stripping design and menus, converting to textile, and then posting to the database.)
try this simple java code hope it work for you
import java.net.*;
import java.io.*;
class Crawle
{
public static void main(String ar[])throws Exception
{
URL url = new URL("https://www.google.co.in/#q=i+am+happy");
InputStream io = url.openStream();
BufferedReader br = new BufferedReader(new InputStreamReader(io));
FileOutputStream fio = new FileOutputStream("crawler/file.txt");
PrintWriter pr = new PrintWriter(fio,true);
String data = "";
while((data=br.readLine())!=null)
{
pr.println(data);
System.out.println(data);
}
}
}
}