Aman
Aman

Reputation: 602

Greasemonkey script to rewrite links

I need the Greasemonkey script to find all links like:

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

and re-write them like this:

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

This is my script, but it is not working.

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

Upvotes: 0

Views: 459

Answers (2)

Commentator
Commentator

Reputation: 650

You should consider use 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 );

    } );

}    

Some elements of the page load after the page was loaded, so you have to check if it is there to work with them. You can trigger your code with waitForKeyElements if you want, but I didn't test it.

Upvotes: 2

user557597
user557597

Reputation:

Everything looks ok.
Some things to look at:

Anchors ^$ do they match the href ?

Parsing Double Quotes won't remove the escape's from \/

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

Change it to ->

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

Upvotes: 0

Related Questions