为什么 jQuery().remove() 和 unbind() 在 GreaseMonkey 中不起作用?
我试图在 GreaseMonkey 脚本中使用 jQuery().remove()
和 jQuery().unbind
方法(以及类似的函数),但它不起作用。不过,同样的调用在 fireBug 中也有效。我猜测这与 GM 沙箱有关,而且范围完全关闭。
我确实尝试使用 unsafeWindow.jQuery
甚至声明 window = unsafeWindow
但它没有帮助。
所有其他 jQuery 东西(例如 clone
append
)都工作得很好。
大家对此有什么想法或建议吗?
编辑:代码:
(function($) {
var changeURLs = function() {
var window = unsafeWindow;
$('.link-results li').each(function() {
var $a = $(this).find('a'), directUrl;
if ($a.hasClass('redirect')) return;
$a.unbind('click'); //!! Not working
if (/sidereel\.com/.test($a[1].href)) { // Megavideo like link
$.get($a[1].href, function(data) {
directUrl = $(data).find('.play-link')[0].innerHTML;
$a[1].href = $a[2].href = directUrl;
$a.each(function() {
unsafeWindow.console.log( $(this).remove() ); //!! Not working
});
});
} else { // Sponsered link
$a[2].href = $a[1].href;
}
$a.addClass('redirect');
});
},
$thickBox = $('.ui-dialog-content');
if ($thickBox.length) $thickBox.dialog('close')
changeURLs();
jQuery('.link-results-container').bind('DOMNodeInserted', changeURLs);
})(unsafeWindow.jQuery);
I'm trying to use the jQuery().remove()
and jQuery().unbind
methods (and similar functions) in a greaseMonkey script but it's not working. The same exact calls work in fireBug though. I'm guessing that it has something to do with GM sandboxing and the fact that the scope is completely off.
I did try to use unsafeWindow.jQuery
and even declared window = unsafeWindow
yet it didn't help.
All the other jQuery stuff (eg clone
append
)is working perfect.
Anyone have any thoughts or suggestions about this?
EDIT: code:
(function($) {
var changeURLs = function() {
var window = unsafeWindow;
$('.link-results li').each(function() {
var $a = $(this).find('a'), directUrl;
if ($a.hasClass('redirect')) return;
$a.unbind('click'); //!! Not working
if (/sidereel\.com/.test($a[1].href)) { // Megavideo like link
$.get($a[1].href, function(data) {
directUrl = $(data).find('.play-link')[0].innerHTML;
$a[1].href = $a[2].href = directUrl;
$a.each(function() {
unsafeWindow.console.log( $(this).remove() ); //!! Not working
});
});
} else { // Sponsered link
$a[2].href = $a[1].href;
}
$a.addClass('redirect');
});
},
$thickBox = $('.ui-dialog-content');
if ($thickBox.length) $thickBox.dialog('close')
changeURLs();
jQuery('.link-results-container').bind('DOMNodeInserted', changeURLs);
})(unsafeWindow.jQuery);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是另一个从 google 加载 JQuery(如果页面上尚未存在)的方法:
http://joanpiedra.com/jquery/greasemonkey/
Heres another one to load up the JQuery from google if it is not already present on the page:
http://joanpiedra.com/jquery/greasemonkey/