查看文章
 
在firefox的greasemonkey扩展中使用jquery
2009-01-22 22:31
// ==UserScript==
// @name          jQuery
// @namespace     http://www.joanpiedra.com/jquery/greasemonkey
// @description    Play nicely with jQuery and Greasemonkey
// @author        Joan Piedra
// @homepage      http://www.joanpiedra.com/jquery/greasemonkey
// @include       http://myxingchenbian.cn/*
// ==/UserScript==


// Add jQuery
var GM_JQ = document.createElement('script');
GM_JQ.src = 'http://cache.soso.com/post/js/jquery.js';
GM_JQ.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(GM_JQ);

// Check if jQuery's loaded
function GM_wait() {
    if(typeof unsafeWindow.jQuery == 'undefined') { window.setTimeout(GM_wait,100); }
    else { $ = unsafeWindow.jQuery; letsJQuery(); }
}
GM_wait();

// All your GM code must be inside this function
function letsJQuery() {
try{
    if (window.location.href.match(/myxingchenbian.cn/))
    {
        $("div.cBody").eq(0).hide();
        $("div.tonglan").hide();
        $("div.right").hide();
        $("div.newsView").children().not($("#content")).each(function(){$(this).hide();});
        $("div.footer").hide();
        $("div.content_up").hide();
        $("div.content_end").hide();
        $("div.cupage").hide();
    }
}catch(e){alert (e);}
}


使用require特性,将js文件下载到本地,以后不必再下载

// ==UserScript==
// @name           hello jquery
// @namespace      http://jquery.com
// @description    using jquery to remove ads
// @include        http://www.myxingchenbian.cn/*
// @require           http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js
// @require           http://ajax.googleapis.com/ajax/libs/jqueryui/1.5.3/jquery-ui.min.js
// ==/UserScript==

try{
    if (window.location.href.match(/myxingchenbian.cn/))
    {
        $(".tonglan, .footer, span:hidden, .right, .ad_style, .info_a3, .cBody:first, body script").remove(); //去掉广告内容
        $("#content").children().each(function(i){if (i>0 && i<5) $(this).remove();});
        $(".thisPlace:first").prependTo("body").after($("#content")); //挪动需要的内容到body中
        if (window.location.href.match(/\d+/)) $(".main").remove(); //只有具体的页才把main去掉
        $("p:contains('myxingchenbian.cn'), p:contains(',,')").remove(); //去除夹杂的广告
        $("p").each(function(){if ($(this).text().length < 2) $(this).remove();}); //去除空的段落
        $("body").attr('style', 'font-size:16px'); //重置字体大小
    }
    else if (window.location.href.match(/lqqm.net/))
    {
        $("table[width='100%']:first td[valign='top']").remove();
    }
}catch(e){alert (e);}


安装

将上述文件保存为以".user.js"结尾命名的js文件,然后使用firefox打开,这个时候greasemonkey就会提示你进行安装,在安装的过程中,就会下载对应的js文件并保存到本地。

类别:Javascript||添加到搜藏 |分享到i贴吧|浏览(874)|评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
     

   
帮助中心 | 空间客服 | 投诉中心 | 空间协议
©2012 Baidu