Patabugen / LatestLaravel

// ==UserScript==
// @name         LatestLaravel
// @namespace    https://www.mha.systems
// @version      1.8
// @description  Automatically redirect you to your preferred version of the laravel documentation.
// @author       Patabugen
// @match        https://laravel.com/docs/*
// @license      MIT
// @grant        none
// @run-at       document-idle
// @downloadURL  https://openuserjs.org/install/Patabugen/LatestLaravel.user.js
// @updateURL    https://openuserjs.org/meta/Patabugen/LatestLaravel.meta.js
// @copyright    2017-2021, Patabugen (https://openuserjs.org/users/Patabugen)
// ==/UserScript==
// Change Log
// 0.1 Initial release
// 0.2 Warn when clicking version options
// 1.0 Use local storage to let users set their own preferred version
// 1.1 Change default "latest version" to be Laravel 8 (you can change it by clucking on the version selector on the Docs homepage)
// 1.2 Updated script because laravel.com changed the ID of the version dropdown.
// 1.3 2021-12-03 Updated updateURL
// 1.4 Set default Laravel version to 9.x
// 1.5 Fix not being redirected when changing preferred version when there's an anchor in the URL
// 1.6 Set default Laravel version to 10.x
// 1.7 2023-03-15 Fix versionParts regex not matching two-digit versions (e.g. Laravel 10)
// 1.8 Set default Laravel version to 11.x

(function() {
    'use strict';
    function redirectToPreferredVersion(){
        var localStorage = window.localStorage;

        var versionParts = document.location.pathname.match(/\/docs\/([0-9]{1,2}.[0-9x])/);
        var version = versionParts[1];

        var preferredVersion = localStorage.getItem('preferredVersion');
        if (!preferredVersion) {
            preferredVersion = "11.x";
            localStorage.setItem('preferredVersion', preferredVersion);
        }


        // Compare the versions ignoring any wildcard ".x"
        if (parseFloat(version) != parseFloat(preferredVersion.replace('.x', ''))) {
            var newUrl = document.location.href.replace('/docs/' + version, '/docs/' + preferredVersion);
            document.location.href = newUrl;
        }
    }

    redirectToPreferredVersion();

    var switcher = document.getElementById('version-switcher');
    switcher.onclick = function(e){
        var response = confirm("You must disable the LatestLaravel UserScript otherwise you'll just end up back here. Would you like to change your preferred version?");
        if (response) {
            var versionOptions = document.getElementById('version-switcher').children;
            var availableVersions = [];
            for (var i = 0; i < versionOptions.length; i++) {
                availableVersions.push(versionOptions[i].text);
            }
            console.log(availableVersions);
            var preferredVersion = prompt("Enter the version you would like?\n\n" + availableVersions.join(', '));
            if (availableVersions.indexOf(preferredVersion) > -1) {
                localStorage.setItem('preferredVersion', preferredVersion);
                redirectToPreferredVersion();
            } else {
                alert("Invalid version - please make sure you type it exactly as it appears in the list");
            }
        }
    }
})();