2013-08-26 80 views
2

我已經寫了的Greasemonkey(火狐)一UserScript和我在開發者控制檯測試它與Chrome的Tampermonkey兼容性,並且得到錯誤:Tampermonkey的GM_xmlhttpRequest沒有實現'context'屬性?

Uncaught TypeError: Cannot read property 'profile_url' of undefined 
Uncaught TypeError: Cannot read property 'encoded_name' of undefined 

的錯誤似乎引用onreadystatechanged回調GM_xmlhttpRequest這是這樣調用:

var flairs = document.querySelectorAll('span.flair'); 
var steam_re = /(?:(?:https?:\/\/)?www\.)?(?:steam|pc)(?:community\.com\/?(?:(id|profiles)\/?)?|[\s\-_]*id)?[\/:\s\|]*(.{2,}?)(?:[\/|:\-\[(] ?(?:\/?(?:ghost|enforcer|tech|mm|master))+[\[)]?)?$/i 

function get_text(e) { return e.innerText || e.textContent; } 

function set_text(e, t) { 
    if (e.innerText) 
     e.innerText = t; 
    else 
     e.textContent = t; 
} 

var parser = new DOMParser(); 

for (var i = 0; i < flairs.length; i++) { 
    var text = get_text(flairs[i]); 
    var match = steam_re.exec(text); 
    if (match == null || match.length < 3) 
     continue; 
    var type = match[1] || 'id'; 
    var name = encodeURIComponent(match[2]); 
    var url = 'http://steamcommunity.com/' + type + '/' + name; 
    var xml_url = url + '?xml=1'; 
    GM_xmlhttpRequest({ 
     method: 'GET', 
     url: xml_url, // Link to a steam profile with ?xml=1 added 
     accept: 'text/xml', 
     context: { 
      flair_index: i, 
      flair_text: text, // textContent of span element 
      encoded_name: name, 
      profile_url: url, // Link to steam profile 
      query_url: xml_url 
     }, 
     onreadystatechange: function(response) { 
      if (response.readyState != 4) 
       return; 
      // Attempt to fall back to alternate forms of context, 
      // none of which works. response.context works on Firefox/Greasemonkey. 
      var context = response.context || this.context || context; 
      var doc = parser.parseFromString(response.responseText, 'text/xml'); 
      var validProfile = doc.documentElement.nodeName == 'profile'; 
      var a = document.createElement('a'); 
      a.href = validProfile ? 
       context.profile_url : // TypeError here, context is undefined 
       ('http://steamcommunity.com/actions/SearchFriends?K=' + context.encoded_name); 
      a.className += (validProfile ? 'steam-profile-link' : 'steam-profile-search-link'); 
      var a_text = document.createTextNode(context.flair_text); 
      a.appendChild(a_text); 
      set_text(flairs[context.flair_index], ''); 
      flairs[context.flair_index].appendChild(a); 
     } 
    }); 
} 

此功能也稱爲細,並調用回調函數,但一旦我嘗試訪問它裏面的context變種,它是不確定的。

這一切都按預期在Firefox中運行。它所做的是遍歷具有「flair」類的span元素,並使用正則表達式檢查它們是否包含Steam用戶名,如果是,則使其成爲SteamCommunity頁面的鏈接。 (全文來源於github)。該腳本在/r/PaydayTheHeistOnline上運行。

我已經測試了使用在函數外部定義的數組來存儲數據,而不是使用傳遞給xmlhttpRequest的上下文屬性,但我得到了完全相同的錯誤。

回答

2

更新:
Tampermonkey現在報告,此功能(目前處於測試階段)固定爲3.8.4116版本。請參閱:


年長/通用解決方法:
The context property is a relatively new feature of GM_xmlhttpRequest(), in Firefox。我懷疑它是否已經在Tampermonkey中實現了;見Tampermonkey's docs for GM_xmlhttpRequest()

同時,這種事情的嘗試和真正的方法是使用closure

GM_xmlhttpRequest()呼叫改變的東西

(function (flair_index, flair_text, encoded_name, profile_url, query_url) { 
    GM_xmlhttpRequest ({ 
     method: 'GET', 
     url: xml_url, // Link to a steam profile with ?xml=1 added 
     accept: 'text/xml', 
     onreadystatechange: function (response) { 
      if (response.readyState != 4) 
       return; 

      var doc = parser.parseFromString (response.responseText, 'text/xml'); 
      var validProfile = doc.documentElement.nodeName == 'profile'; 
      var a = document.createElement ('a'); 
      a.href = validProfile ? 
       profile_url : 
       ('http://steamcommunity.com/actions/SearchFriends?K=' + encoded_name); 
      a.className += (validProfile ? 'steam-profile-link' : 'steam-profile-search-link'); 
      var a_text = document.createTextNode (flair_text); 
      a.appendChild (a_text); 
      set_text (flairs[flair_index], ''); 
      flairs[flair_index].appendChild (a); 
     } 
    }); 
}) (
    i, 
    text, // textContent of span element 
    name, 
    url, // Link to steam profile 
    xml_url 
); 
+0

這工作完全,謝謝!我將不得不閱讀關閉和範圍。 – Sharparam

+0

不客氣。樂意效勞。 –