2010-06-26 36 views
1

我完全不熟悉JavaScript和Greasemonkey,所以请随时纠正我,如果我这样做效率低下或不正确。如何将动态表单附加到表的末尾?

在我发布的论坛中,单击“回复”按钮将弹出一个只有文本形式的新窗口,以便发布。我想创建一个greasemonkey脚本,将回复表单的脚本添加到实际线程中页。

因此,程序通过存储讨论的表格,并将childNode追加到表格的末尾。我希望childNode是在回复页面中创建的表单。

这里是我的脚本的骨架:

// ==UserScript== 
// @name QuickEeply 
// @namespace http://userscripts.org/users/181447 
// @description Adds "QuickReply" forms to TCC discussion posts 
// @include  * 
// ==/UserScript== 


var tables = document.getElementsByTagName("td"); 


for (var i = 0; i < tables.length; i++) { 

if (tables[i].className == "content") 
{ var editTable = tables[i]; 
} 

} 

editTable.appendChild = '' 

下面是我复制并从“答复页”粘贴脚本

<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> 

所以,我怎么能去创建一个脚本找到当前页面的thread_id,并为线程实际所在页面上的每个页面创建一个回复框。

编辑:这是源代码 -

http://pastebin.com/2UaUVGJA(主要讨论页)
http://pastebin.com/hAx2SPUu(回复页)

编辑2:

I've used Brock's template, and it's not working. What do I need to do to correct it? 

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

很难说没有看到您正在查看的HTML。帖子ID和帖子ID变量是否可用? – desau 2010-06-26 00:37:56

+0

我编辑过它以包含源代码。 – Parseltongue 2010-06-26 00:49:03

回答

0

转到this answer并获取Greasemonkey模板。

它向您展示了如何在特定节点插入HTML,比如你的表单。

用您的表单HTML替换该样本的表格HTML。那么你的TargetNode将会像$("a[href*='event=reply/post']") - 基于参考页面。请注意,这应该在每个帖子条目处插入表单。

警告:表单不一定会在您想要的位置,而且我们还没有将表单同步到每个帖子。

尽你所能,但在第一部分工作和你的接受率提高之后,应该在新的问题中询问下一个阶段。 ;-)

+0

谢谢布鲁克,我选择你作为我的永久导师。 – Parseltongue 2010-06-26 17:15:04

+0

看看我在这个主题上做出的新编辑 - 我遵循了你的指示,但脚本没有添加表单。我从来没有用过Jquery,所以我不太明白。 – Parseltongue 2010-06-26 19:04:16

0

回复:

编辑2: 我用Brock的模板,它不工作。我需要做些什么来纠正它?

你需要非常小心多行字符串和字符串用引号或撇号。 <form>字符串中出现javascript语法错误。

我已经为您清理它(并将语法更改为我的首选样式)...

请注意StackOverflow的语法高亮显示如何保持字符串全部为红色?大多数编程编辑也会给出这种线索。

$(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>                       \ 
    ' 
); 

如果还有其他问题,请打开一个新问题,以便这个问题不会太难遵循。

+0

http://stackoverflow.com/questions/3125674/add-a-dynamic-form-to-specific-pages-using-greasemonkey-and-jquery 我已经打开了新的问题。谢谢。 另一方面,您使用哪种编辑器的JavaScript? – Parseltongue 2010-06-26 22:17:41

+0

我主要使用TextPad(http://www.textpad.com/products/textpad/index.html)和SlickEdit(http://www.slickedit.com/)。但是我听说Notepad ++(http://notepad-plus-plus.org/)很好,而且它是免费的。 – 2010-06-27 07:14:38

相关问题