Greasemonkey脚本如何将链接拆分为三个相关链接? [英] How can a Greasemonkey script split a link into three related links?

查看:162
本文介绍了Greasemonkey脚本如何将链接拆分为三个相关链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Greasemonkey将cgit提交消息中显示的 Redmine 问题编号链接到他们的问题或项目。

I want to use Greasemonkey to link Redmine issue numbers which are displayed in cgit commit messages to their issues or projects.

HTML cgit提交消息的来源是这样的。

The HTML source of the cgit Commit messages is like this.

<a href='/editingmodule/commit/?id=49e4a33e0f8b306ded5'>refs #459 - create separate directory and repo for editingModule, lots of dif...</a>

我想做的是让#459成为 Redmine的单独href 问题,但保留两边的cgit链接不变。所以上面的URL转换成这样的东西:

What I want to do is to make the #459 a separate href to the Redmine issue, but leave retain the cgit links on both sides unchanged. So the URL above is transformed into something like this:

<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 项目。

It may be hard to read but the #459 in the link above is linked to the Redmine project.

为了清楚起见,还可以将指向 Redmine 问题的链接附加到cgit链接。

The link to the Redmine issue can also be appended to the cgit link for clarity.

推荐答案

使用jQuery查找链接,使用正则表达式提取关键文本部分。

Use jQuery to find the links, and regex to extract the key text parts.

这是完整的工作脚本;有关详细信息,请参阅内联注释:

Here is a complete working script; see the inline comments for more info:

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

这篇关于Greasemonkey脚本如何将链接拆分为三个相关链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆