iRacing-Auto-Join

This script adds an auto join checkbox after registering for an iRacing session. Once a green join button becomes available it will be automatically clicked if the checkbox is ticked.

当前为 2015-10-13 提交的版本,查看 最新版本

// ==UserScript==
// @name			iRacing-Auto-Join
// @version			2.0.0
// @author			Paul Ilbrink
// @copyright		2012 - 2015, Paul Ilbrink
// @namespace		http://www.paulilbrink.nl/userscripts
// @description		This script adds an auto join checkbox after registering for an iRacing session. Once a green join button becomes available it will be automatically clicked if the checkbox is ticked.
// @match			http://members.iracing.com/membersite/member/*
// @exclude			http://members.iracing.com/membersite/member/EventResult*
// @exclude			http://members.iracing.com/membersite/member/JoinedRace.do
// @grant			none
// ==/UserScript==

var script = document.createElement("script");
script.textContent = "(" + PI_iRacingAutoJoin.toString() + ")();";
document.body.appendChild(script);

function PI_iRacingAutoJoin() {

    PI_addStyle = function(css) {
        var style = document.createElement('style');
        style.textContent = css;
        document.head.appendChild(style);
    };

    PI_AUTO_JOIN = {
        key: 'PI_AUTO_JOIN',
        settings: {
            autoJoinChecked: false,
            autoJoinPreSessionPractice: false,
            sessionJoined: -1,
            uid: null
        },
        checkbox: null,
        uid: new Date().valueOf(),

        INIT: function() {
            var thiz = this;
            thiz.setupListeners();
            thiz.loadSettings();
            thiz.addSettings();
        },
        addSettings: function() {
            var thiz = this;
            $('<span class="icon icon-gear"></span>').css({
                position: 'absolute',
                top: 40, // 25,
                right: 7,
                color: 'gold',
                cursor: 'pointer'
            }).on('click', function() {
                var settingsHtml = '<div id=piAjSettings>';
                settingsHtml += '<h3>iRacing Auto Join settings</h3>';

                if (!thiz.checkbox) {
                    settingsHtml += '<label><input type=checkbox class=piAjCheckbox';
                    settingsHtml += (thiz.settings.autoJoinChecked  ?  ' checked=checked' : '');
                    settingsHtml += ' /> Auto join enabled?</label>';
                }

                settingsHtml += '<label><input type=checkbox class=piAjPreSessionPractice';
                settingsHtml += (thiz.settings.autoJoinPreSessionPractice  ?  ' checked=checked' : '');
                settingsHtml += ' /> Join pre-session practice? (if available)</label>';
                // settingsHtml += '<em>Will join ~2 minutes before the session start</em>';
                settingsHtml += '</div>';

                iRacingAlerts(settingsHtml);
            }).appendTo('#racingpanel_inner');
        },
        setupListeners: function() {
            var thiz = this;

            // storage listener
            $(window).on('storage', function(e) {
                if (e.originalEvent.key === thiz.key) {
                    // console.log('storage event updated', e.originalEvent);
                    lightsOn();
                    thiz.loadSettings();
                }
            });

            // pre session setting
            $(document).on('change', '.piAjPreSessionPractice', function() {
                thiz.settings.autoJoinPreSessionPractice = this.checked;
                thiz.saveSettings();
            });

            // checkbox
            $(document).on('change', '.piAjCheckbox', function(e) {
                e.stopPropagation();
                thiz.settings.autoJoinChecked = this.checked;
                if (!thiz.settings.autoJoinChecked) {
                    // manually unchecked the checkbox, so reset hasJoinedBefore?
                    // thiz.settings.sessionJoined = -1;
                }
                thiz.saveSettings();
            });
        },
        showCheckbox: function(data) {
            var thiz = this;
            if (thiz.checkbox) {
                // console.log('checkbox already present');
                return;
            }

            thiz.checkbox = $('<input type=checkbox class=piAjCheckbox />').css({
                position: 'absolute',
                top: 37, // 38,
                right: 27 // 2
            }).attr(
                'checked', thiz.settings.autoJoinChecked
            ).appendTo('#racingpanel_inner');

        },
        cleanupCheckbox: function(data, notRegisteredAnymore) {
            var thiz = this;
            if (thiz.checkbox) {
                thiz.checkbox.remove();
                thiz.checkbox = null;
            }
            if (notRegisteredAnymore) {
                thiz.settings.sessionJoined = -1;
                thiz.saveSettings();
            }
        },
        clickJoinButton: function(data) {
            var thiz = this;

            var isActualSession = data.sessionid === data.targetsessionid;
            var canJoin = thiz.settings.autoJoinChecked &&
                          (isActualSession || thiz.settings.autoJoinPreSessionPractice);

            if (canJoin) {
                setTimeout(function() {

                    var hasJoinedBefore = data.sessionid === thiz.settings.sessionJoined;
                    var joinButton = $('#racingpanel_session a.brand-success');

                    if (hasJoinedBefore) {
                        joinButton.text('Click yourself this time...');
                    } else {
                        thiz.settings.sessionJoined = data.sessionid;
                        thiz.settings.uid = thiz.uid;

                        joinButton.text('Auto join in progress...');
                        thiz.saveSettings();
                        joinButton.trigger('click');
                    }

                    thiz.cleanupCheckbox();

                }, 250);
            } else if (!isActualSession && !thiz.settings.autoJoinPreSessionPractice) {
                // there is a pre session practice, so wait for the actual session
            } else {
                // auto join not checked, leave checkbox there for now...
            }
        },
        loadSettings: function() {
            var thiz = this;
            var storageSettings = localStorage.getItem(thiz.key);

            if (storageSettings) {
                try {
                    $.extend(thiz.settings, JSON.parse(storageSettings));

                    if (thiz.checkbox && thiz.checkbox.checked !== thiz.settings.autoJoinChecked) {
                        thiz.checkbox.attr('checked', thiz.settings.autoJoinChecked);
                    }
                } catch (err) {
                    console.error('an error occurred while parsing the storageSettings', storageSettings, err);
                    return false;
                }
            }
        },
        saveSettings: function() {
            var thiz = this;

            thiz.settings.uid = thiz.uid;
            localStorage.setItem(thiz.key, JSON.stringify(thiz.settings));
        },
        monitorRegistrationStatus: function(data) {
            var thiz = this;
            if (data) {
                switch (data.status) {
                    case racingpaneldata.sessionstatus.ok_to_join:  // 3
                        thiz.showCheckbox(data);
                        thiz.clickJoinButton(data);
                        break;
                    case racingpaneldata.sessionstatus.registered:  // 1
                    case racingpaneldata.sessionstatus.assigned:    // 5
                        thiz.showCheckbox(data);
                        break;
                    case racingpaneldata.sessionstatus.do_not_join: // 2
                    case racingpaneldata.sessionstatus.withdrawn:   // 6
                    case racingpaneldata.sessionstatus.joined:      // 4
                    case racingpaneldata.sessionstatus.rejected:    // 7
                    case -1: // reg_none | You are not registered for any event.
                        thiz.cleanupCheckbox(data, true);
                        break;
                    default:
                        console.error('unknown sessionstatus', data.status, data.regStatus);
                        break;
                }
            } else {
                // not registered at all
                thiz.cleanupCheckbox('empty registration status object');
            }
        }
    };

    // check if there even is a control panel
    if (IRACING.control_panels) {

        PI_addStyle('#piAjSettings label { text-align:left; display:block; }');

        // init auto join
        PI_AUTO_JOIN.INIT();

        // interecepting iracing triggers to detect session registration status
        IRACING.control_panels.OrgShowRegistrationStatus = IRACING.control_panels.showRegistrationStatus;
        IRACING.control_panels.showRegistrationStatus = function(data) {
            try {
                PI_AUTO_JOIN.monitorRegistrationStatus(data);
            } catch (err) {
                console.err('an error occured while processing PI_AUTO_JOIN', err);
            }
            this.OrgShowRegistrationStatus(data);
        };
    }
}

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址