2015-09-02 20 views
-1

我需要將html/javascript-code注入到加載的每個HTML中。將HTML/JavaScript代碼注入到Flex AS3 HTML類中

該程序使用HTML類。

我拆開了原來的Flex3文件,發現this

重要嗎?功能我修改/測試:

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")); 
} 

我成功地改變組位置的代碼和管理僅http://www.example.com/被加載。

但是,當使用location設置位置時,似乎不會調用htmlText設置程序。

這是我曾嘗試:

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")); 
} 

我必須flash.net.HTMLloader看看,但我無法找到該文件。

我希望有人能幫助我,並告訴我HTML代碼從位置加載的位置以及它被呈現的位置,因此我可以在之前對其進行修改。

在此先感謝。

+0

我不確定你在問什麼。什麼是「HTML代碼」?這似乎是你的意思是不同於「

測試

」,但它根本不明確。在更高層次上,你想通過修改'htmlText'完成什麼? – Brian

+0

我其實不想改變** htmlText **。我想修改由特定位置加載的** html **。例如:我導航到http:// google。德和,讓我們叫它掛鉤,增加

你好不是谷歌

谷歌網站的HTML之前,所以在谷歌網站頂部**你好不是谷歌**是。 –

回答

1

我不知道你怎麼想注入到你的HTML內容,但我認爲你可以做到這一點,當你的HTML組件的HTMLLoader完整的事件被激發,使用window對象,它是:

的全局JavaScript對象加載到HTML控件中的內容..

所以,你可以用它(window對象)中的任何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); 

而且

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>'; 
    }   
} 

此代碼會給你這樣的事情:

當然我想只是給你的你可以做什麼的例子,你必須根據您的需求改進和調整此代碼。

希望能有所幫助。

+0

你太棒了!非常感謝你! –