How to change CSS-classes with Greasemonkey?

两盒软妹~` 提交于 2019-12-11 13:54:42

问题


I've been trying to gin up a small Greasemonkey script, that takes the content of a class and changes it to another class.
Basically it would change:

<ul class='user_type_1'>

into:

<ul class='administrator'>

But, I'm completely green to javascript & Greasemonkey, and all the research I did just left me even more confused.

Ideally I would like someone to explain in detail HOW I achieve this, instead of just handing over a script that works (though currently even that would be a help).


回答1:


This is super-easy with jQuery (a javascript utility/library). jQuery provides the functions addClass() and removeClass(), to make this a snap.

Here is a complete, Firefox Greasemonkey, script that changes that class:

// ==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");


来源:https://stackoverflow.com/questions/13102195/how-to-change-css-classes-with-greasemonkey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!