我想使用Greasemonkey将cgit提交消息中显示的 Redmine 问题编号链接到他们的问题或项目。
cgit Commit消息的HTML源代码是这样的。
<a href='/editingmodule/commit/?id=49e4a33e0f8b306ded5'>refs #459 - create separate directory and repo for editingModule, lots of dif...</a>
我想要做的是让#459成为 Redmine 问题的单独href,但保留两边的cgit链接不变。所以上面的URL转换成这样的东西:
<a href='/editingmodule/commit/?id=49e4a33e0f8b306ded5'>refs</a>
<a href='http://redmine.project.com/redmine/issues/459'>#459</a>
<a href='/editingmodule/commit/?id=49e4a33e0f8b306ded5'> - create separate directory and repo for editingModule, lots of dif...</a>
可能很难阅读,但上面链接中的#459链接到 Redmine 项目。
为了清楚起见,还可以将指向 Redmine 问题的链接附加到cgit链接。
答案 0 :(得分:3)
使用jQuery查找链接,使用正则表达式提取关键文本部分。
这是完整的工作脚本;有关详细信息,请参阅内联注释:
// ==UserScript==
// @name _Split Commit links and add Redmine links
// @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.
*/
//-- Fing matching links
$("a[href*='/editingmodule/commit/']").each ( function () {
/*-- Parse each link for the expected format:
refs #{number} {description text}
*/
var jThis = $(this);
var parseText = jThis.text ().match (/\s*(refs)\s+\#(\d+)\s+(.+)/i);
if (parseText && parseText.length >= 4) {
//-- Truncate original link text.
jThis.text (parseText[1]);
//-- Add tailing link.
jThis.after (
' <a href="' + jThis.attr ("href") + '">' + parseText[3] + '</a>'
);
//-- Add Redmine link. Note it is inserted just after the original link.
var issueNumber = parseInt (parseText[2], 10);
jThis.after (
' <a href="http://redmine.project.com/redmine/issues/'
+ issueNumber + '">#' + issueNumber + '</a>'
);
//-- Style the Redmine link, if desired.
jThis.next ().css ("background", "yellow")
}
} );