Greasemonkey脚本重写链接

时间:2014-11-05 17:02:03

标签: javascript regex greasemonkey

我需要Greasemonkey脚本来查找所有链接,如:

http://legendas.tv/download/545832dfb67eb/American_Dad/American_Dad_S11E02_HDTV_x264_LOL_AFG_FUM_DIMENSION

并像这样重写它们:

http://legendas.tv/downloadarquivo/545832dfb67eb

这是我的脚本,但它不起作用。

// ==UserScript==
// @name        LTV
// @namespace   legendas.tv
// @include     http://legendas.tv
// @version     1
// @grant       none
// ==/UserScript==

var links = document.getElementsByTagName("*"); //array
var regex = /^(http:\/\/)(legendas\.tv\/download\/)(.{13})(.*)$/i;
for (var i=0,imax=links.length; i<imax; i++) {
   links[i].href = links[i].href.replace(regex,"$1.legendas.tv\/downloadarquivo\/$3\/");
}

2 个答案:

答案 0 :(得分:2)

你应该考虑使用jQuery。

// @include     http://*legendas.tv/
// @require     https://code.jquery.com/jquery-2.1.1.min.js
// @require     https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant       GM_addStyle
// ==/UserScript==

//wait it load
waitForKeyElements (".film", dlink);

//change headers links
$(".item a").each ( function () {

    var jThis = $(this);
    var href  = jThis.prop("href");
    var re = /[\w]+[\d]+/;
    var code = href.match(re)[0];
    var nLink = "/downloadarquivo/" + code;
    jThis.prop( "href", nLink );

});

//change download buttons links
function dlink() {

    $(".bt_seta_download a").each ( function () {

        var jThis = $(this);
        var href  = jThis.prop("href");
        var re = /[\w]+[\d]+/;
        var code = href.match(re)[0];
        var nLink = "/downloadarquivo/" + code;
        jThis.prop( "href", nLink );

    } );

}    

页面的一些元素在页面加载后加载,因此您必须检查是否可以使用它们。如果需要,可以使用waitForKeyElements触发代码,但我没有测试它。

答案 1 :(得分:0)

一切都很好 有些事情要看:

锚点^$是否匹配href?

解析双引号不会从\/

中删除转义符
"$1.legendas.tv\/downloadarquivo\/$3"

将其更改为 - &gt;

"$1.legendas.tv/downloadarquivo/$3"
相关问题