I need to Inject html/javascript-code into every HTML that gets loaded.
我需要将html / javascript代码注入每个加载的HTML中。
The program uses the HTML
class.
该程序使用HTML类。
I disassembled the original Flex3 files and found this.
我拆解了原始的Flex3文件,发现了这个。
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.
我成功更改了设置位置的代码并管理了只有http://www.example.com/加载。
However, the htmlText
setter seems not to get called when the location is set using location
.
但是,当使用location设置位置时,似乎不会调用htmlText setter。
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.
我需要看看flash.net.HTMLloader,但我无法找到该文件。
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.
我希望有人可以帮助我并告诉我从该位置加载HTML代码的位置以及它在何处被渲染,以便我可以在之前修改它。
Thanks in advance.
提前致谢。
1 个解决方案
#1
1
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 :
我不知道你想要为你的html内容注入什么,但我认为你可以使用以下窗口对象触发HTML组件的HTMLLoader上的完整事件时执行此操作:
The global JavaScript object for the content loaded into the HTML control..
加载到HTML控件中的内容的全局JavaScript对象..
So you can use it ( the window
object ) as in any JavaSript code.
因此,您可以像在任何JavaSript代码中一样使用它(窗口对象)。
// for the example, I took the page of your current question
var url:String = 'http://*.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.
希望能有所帮助。
#1
1
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 :
我不知道你想要为你的html内容注入什么,但我认为你可以使用以下窗口对象触发HTML组件的HTMLLoader上的完整事件时执行此操作:
The global JavaScript object for the content loaded into the HTML control..
加载到HTML控件中的内容的全局JavaScript对象..
So you can use it ( the window
object ) as in any JavaSript code.
因此,您可以像在任何JavaSript代码中一样使用它(窗口对象)。
// for the example, I took the page of your current question
var url:String = 'http://*.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.
希望能有所帮助。