Search code examples
javascriptreplaceuserscripts

Userscript to change Links


Im just trying to make a quick userscript to chnage a .com link into a .co.uk link, I looked at this question ( jQuery replacing relative links ) to try get some ideas and came up with this, but its not working.

theres a link to the script as well as the script below, but i cant see what im doing wrong. I did try it beofre hand with

$(document).ready(function()

but didnt know if that was supported in userscripts, so ditched i, still no luck.

http://www.seriousbrew.co.uk/scripts/FreeBooksLinkReplace.user.js

// ==UserScript==
// @name        Free Books Link Replace
// @namespace   www.seriousbrew.co.uk/scripts/
// @description one hundred freee books, changing links to .co.uk from .com
// @include     http://onehundredfreebooks.com/ 
// @version     2
// ==/UserScript==


function replacelinks() {
      $("a[href^='/']").each(function(){ 
      var cur_href = $(this).attr("href");
      var n=str.replace(/.com/gi, ".co.uk"); 
    });
}

replacelinks();

Solution

  • Try this:

    document.addEventListener("DOMContentLoaded", replaceLinks, false );
    
    if( document.readyState === "complete" ) {
        replaceLinks();
    }
    
    function replaceLinks() {
        Array.forEach( document.links, function(a) {
            a.href = a.href.replace( ".com", ".co.uk" );
        });
    }