Javascript 使用Greasemonkey和JQuery将动态表单添加到特定页面
在特定论坛中,单击“回复”按钮将生成一个新窗口,其中包含一个文本表单,用于键入回复。我想实现一个脚本,在主页中创建特定的文本表单(而不是生成一个新窗口)。我该怎么做呢 以下是我要在其上实现脚本的页面的源代码: (主讨论页) (回复页) 这里是一个尝试过的脚本(注意,我仍然需要一些方法来提取适当的post_id值,并基于该id创建表单),它根本不起作用Javascript 使用Greasemonkey和JQuery将动态表单添加到特定页面,javascript,jquery,greasemonkey,Javascript,Jquery,Greasemonkey,在特定论坛中,单击“回复”按钮将生成一个新窗口,其中包含一个文本表单,用于键入回复。我想实现一个脚本,在主页中创建特定的文本表单(而不是生成一个新窗口)。我该怎么做呢 以下是我要在其上实现脚本的页面的源代码: (主讨论页) (回复页) 这里是一个尝试过的脚本(注意,我仍然需要一些方法来提取适当的post_id值,并基于该id创建表单),它根本不起作用 // ==UserScript== // @name Quick_ReplyTest // @namespace h
// ==UserScript==
// @name Quick_ReplyTest
// @namespace http://userscripts.org/users/181447
// @description Inserts QuickReply
// @include *
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// ==/UserScript==
/* Optional:
window.addEventListener ("load", Greasemonkey_main, false);
*/
$(document).ready (Greasemonkey_main);
function Greasemonkey_main ()
{
/*--- Get the first node inside the id="main" span (Google.com)
If that's not there, then get the first node of the html body.
*/
var TargetNode = $("a[href*='event=reply/post']");
if (!TargetNode)
TargetNode = $("body *:first");
$(TargetNode).after
(
'<form method="POST" action="http://dl.tccd.edu/index.php/classforums/posts/event=saveReply"> \
<input type="hidden" name="subject" size="45" id="txt_subject" maxlength="200" value=""> \
<br> Message:<br> \
<textarea rows="20" style="width:70%;" name="message" id="message"></textarea> \
<br> <br> \
<input type="submit" id="submit_post" value="Post Reply"> \
<input type="hidden" name="post_id" value="1010815"> \
<input type="hidden" name="thread_id" value="1010815"> \
</form> \
'
);
}
/==UserScript==
//@name快速回复测试
//@名称空间http://userscripts.org/users/181447
//@description插入快速回复
//@包括*
//@需要http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
//==/UserScript==
/*可选:
window.addEventListener(“加载”,Greasemonkey_main,false);
*/
$(文件).ready(Greasemonkey_main);
功能Greasemonkey\u main()
{
/*---获取id=“main”span(Google.com)中的第一个节点
如果没有,则获取html正文的第一个节点。
*/
var TargetNode=$(“a[href*='event=reply/post']);
如果(!TargetNode)
TargetNode=$(“body*:first”);
$(TargetNode)。之后
(
' \
\
信息:
\
\
\
\
\
\
\
'
);
}
好的,下面是修改后的脚本,使表单生效。它现在应该可以正常工作了,但显然,如果没有登录,我无法完全测试它
我试图解释评论上发生了什么,一个方便的jQuery参考位于:
/*注意:不要使用:
//@包括*
这会减慢速度,并可能导致各种有趣的副作用。
*/
//==用户脚本==
//@name快速回复测试
//@名称空间http://userscripts.org/users/181447
//@description插入快速回复
//@include http://*.tccd.edu/*
//@include https://*.tccd.edu/*
//@需要http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
//==/UserScript==
$(文件).ready(Greasemonkey_main);
功能Greasemonkey\u main()
{
/*---使用jQuery获取指向回复表单的链接(锚定标记)。
链接的形式如下:
*/
var zTargetNodes=$(“a[href*='event=reply/post']”;/--href包含“event=reply/post”。
/*---现在,对于每个链接,提取post_id并插入带有post_id的回复表单。
使用jQuery each()函数。
*/
每个(函数(iPostNum){AddReplyForm($(this),iPostNum);});
}
函数AddReplyForm(zJnode,iPostNum)
{
/*---使用jQuery attr()函数和javascript从链接中提取post_id
match()函数。
*/
var sHref=zJnode.attr('href');
var sPostID=(sHref+'&').match(/post_id=([^\&\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\;
/*---构建表单HTML字符串。使用sPostID作为post\u id和thread\u id。
(或者我们可以将表单插入到文档中,然后更改id值。)
*/
var sNewHTML='\n'
+“\n”
+“
消息:
\n”
+“\n”
+“
\n”
+“\n”
+“\n”
+“\n”
+ ''
;
/*---插入回复表单。
*/
zJnode.after(sNewHTML);
}
非常有教育意义!感谢您抽出时间始终回答我的问题,并对代码进行评论。但是,脚本不起作用。我是否可以调试它以发现问题?我是否必须导入Jquery数据库,或者该“require”语句是否为我执行此操作?是的,“require”语句处理Jquery的导入。它只在安装脚本时执行一次。因此,如果您认为该脚本已损坏或进行了重大更改,请使用Greasemonkey“管理用户脚本”面板卸载并重新安装该脚本。它是如何工作的?它在本地页面上对我很有用——当然,除了我不能发布实际的回复。至于调试。。。是的,不太难。安装Firebug扩展()。然后打开Firebug的javascript控制台,打开所有错误跟踪,然后重新加载页面。请注意由脚本引起的任何错误(唉,大多数页面都是自己引起大量错误的)。更多Firebug使用信息:错误如下:$未定义$(文档).ready(Greasemonkey\u main);第16行是的,脚本安装不知怎的出错了。确保“require”行与我指定的完全一致。卸载脚本并重新安装。同时卸载(a)相同名称或(b)在同一页面上运行的任何其他脚本。
/* NOTE: Do not use:
// @include *
This slows things down and could cause all sorts of interesting side-effects.
*/
// ==UserScript==
// @name Quick_ReplyTest
// @namespace http://userscripts.org/users/181447
// @description Inserts QuickReply
// @include http://*.tccd.edu/*
// @include https://*.tccd.edu/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// ==/UserScript==
$(document).ready (Greasemonkey_main);
function Greasemonkey_main ()
{
/*--- Use jQuery to get links (anchor tags) that point to the reply form.
The links are of the form:
<a href="http://dl.tccd.edu/index.php/classforums/posts/event=reply/post_id=1013402">Reply</a>
*/
var zTargetNodes = $("a[href*='event=reply/post']"); //-- href contains "event=reply/post".
/*--- Now, for each link, extract the post_id and insert the reply form with the post_id filled in.
Uses the jQuery each() function.
*/
zTargetNodes. each (function (iPostNum) {AddReplyForm ($(this), iPostNum);} );
}
function AddReplyForm (zJnode, iPostNum)
{
/*--- Extract the post_id from the link, using the jQuery attr() function and the javascript
match() function.
*/
var sHref = zJnode.attr ('href');
var sPostID = (sHref + '&').match (/post_id=([^\&\#]+)[\&\#]/) [1];
/*--- Build up the form HTML-string. Using sPostID for post_id and thread_id.
(Or we could just insert the form into the doc, and change the id values after.)
*/
var sNewHTML = '<form method="POST" action="http://dl.tccd.edu/index.php/classforums/posts/event=saveReply"> \n'
+ '<input type="hidden" name="subject" size="45" id="txt_subject" maxlength="200" value=""> \n'
+ '<br> Message:<br> \n'
+ '<textarea rows="20" style="width:70%;" name="message" id="message"></textarea> \n'
+ '<br> <br> \n'
+ '<input type="submit" id="submit_post" value="Post Reply"> \n'
+ '<input type="hidden" name="post_id" value="' + sPostID + '"> \n'
+ '<input type="hidden" name="thread_id" value="' + sPostID + '"> \n'
+ '</form>'
;
/*--- Inject the reply form.
*/
zJnode.after (sNewHTML);
}