Azure DevOps URL Base Toggle

Toggle Azure DevOps URL between dev.azure.com/{org} and {org}.visualstudio.com

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         Azure DevOps URL Base Toggle
// @namespace    https://tampermonkey.net/
// @version      1.1
// @author       Jatin Sharma
// @description  Toggle Azure DevOps URL between dev.azure.com/{org} and {org}.visualstudio.com
// @icon         https://www.google.com/s2/favicons?sz=64&domain=visualstudio.com
// @match        https://dev.azure.com/*
// @match        https://*.visualstudio.com/*
// @run-at       document-idle
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    const BUTTON_ID = 'ado-url-toggle-btn';

    function toggleUrl() {
        const url = new URL(window.location.href);

        if (url.hostname === 'dev.azure.com') {
            const parts = url.pathname.split('/').filter(Boolean);
            if (!parts.length) return;

            const org = parts.shift();
            url.hostname = `${org}.visualstudio.com`;
            url.pathname = '/' + parts.join('/');
        } else if (url.hostname.endsWith('.visualstudio.com')) {
            const org = url.hostname.split('.')[0];
            url.hostname = 'dev.azure.com';
            url.pathname = `/${org}${url.pathname}`;
        } else {
            return;
        }

        window.location.href = url.toString();
    }

    function createButton() {
        if (document.getElementById(BUTTON_ID)) return;

        const menuBar = document.querySelector('div.region-header-menubar');
        if (!menuBar) return;

        const button = document.createElement('button');
        button.id = BUTTON_ID;
        button.textContent = 'URL';
        button.title = 'Toggle Azure DevOps URL base';
        button.className = 'bolt-button bolt-icon-button enabled subtle bolt-focus-treatment';
        button.style.marginRight = '8px';
        button.style.cursor = 'pointer';

        button.addEventListener('click', toggleUrl);

        menuBar.prepend(button);
    }

    const observer = new MutationObserver(createButton);
    observer.observe(document.documentElement, { childList: true, subtree: true });

    createButton();
})();