将html/javascript代码注入Flex AS3 html类
我需要将html/javascript代码注入到每个加载的html中 程序使用该类 我反汇编了原来的Flex3文件,找到了 重要?我修改/测试的功能:将html/javascript代码注入Flex AS3 html类,html,actionscript-3,apache-flex,air,Html,Actionscript 3,Apache Flex,Air,我需要将html/javascript代码注入到每个加载的html中 程序使用该类 我反汇编了原来的Flex3文件,找到了 重要?我修改/测试的功能: public function set htmlText(value:String) : void { _htmlText = value; htmlTextChanged = true; _location = null; locationChanged = false; invalidateProper
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"));
}
我成功地更改了set location的代码,并管理了只加载的代码
但是,当使用location
设置位置时,似乎不会调用htmlText
setter
这就是我尝试过的:
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"));
}
公共函数集htmlText(值:字符串):无效
{
_htmlText=“测试”+数值;
htmlTextChanged=true;
_位置=空;
位置更改=错误;
无效属性();
无效化();
invalidateDisplayList();
dispatchEvent(新事件(“htmlTextChanged”);
}
我需要查看flash.net.HTMLloader
,但找不到该文件
我希望有人能帮助我,告诉我HTML代码从何处加载,在何处呈现,这样我就可以在之前修改它
提前感谢。我不知道您想向html内容中注入什么,但我认为您可以在
html
组件上的完整事件被触发时,使用以下对象:
加载到HTML控件中的内容的全局JavaScript对象
因此,您可以像在任何JavaSript代码中一样使用它(窗口
对象)
// 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);
及
受保护功能完成(e:事件):无效
{
var html_loader:HTMLLoader=html_content.HTMLLoader;
var document=html_loader.window.document;
var_head=document.getElementsByTagName('head')[0];
//创建一个新的css类
var#u class='.akmozo{背景:#ff9900;颜色:白色;填充:2px;}';
var_style=document.createElement('style');
_appendChild(document.createTextNode(_类));
//创建一个新的js函数
var_js='函数click_me(){alert(“你好吗?”);}”;
var_script=document.createElement('script');
_appendChild(document.createTextNode(_js));
_头、子(_型);
_head.appendChild(_脚本);
//将页面顶部栏的背景颜色更改为绿色
if(document.querySelector('.topbar wrapper'))
{
document.querySelector('.topbar wrapper').style.backgroundColor='green';
}
//编辑问题的标题,
//应用新的css类并调用js函数
if(document.querySelector('.question-hyperlink'))
{
document.querySelector(“.question hyperlink”).innerHTML+=“[由AKMOZO编辑]”;
}
//将SO徽标更改为我的头像
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';
}
//在你的评论中做一些改变
if(document.querySelector('#comment-52605069'))
{
document.querySelector('#comment-52605069.comment copy').style.color='green';
document.querySelector(“#comment-52605069.comment copy”).style.fontwweight=700;
document.querySelector(“#comment-52605069.comment copy”).style.fontSize='24px';
document.querySelector('#comment-52605069.comment user')。innerHTML='akmozo';
}
}
此代码将为您提供如下内容:
当然,我只是想给你一个例子,说明你能做些什么,你必须改进和调整这段代码以适应你的需要
希望这能有所帮助。我不知道你在问什么。什么是“HTML代码”?看起来你的意思和“测试”不同,但一点也不清楚。在更高的层次上,通过修改
htmlText
,您想要实现什么?我实际上不想更改htmlText。我想修改由特定位置加载的html。例如:我导航到,让我们称之为钩子,在google站点html之前添加hello not google,这样在google站点的顶部hello not google是。你太棒了!非常感谢你!
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>';
}
}