2011-08-03 18 views
2

我有一個AS3應用程序,顯示用戶鳴叫。我試圖使用Twitter意圖鏈接來做彈出窗口,如https://twitter.com/intent/retweet?tweet_id=93035636432437248Twitter意向從Actionscript 3應用程序的彈出框

當你有一個簡單的href =「link」時,它將使用嵌入的http://platform.twitter.com/widgets.js來正確地格式化一個漂亮的彈出窗口。

在閃光燈中,我只能打開自己或空白,並且不會以與從href鏈接點擊時相同的方式觸發JavaScript。

我也嘗試使用ExternalInterface來調用js方法來使用document.location或window.open並且都不使用twitter js。

什麼是從閃光燈按鈕利用Twitter JavaScript的最佳方式,所以我們得到漂亮的清潔彈出窗口?

回答

2

查看Twitter Web Intents API Documentation,在頁面底部附近,您可以找到未混淆的源代碼鏈接,其中顯示了它們的API如何自動處理鏈接。

簡而言之,他們將一個Click事件處理程序附加到DOM,然後檢查點擊鏈接是否指向他們的Web Intents URL。當您從ActionScript打開一個窗口時,您正繞過DOM,因此代碼不會觸發。

現在通常您只希望使用ExternalInterface來調用Web Intents API公開的方法;然而,Twitter上的聰明聊天室爲了避免污染DOM而創建了他們的整個API--要得到愛情;)

所以,我個人會通過創建我自己的Twitter Web Intents API版本來解決這個問題,包括它在我的Flash應用程序所在的HTML頁面上;例如:

// Create a global object which we can attach methods to. 
var TwitterAPI = {}; 

// The methods themselves are created in a closure to avoid polluting the global 
// namespace with temporary variables. 
(function() { 
    // The base URL of the Twitter API. 
    TwitterAPI.baseURL = "https://twitter.com/intent/"; 

    // Opens a pop-up window and prompts the user to retweet the Tweet linked to 
    // the supplied tweet_id. 
    TwitterAPI.retweet = function(tweet_id) { 
     var url = TwitterAPI.baseURL + "retweet?tweet_id=" + tweet_id; 
     openWindow(url); 
    } 

    function openWindow(url) { 
     var windowOptions = "scrollbars=yes,resizable=yes,toolbar=no,location=yes"; 
     var width = 550; 
     var height = 420; 

     // Center the popup window. 
     var left = Math.round((screen.width/2) - (width/2)); 
     var top = 0;   
     if (screen.height > height) { 
       top = Math.round((screen.height/2) - (height/2)); 
      } 

     window.open(url, 'intent', windowOptions + ",width=" + width + 
      ",height=" + height + ",left=" + left + ",top=" + top); 

    } 
}()); 

然後,您可以從您的ActionScript項目通過ExternalInterface調用調用這個,因爲你曾建議:

ExternalInterface.call("TwitterAPI.retweet", "35782000644194304"); 
+0

我當然不希望我不得不這樣做,但我認爲你是對的。還通過電子郵件在Twitter上聯繫了聯繫人,看看他們是否對此有任何意見。非常感謝您的幫助! –

+0

我很抱歉跳到這個問題的線程。該例程運行良好,但我無法弄清楚的是如何在推文發佈後觸發Twitter的回調事件。通常,以下設置回調:twttr.events.bind('retweet',function(event){alert('retweet posted');});如何將它與您發佈的TwitterAPI一起使用?謝謝! – Victor