Fallen London - Wiki Linking

Adds Fallen London Archives wiki links to storylet titles and options.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        Fallen London - Wiki Linking
// @namespace   fallenlondon/wiki
// @description Adds Fallen London Archives wiki links to storylet titles and options.
// @author      Travers
// @include     http://*fallenlondon.com/Gap/Load*
// @include     http://fallenlondon.storynexus.com/Gap/Load*
// @version     1.1
// @grant       none
// ==/UserScript==
setUpObserver();

function setUpObserver()
{
    var target = document.querySelector('#mainContentLoading');
    var peeper = new MutationObserver(linkStorylets);
    peeper.observe(target, {attributes: true, childList: false, characterData: false});
}

function encodeLink(s)
{
    //Example: An Island with Secrets?  --->  An_Island_with_Secrets%3F
    s = s.replace(/ /g,'_');
    s = encodeURIComponent(s);
    return s;
}

function insertLink(elements)
{
    if (elements.length > 0)
    {
        var name = elements[0].innerHTML.trim();  
        elements[0].innerHTML = '<a style="color:black" href="http://fallenlondon.wikia.com/wiki/' + encodeLink(name) + '">' + name+'</a>';  
    }  
}

function linkStorylets()
{
    var storylets = document.getElementsByClassName('storylet_rhs');
    for (var i = 0; i < storylets.length; i++)
    {
        insertLink(storylets[i].getElementsByTagName('H2'));
        insertLink(storylets[i].getElementsByTagName('H5'));
    }   
    
    storylets = document.getElementsByClassName('storylet_flavour_text');
    if (storylets.length > 0)
    {
        insertLink(storylets[0].getElementsByTagName('H3'));
    }    
}