繁体   English   中英

用Greasemonkey脚本重写链接

[英]Greasemonkey script to rewrite links

我需要使用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\/");
}

您应该考虑使用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触发代码,但是我没有对其进行测试。

一切看起来还好。
一些事情要看:

^$他们是否匹配href?

解析双引号不会删除\\/中的转义符

"$1.legendas.tv\/downloadarquivo\/$3"

更改为->

"$1.legendas.tv/downloadarquivo/$3"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM