Inject html/javascript-code into Flex AS3 HTML class - html

I need to Inject html/javascript-code into every HTML that gets loaded.
The program uses the HTML class.
I disassembled the original Flex3 files and found this.
The important? functions I modified/tested:
public function set htmlText(value:String) : void
{
_htmlText = value;
htmlTextChanged = true;
_location = null;
locationChanged = false;
invalidateProperties();
invalidateSize();
invalidateDisplayList();
dispatchEvent(new Event("htmlTextChanged"));
}
public function set location(value:String) : void
{
_location = value;
locationChanged = true;
_htmlText = null;
htmlTextChanged = false;
invalidateProperties();
invalidateSize();
invalidateDisplayList();
dispatchEvent(new Event("locationChange"));
}
I successfully changed the code of set location and managed that only http://www.example.com/ gets loaded.
However, the htmlText setter seems not to get called when the location is set using location.
This is what I have tried:
public function set htmlText(value:String) : void
{
_htmlText = "<html><h1>test</h1></html>" + value;
htmlTextChanged = true;
_location = null;
locationChanged = false;
invalidateProperties();
invalidateSize();
invalidateDisplayList();
dispatchEvent(new Event("htmlTextChanged"));
}
I need to have a look in flash.net.HTMLloader but I cannot locate the file.
I hope someone can help me and tell me where the HTML-code gets loaded from the location and where it gets rendered so I can modify it before.
Thanks in advance.

I don't know what do you want to inject to your html content, but I think that you can do it when the complete event on the HTMLLoader of your HTML component is fired, using the window object which is :
The global JavaScript object for the content loaded into the HTML control..
So you can use it ( the window object ) as in any JavaSript code.
// for the example, I took the page of your current question
var url:String = 'http://stackoverflow.com/questions/32348824/inject-html-javascript-code-into-flex-as3-html-class';
html_content.location = url;
html_content.htmlLoader.addEventListener(Event.COMPLETE, on_complete);
And
protected function on_complete(e:Event): void
{
var html_loader:HTMLLoader = html_content.htmlLoader;
var document = html_loader.window.document;
var _head = document.getElementsByTagName('head')[0];
// create a new css class
var _class = '.akmozo { background: #ff9900; color: white; padding: 2px; }';
var _style = document.createElement('style');
_style.appendChild(document.createTextNode(_class));
// create a new js function
var _js = 'function click_me(){ alert("Hello, how are you ?"); }';
var _script = document.createElement('script');
_script.appendChild(document.createTextNode(_js));
_head.appendChild(_style);
_head.appendChild(_script);
// change the page's top bar background color to green
if(document.querySelector('.topbar-wrapper'))
{
document.querySelector('.topbar-wrapper').style.backgroundColor = 'green';
}
// edit the title of your question,
// apply the new css class and call the js function
if(document.querySelector('.question-hyperlink'))
{
document.querySelector('.question-hyperlink').innerHTML += ' [ edited by <span class="akmozo" onclick="click_me()">AKMOZO</span> ]';
}
// change the SO logo to my avatar
if(document.getElementById('hlogo'))
{
document.getElementById('hlogo').style.backgroundImage = 'url(https://i.stack.imgur.com/YAKpv.png?s=50)';
document.getElementById('hlogo').style.backgroundRepeat = 'no-repeat';
}
// do some changes in your comment
if(document.querySelector('#comment-52605069'))
{
document.querySelector('#comment-52605069 .comment-copy').style.color = 'green';
document.querySelector('#comment-52605069 .comment-copy').style.fontWeight = 700;
document.querySelector('#comment-52605069 .comment-copy').style.fontSize = '24px';
document.querySelector('#comment-52605069 .comment-user').innerHTML = '<span class="akmozo">akmozo</span>';
}
}
This code will give you something like this :
Of course I tried just to give you an example of what you can do, you have to improve and adapt this code to your needs.
Hope that can help.

Related

How to get image if I drag and drop an Image from Google Chrome or Internet Explorer in AS3

I am trying to drag an Image from Google Chrome or Internet Explorer and Drop into my Flex Project but I am unable to get Image directly from temp folder like Mozilla Firefox,
For Example I am using the following codes go get drag drop images from system temp folder in case of Mozilla Firefox:
this.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, onNativeDragDrop);
private function onNativeDragDrop(vEvent:NativeDragEvent)
{
var dropFiles:Array = vEvent.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
if(dropFiles && dropFiles.length > 0)
{
var original:File = File(dropFiles[0]);
var nativePath:String = original.nativePath;
}
}
In nativePath i am getting the path where the image is initially stored like : "C:\Users\User_Name\AppData\Local\Temp\ubeh2wbl.bmp"
But In case of Google Chrome or Internet Explorer I am getting NULL in nativePath.
So I Don't know where Google Chrome or Internet Explorer initially storing the images.
Does anyone have an idea where it is storing or how to solve this problem ?
I just tried this, and when you drag an image from Chrome over the an AIR app, it comes in as the following formats:
file promise list
url
text
html
The file promise list is empty, so we'll focus on the others.
Here is the code I used which falls back through several formats to try and get a dragged image or image path:
private function onNativeDragDrop(e:NativeDragEvent):void
{
var img:DisplayObject; //the image (or first image) that was dragged
//IF IT'S A BITMAP (The easiest, so check it first)
if (e.clipboard.hasFormat(ClipboardFormats.BITMAP_FORMAT)) {
var bmd:BitmapData = BitmapData(e.clipboard.getData(ClipboardFormats.BITMAP_FORMAT));
img = new Bitmap(bmd);
addChild(img);
trace("It's a bitmap");
}
//IF IT'S FILE(S) that are dragged, try this next
if (!img && e.clipboard.hasFormat(ClipboardFormats.FILE_LIST_FORMAT)) {
var dropfiles:Array;
var file:File;
dropfiles = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
for each (file in dropfiles)
{
//isImagePath is defiend below
if (isImagePath(file.nativePath)) {
img = loadImage(file.nativePath); //load image function is defined below
break; //let's just load the first image
}
}
}
//IF IT's A URL that was dragged, try this next
if (!img && e.clipboard.hasFormat(ClipboardFormats.URL_FORMAT)) {
var url:String = String(e.clipboard.getData(ClipboardFormats.URL_FORMAT));
trace("It's a url: ", url);
if (isImagePath(url)) {
trace("it's a URL image path");
img = loadImage(url);
}
}
//IF IT's HTML that was dragged, try this next
if (!img && e.clipboard.hasFormat(ClipboardFormats.HTML_FORMAT)) {
var html:String = String(e.clipboard.getData(ClipboardFormats.HTML_FORMAT));
trace("its HTML: ", html);
//use regex to get all the <img> tags from the html
var imgs:Array = html.match(/(<img.*?>)/g);
//if one or more was found
if (imgs.length) {
trace("Image tag(s) found");
var imgPath:String;
for (var i:int = 0; i < imgs.length; i++) {
//use regex to get the src value from the img tag
imgPath = imgs[i].match(/src="(.*?)"/)[1];
img = loadImage(imgPath);
break; //let's just load the first image
}
}
}
//IF IT's raw text that dragged, try this next
if (!img && e.clipboard.hasFormat(ClipboardFormats.TEXT_FORMAT)) {
var txt:String = String(e.clipboard.getData(ClipboardFormats.TEXT_FORMAT));
trace("its text: ", txt);
if (isImagePath(txt)) {
img = loadImage(txt);
}
}
}
private var imgTypes:Vector.<String> = new < String > ["jpg", "gif", "png"];
private function isImagePath(path:String):Boolean {
if (path.lastIndexOf(".") > -1) {
var ext:String = path.substr(path.lastIndexOf(".")).toLowerCase();
return new RegExp(imgTypes.join("|")).test(ext);
}
return false;
}
private function loadImage(path):Loader {
var l:Loader = new Loader();
l.load(new URLRequest(path));
addChild(l);
return l;
}

How to handle tvOS MenuBarTemplate selection?

I have a basic MenuBarTemplate set up and displaying.
How do I react to a user's Menu selection and load an appropriate content template?
In the menuItem tag include a template attribute pointing to the template to load and a presentation attribute set to menuBarItemPresenter.
<menuItem template="${this.BASEURL}templates/Explore.xml.js"
presentation="menuBarItemPresenter">
<title>Explore</title>
</menuItem>
You can then use the menu bar's MenuBarDocument feature to associate a document to each menu bar item.
menuBarItemPresenter: function(xml, ele) {
var feature = ele.parentNode.getFeature("MenuBarDocument");
if (feature) {
var currentDoc = feature.getDocument(ele);
if (!currentDoc) {
feature.setDocument(xml, ele);
}
}
This assumes you're using a Presenter.js file like the one in Apple's "TVML Catalog" sample. The load function specified there is what calls the function specified in the menuItem's presentation attribute.
I suppose that TVML and TVJS is similar with HTML and Javascript. When we want to add some interaction into the user interface, we should addEventListener to DOM.
In Apple's "TVML Catalog", Presenter.js is a nice example, but it is abstract, and it could be used in different Present actions.
When I develop my app, I had wrote this demo for handling menuBar selection.
Module : loadTemplate.js
var loadTemplate = function ( baseURL , templateData ){
if( !baseURL ){
throw("baseURL is required");
}
this.BASEURL = baseURL;
this.tpData = templateData;
}
loadTemplate.prototype.loadResource = function ( resource , callback ){
var self = this;
evaluateScripts([resource], function(success) {
if (success) {
var resource = Template.call(self);
callback.call(self, resource);
} else {
var title = "Resource Loader Error",
description = `There was an error attempting to load the resource '${resource}'. \n\n Please try again later.`,
alert = createAlert(title, description);
Presenter.removeLoadingIndicator();
navigationDocument.presentModal(alert);
}
});
}
module.exports = loadTemplate;
Module nav.js ( use menuBarTemplate ) :
import loadTemplate from '../helpers/loadTemplates.js'
let nav = function ( baseURL ){
var loader = new loadTemplate(
baseURL ,
{
"explore" : "EXPLORE",
"subscribe" : "SUBSCRIBE",
"profile" : "PROFILE",
"settings" : "SETTINGS"
}//need to use i18n here
);
loader.loadResource(`${baseURL}templates/main.xml.js`, function (resource){
var parser = new DOMParser();
var navDoc = parser.parseFromString(resource, "application/xml");
navDoc.addEventListener("select" , function ( event ){
console.log( event );
var ele = event.target,
templateURL = ele.getAttribute("template");
if (templateURL) {
loader.loadResource(templateURL,
function(resource) {
if (resource) {
let newParser = new DOMParser();
var doc = newParser.parseFromString( resource , "application/xml" );
var menuBarItemPresenter = function ( xml , ele ){
var feature = ele.parentNode.getFeature("MenuBarDocument");
if( feature ){
var currentDoc = feature.getDocument( ele );
if( !currentDoc ){
feature.setDocument( xml , ele );
}
}
};
menuBarItemPresenter( doc , ele );
}
}
);
}
});
navigationDocument.pushDocument(navDoc);
});//load from teamplate.
}
module.exports = nav;
My code is not the best practice, but as you can see, you just need to addEventListener like you are writing a web application. Then you can handle menuBarTemplate selection easily, even after XHR loading.
Avoid too many callbacks, you should rebuild your code again and again. :-)

Refresh an ObservableCollection when Item's property changed

I have a observableCollection which is loaded with elements and displayed on the UI .At the same time the app is downloading one Icon for each element of the observable collection.I would like to display each Icon when its download finishes...
My code is working but i guess it's not the best pratice because I bind my collection to the Ui control 2 times...I am pretty convinced that it shouldn't be necessary ...I try to implement the InotifypropertyChanged on the element's Icon property but I still have to add those lines of code to display the Icons:
listDocsLibs = new ObservableCollection<BdeskDocLib>(listBoxGetDocsLibs);
llslistDocslibs.ItemsSource = listDocsLibs;
Below the function which dowload the Icons
List<BdeskDocLib> listBoxGetDocsLibs = new List<BdeskDocLib>();
ObservableCollection<BdeskDocLib> listDocsLibs = new ObservableCollection<BdeskDocLib>();
private async void LoadIconDocLibs()
{
foreach (var doclib in listBoxGetDocsLibs)
{
byte[] Icon = await ServerFunctions.GetDocLibsIcon(doclib);
if (Icon != null)
{
{
var ms = new MemoryStream(Icon);
BitmapImage photo = new BitmapImage();
photo.DecodePixelHeight = 64;
photo.DecodePixelWidth = 92;
photo.SetSource(ms);
doclib.Icon = photo;
}
}
else if (Icon == null)
{
doclib.Icon = new BitmapImage();
doclib.Icon.UriSource = new Uri("/Images/BDocs/ico_ext_inconnu.png", UriKind.Relative);
}
}
}
//IM PRETTY SURE The following code IS NOT NECESSARY BUT WHY MY UI IS NOT REFRESHING WITHOUT ?
listDocsLibs = new ObservableCollection<BdeskDocLib>(listBoxGetDocsLibs);
llslistDocslibs.ItemsSource = listDocsLibs;
}

Flex Mobile webView.loadURL() not working

I have a stageWebView in my flex Mobile app. I am trying to call a javascript function passing along some arguments.
protected function list1_changeHandler(event:IndexChangeEvent):void
{
var list:List = event.target as List;
var selectedString:String = list.selectedItem.label;
webView.loadURL("javascript:doIt("+selectedString+")");
}
Then I have my javascript code trying to use the selectedString I passed. It does not seem to be getting passed to the javascript. If I however pass a raw string like below it does work. Do I have quotes wrong or something else in the above code? Or can you not pass Objects from flexs?
protected function list1_changeHandler(event:IndexChangeEvent):void
{
var list:List = event.target as List;
var selectedString:String = list.selectedItem.label;
webView.loadURL("javascript:doIt('yeah')");
}
EDIT
I had the quotes wrong.Should have been:
webView.loadURL("javascript:doIt('"+selectedString+"')");
Try
protected function list1_changeHandler(event:IndexChangeEvent):void
{
var list:List = event.target as List;
var selectedString:String = list.selectedItem;
var url:String = "javascript:doIt('" + selectedString + "')";
webView.loadURL(url);
}

manually set check box selected in as3

I have a CheckBox in as3 that i need to set as checked with code. Do I dispatch an event? If so, how do I create an event, and give it a target (target is read only). Or is there another way to do it? thanks.
my_checkbox.selected = true;
And of course:
my_checkbox.selected = false;
To handle the box being checked/unchecked:
my_checkbox.addEventListener(MouseEvent.CLICK, _selected);
function _selected(e:MouseEvent):void
{
var bool:Boolean = e.target.selected;
if(bool)
{
trace("was checked");
// more code
}
else
{
trace("was unchecked");
// more code
}
}