[Home]PeterTaylor/RavenblackGreasemonkeyScript

ec2-18-223-32-230.us-east-2.compute.amazonaws.com | ToothyWiki | PeterTaylor | RecentChanges | Login | Webcomic

Adds a Notes field which allows you to remind yourself what you're doing and what you think your opponent's doing.
// ==UserScript==
// @name         Notes Field
// @include      http://games.ravenblack.net/warlocks?*
// @description  Facilitates storage of notes for a game.
// @exclude
// ==/UserScript==
 
(function() {
    // Get stored notes from cookie.
    var gameID = "" + document.location;
    gameID = gameID.replace(/.*num=([^&]*).*/, "$1");
    var cookieName = "warlocksNotes" + gameID;
    var notes = "";
    var cookies = document.cookie.split('; ');
    for (var i = 0; i < cookies.length; i++) {
        var oneCookie = cookies[i].split('=');
        if (oneCookie[0] == cookieName) {
            notes = oneCookie[1];
            break;
        }
    }

    // Find out where to add them to the page.
    var ordersForm = document.evaluate("//form[@action='warlocksubmit']", document, null, XPathResult.ANY_TYPE, null);
    var formNode = ordersForm.iterateNext();
    if (formNode == null)
    {
        // No form, so no need for notes.
        return;
    }

    var tableNode = formNode.parentNode;
    var tbody = document.evaluate("tbody", tableNode, null, XPathResult.ANY_TYPE, null);
    var tbodyNode = tbody.iterateNext();

    // Create and add the various elements required
    var notesTR = document.createElement('tr');
    tbodyNode.insertBefore(notesTR, tbodyNode.firstChild);
    var notesTD = document.createElement('td');
    notesTR.appendChild(notesTD);
    var textArea = document.createElement('textarea');
    notesTD.appendChild(textArea);
    var notesTD2 = document.createElement('td');
    notesTR.appendChild(notesTD2);
    var button = document.createElement('input');
    notesTD2.appendChild(button);

    // Set up the necessary attributes.
    notesTD.setAttribute('colspan', '3');

    textArea.setAttribute('name', 'notesField');
    textArea.setAttribute('cols', '50');
    textArea.setAttribute('rows', '5');
    textArea.value = notes;

    button.setAttribute('value', 'Store notes');
    button.setAttribute('type', 'button')
    button.setAttribute(
        'onclick',
        'var setTA = document.evaluate("//textarea", document, null, XPathResult.ANY_TYPE, null);' +
        'var notes = setTA.iterateNext().value;' +
        'var date = new Date();' +
        'date.setTime(date.getTime() + (365*24*60*60*1000));' +
        'var expires = "; expires=" + date.toGMTString();' +
        'document.cookie = "' + cookieName + '=" + notes + expires + "; path=/";' +
        'return true;');

    // Why not add a header too?<tr>
    var headerTR = document.createElement('tr');
    tbodyNode.insertBefore(headerTR, notesTR);
    var headerTD = document.createElement('td');
    headerTR.appendChild(headerTD);
    var headerB = document.createElement('b');
    headerTD.appendChild(headerB);
    headerB.appendChild(document.createTextNode("Notes"));

    headerTD.setAttribute('colspan', '4');
    headerTD.setAttribute('class', 'lightbg');
})();


ec2-18-223-32-230.us-east-2.compute.amazonaws.com | ToothyWiki | PeterTaylor | RecentChanges | Login | Webcomic
This page is read-only | View other revisions | Recently used referrers
Last edited November 2, 2005 11:40 pm (viewing revision 1, which is the newest) (diff)
Search: