我正在通过tampermonkey从用户脚本插入qtips
,tampermonkey是在加载页面后加载的。我似乎无法从工具提示(qtip
)内部获得自定义Javascript函数。我一直得到引用错误,说我想调用的函数没有定义。有人可以解释一下原因吗? (最新的Chrome测试版错误消息)
Uncaught ReferenceError: redirectAH is not defined index.php?cmd=news:1
onclick
代码:
//headers that work fine
$( function() {
GM_addStyle(GM_getResourceText('qtipCSS'));
(function redirectAH(playerID){
window.location = 'http://domain.com/index.php?data1=cmd&data2=-5&pid=' + playerID;
//correct format for above: http://domain.com/index.php?data1=cmd&data2=-5&pid=123456 or any sort of other PID
return;
})();
var makeConfig = function(jqObject){
var playerID = something; //code to define it correctly
var arrLen = jqObject.attr('href').split('player_id=').length || -1;
var playerID = 0;
if (arrLen > 0){
playerID = jqObject.attr('href').split('player_id=')[arrLen -1];
}
var argumentAH = '<span onclick="javascript:redirectAH(playerID)"> [AH] </span>';
var finalText = '<div style = "text-align: center;">' + 'PID: ' + playerID + '<br/>'+ argumentAH + '</div>';
var qtipContent = {
overwrite: false,
content: { text: finalText, attr: 'error' },
position: { my: 'bottom left', at: 'top left', adjust: { method: 'shift none' } },
show: { delay: 50 },
hide: { fixed: true, delay: 90 },
style: { classes: 'qtip-tipsy qtip-shadow' }
};
return qtipContent;
};
$('[href *= "index.php"]').each(function(){
if (this.textContent.trim()){
var inputHTML = $(this);
var config = makeConfig(inputHTML);
$(this).qtip(config);
}
});
});
我认为包装redirectAH()
应该将函数添加到全局范围,但我仍然遇到问题,所以我想这不会发生。由于这个链接必须随着每个qtip
而改变,直到某人悬停在玩家的名字上才会被渲染,我不知道该如何做到这一点(我不能分配ID并在以后再做)当该函数运行时,qtip可能尚未加载。此外,播放器数据将丢失,我不得不再次翻录它。也许它与tampermonkey沙箱有关,虽然我不知道如何解决它。
我也遇到以下问题:
Uncaught TypeError: Cannot call method 'hasClass' of null libs.js?ver=00120:14353
inactiveMethod libs.js?ver=00120:14353
(anonymous function) libs.js?ver=00120:14392
jQuery.event.special.(anonymous function).handle libs.js?ver=00120:3535
jQuery.event.dispatch libs.js?ver=00120:3256
elemData.handle.eventHandle
编辑:调用$('[href *= "index.php"]')
<a href="index.php?player_id=######" class="someclass" style="">playerName</a>
我已经使用我用来剥离playerID的代码更新了主代码。