灵感点亮生活

导航

Blog统计

公告

文章

收藏

相册

Developer

Other

好友的Blog

我的开源项目

存档


正在读取评论……

今天偶然在Greasemonkey的maillist里发现了个有趣的东西Monkeygrease ,一个服务器端的Greasemonkey,可以在服务器端执行js代码,可以对现有的html中插入你想要的html或js代码,有些像在html中作aop操作。


只需要简单简单的配置Monkeygrease就可以工作了:

加入一个Filter
<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>

配置插入规则:
<?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-begin">
<![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>

你可以在

<![CDATA[
中放入js或html代码,比如插入如下js代码

<![CDATA[
<script type="text/javascript" language="javascript">
<!--
window.onload = function() {
alert('Hello World!!!');
};
-->
</script>
]]>
接下来简单说一下规则:
insert-at="head-begin"
在head前插入如下文本
<!-- Whatever you insert here gets injected as the first item in the <head> section -->

其他配置以此类推


原始的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
<!-- This page has been Monkeygrease'd (Config file last loaded on 2005-11-9 ÏÂÎç06ʱ17·Ö03Ãë) -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<!-- Monkeygrease Rule Begin: Sample rule 2 head-begin -->
<title>This page has been monkeygrease'd</title>
<!-- Monkeygrease Rule End -->
<!-- Monkeygrease Rule Begin: Sample rule 1 head-begin -->
<script type="text/javascript" src="/dojo/dojo.js">&nbsp;</script>
<link href="css.css" rel="stylesheet" type="text/css" />
<!-- Monkeygrease Rule End -->
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<!-- Monkeygrease Rule Begin: Sample rule 3 body-begin -->
<div style="background:green">beginning of body</div>
<!-- Monkeygrease Rule End -->
This is a test
<!-- Monkeygrease Rule Begin: Sample rule 4 body-end -->
<div style="background:red">End of body</div>
<!-- Monkeygrease Rule End -->
</body>

</html>





Trackback: http://tb.donews.net/TrackBack.aspx?PostId=620560


[点击此处收藏本文]  发表于2005年11月09日 6:30 PM




正在读取评论……
大名
网址
验证码
评论