// KickAss Magnet Link Fixer v0.1

// Author @marceloclp

// Installation process:

// 1) Go to this link (it redirects you to the Chrome Web Store) and install this extension:

// https://chrome.google.com/webstore/detail/css-and-javascript-inject/ckddknfdmcemedlmmebildepcmneakaa

// 2) Go to http://kickasstorrents.to/ or the current domain of KickAss.

// 3) Open the extension menu on the top right corner of your Chrome.

// 4) Select the "Javascript" tag, copy and paste the content of this file in there.

// 5) Reload the KickAss page. If the script was sucessfull, you will see the magnet button turn to green.

// OBS: This doesn't work on the main page.

setTimeout ( function ( ) {

// Container with the download buttons which will be used to retrieve the magnet link.

let buttons = document. getElementsByClassName ( "downloadButtonGroup" ) [ 0 ] ;

if ( buttons ) {

console. log ( "Finding magnet..." ) ;

const MAGNET_START = 13 ; // The start index of the real magnet string for formatting porpouses.

const BREAKING_CHAR = "," ; // The breaking character that will be used to determine the end of the formated magnet string.

// Retrieve the full unformatted magnet string.

let magnet = buttons. getElementsByClassName ( "inlineblock" ) [ 0 ] . getAttribute ( "data-sc-params" ) ;

for ( let i = 0 ; i < magnet. length ; i ++ ) {

if ( magnet [ i ] === BREAKING_CHAR ) {

magnet = magnet. substring ( MAGNET_START , i - 1 ) ;

break ;

}

}

console. log ( "Attaching magnet to the magnet button..." ) ;

// The magnet DOM button that we will attach the magnet to.

let magnetElem = buttons. getElementsByClassName ( "kaGiantButton" ) [ 2 ] ;

// Attaching the magnet link to the element and resetting the link so it doesn't open a new tab.

magnetElem. setAttribute ( "href" , magnet ) ;

magnetElem. setAttribute ( "target" , "" ) ;

// Changing the element styles so the user knows the process has been sucessfull.

magnetElem. setAttribute ( "style" , "background: green !important;" ) ;

console. log ( "The magnet has been successfully attached to the button." )

}