Javascript 坦佩尔猴子';s GM_xmlhttpRequest未实施';上下文';财产?
我已经为Greasemonkey(Firefox)编写了一个用户脚本,正在测试它与Chrome的Tampermonkey的兼容性,并且在开发人员控制台中出现错误:Javascript 坦佩尔猴子';s GM_xmlhttpRequest未实施';上下文';财产?,javascript,google-chrome,tampermonkey,gm-xmlhttprequest,Javascript,Google Chrome,Tampermonkey,Gm Xmlhttprequest,我已经为Greasemonkey(Firefox)编写了一个用户脚本,正在测试它与Chrome的Tampermonkey的兼容性,并且在开发人员控制台中出现错误: Uncaught TypeError: Cannot read property 'profile_url' of undefined Uncaught TypeError: Cannot read property 'encoded_name' of undefined 这些错误似乎引用了GM_xmlhttpRequest的onr
Uncaught TypeError: Cannot read property 'profile_url' of undefined
Uncaught TypeError: Cannot read property 'encoded_name' of undefined
这些错误似乎引用了GM_xmlhttpRequest
的onreadystatechanged
回调,其调用方式如下:
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);
}
});
}
var flairs=document.queryselectoral('span.flair');
var steam|u re=/(?:(?:https?:\/\/)?www\)(?:steam | pc)(?:community\.com\/(?:(id | profiles)\/?[\s\-\]*id)?[\/:\s\\\\]*(.{2,}:[\/\-\-\[(?:\/(?:\/)(?:ghost)\/(?:ghost | ghost |技术| mm |硕士+)[$)/i]
函数get_text(e){返回e.innerText | | e.textContent;}
函数集\文本(e,t){
如果(如innerText)
e、 innerText=t;
其他的
e、 textContent=t;
}
var parser=新的DOMParser();
对于(变量i=0;i
函数本身调用得很好,回调也被调用,但一旦我尝试访问它内部的上下文
变量,它就没有定义
这一切在Firefox中都能正常工作。它所做的是迭代具有“flair”类的span
元素,并使用正则表达式检查它们是否包含Steam用户名,如果包含,则将其作为指向其Steam社区页面的链接。(完整源代码在上)。脚本在上运行
我已经使用函数外部定义的数组来存储数据,而不是使用传递给xmlhttpRequest的上下文属性进行了测试,但是我得到了完全相同的错误。更新:
Tampermonkey现在报告说,从版本3.8.4116(目前为beta版)起,此功能已修复。请参阅:
旧的/通用的解决方法:
.我怀疑它是否已在Tampermonkey中实现;请参阅 同时,解决这类问题的行之有效的方法是使用 将您的
GM\u 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
);