用Monkeygrease进行Server端的代码注入
版权声明:本文可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者署名及本版权声明。
最近看到了一些Monkeygrease的东西,将Greasemonkey的两个词颠倒,倒是不错的命名方法,可以利用一下Greasemonkey的知名度。
实际上Monkeygrease本来就号称是服务器端的Greasemonkey(在 Firefox 浏览器端执行用户Javascript代码来修改增强web页面),它们的目的都是相同的,不过实现方式不一样,Greasemonkey是基于用户的、客户端的,Monkeygrease是基于代理、服务器端的。
既然在服务器端采用这种方式,面向客户端的都是一样的效果,没有了Greasemonkey的基于用户的可定制性,为什么不直接在服务器端修改本身的站点代码呢?关于这个问题,它的网站上有回答:对于许多web应用来说,有些不容易改进、有些不方便定制、有些看不到源代码、有些很复杂需要专业知识才能定制。
Monkeygrease是一个简单的servlet filter,它将Javascript,CSS或其它的元素插入到html页面上,就像在html代码中作AOP注入。使用Monkeygrease可以增强web的某些方面:
- 改变web页面的观感
- 加入DHTML/AJAX效果
- 文本框加入WYSIWYG
- 整合进上下文相关的其它网站内容
- 增强页面的可用性
对于如何开始Monkeygrease应用,下面做个简单的说明:
1、下载Monkeygrease。
2、将monkeygrease.jar放入WEB-INF/lib目录下。
3、修改web.xml,加入下面的过滤器:
<filter>
<filter-name>Monkeygrease</filter-name>
<filter-class>
org.manalang.monkeygrease.MonkeygreaseFilter
</filter-class>
<init-param>
<param-name>confReloadCheckInterval</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>commentsOn</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>logLevel</param-name>
<param-value>2</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Monkeygrease</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
4、在monkeygrease.xml文件中定义规则。规则中url-pattern属性支持正则表达式;insert-at属性定义注入区域,如head-begin、head-end、body-begin、body-end;规则的节点内容通过CDATA表示,Javascript、CSS等代码就插入在这里,例如:
<![CDATA[
<script type="text/javascript" language="javascript">
<!--
window.onload = function() {
alert('Hello World!!!');
};
-->
</script>
]]>
下面是一个monkeygrease.xml的示例:
<?xml version="1.0" encoding="UTF-8"?>
<mg:monkeygrease xmlns:mg="http://monkeygrease.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://monkeygrease.org http://monkeygrease.org/monkeygrease.xsd ">
<rule enabled="true" name="Sample rule 1" url-pattern="/.*" insert-at="head-begin">
<![CDATA[
<!-- Whatever you insert here gets injected as the first item in the <head> section -->
]]>
</rule>
<rule enabled="true" name="Sample rule 2" url-pattern="/.*" insert-at="head-end">
<![CDATA[
<!-- Whatever you insert here gets injected as the last item in the <head> section -->
]]>
</rule>
<rule enabled="true" name="Sample rule 3" url-pattern="/.*" insert-at="body-begin">
<![CDATA[
<!-- Whatever you insert here gets injected as the first item in the <body> section -->
]]>
</rule>
<rule enabled="true" name="Sample rule 4" url-pattern="/.*" insert-at="body-end">
<![CDATA[
<!-- Whatever you insert here gets injected as the last item in the <body> section -->
]]>
</rule>
</mg:monkeygrease>
</xml>
5、将monkeygrease.xml放入WEB-INF目录下。
6、初始的客户端浏览的html代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
This is a test
</body>
</html>
在monkeygrease规则应用注入内容后的html代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<!– Whatever you insert here gets injected as the first item in the <head> section –>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<!– Whatever you insert here gets injected as the last item in the <head> section –>
</head>
<body>
<!– Whatever you insert here gets injected as the first item in the <body> section –>
This is a test
<!– Whatever you insert here gets injected as the last item in the <body> section –>
</body>
</html>
完了,就是这么简单。不过看起来实用价值不大。

我也认为这咚咚用处不大,web application自己就可以改代码。
思路还是不错的,有一定用处。