如何使用Greasemonkey更改CSS类?

时间:2012-10-27 16:56:42

标签: javascript css class greasemonkey

我一直在尝试打开一个小的Greasemonkey脚本,它接受一个类的内容并将其更改为另一个类。
基本上它会改变:

<ul class='user_type_1'>

成:

<ul class='administrator'>

但是,我对javascript&amp; Greasemonkey,以及我所做的所有研究让我更加困惑。

理想情况下,我希望有人详细解释我如何实现这一目标,而不仅仅是交出一个有效的脚本(虽然目前甚至可以提供帮助)。

1 个答案:

答案 0 :(得分:3)

使用jQuery(javascript实用程序/库)非常简单。 jQuery提供了函数addClass()removeClass(),以便快速实现。

以下是更改该类的完整的Firefox Greasemonkey脚本

// ==UserScript==
// @name     _Change one class into another.
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @grant    GM_addStyle   
// ==/UserScript==
/*- The @grant directive is needed to work around a design change introduced
    in GM 1.0.   It restores the sandbox.
*/
//-- Get everything that has the class "user_type_1".
var userTypeNodes = $(".user_type_1");

userTypeNodes.removeClass ("user_type_1");
userTypeNodes.addClass ("administrator");