Amazon A to Z Org Chart Sorter

A new extension

Size

4.9 KB

Version

1.0.1

Created

Feb 23, 2026

Updated

27 days ago

1// ==UserScript==
2// @name		Amazon A to Z Org Chart Sorter
3// @description		A new extension
4// @version		1.0.1
5// @match		https://*.atoz.amazon.work/*
6// @icon		https://atoz.amazon.work/neverland-assets/c072b9453b6cc2e8ad56.png
7// ==/UserScript==
8(function() {
9    'use strict';
10
11    function extractLevel(levelText) {
12        // Extract numeric level from text like "L6", "L10", etc.
13        const match = levelText.match(/L(\d+)/);
14        return match ? parseInt(match[1]) : 0;
15    }
16
17    function extractTenure(infoText) {
18        // Extract tenure from text like "4+ years", "11+ years", etc.
19        const match = infoText.match(/(\d+)\+?\s*years?/i);
20        return match ? parseInt(match[1]) : 0;
21    }
22
23    function hasDirectReports(infoText) {
24        // Check if employee has direct reports
25        return infoText.includes('Direct reports:');
26    }
27
28    function sortOrgChart() {
29        console.log('Starting org chart sorting...');
30        
31        // Find all list items in the org chart
32        const orgChartContainer = document.querySelector('[data-pxt-telemetry-scope="OrgChart"]');
33        if (!orgChartContainer) {
34            console.log('Org chart container not found');
35            return;
36        }
37
38        // Get all employee list items
39        const listItems = Array.from(orgChartContainer.querySelectorAll('li.css-r73csp'));
40        console.log(`Found ${listItems.length} employee items`);
41
42        if (listItems.length === 0) return;
43
44        // Extract employee data
45        const employees = listItems.map(li => {
46            const levelSpan = li.querySelector('[data-test-component="AvatarLevel"]');
47            const infoText = li.querySelector('[data-testid="additional-info-text"]');
48            const nameDiv = li.querySelector('[data-test-component="AvatarFullname"]');
49            
50            const level = levelSpan ? levelSpan.textContent.trim() : '';
51            const info = infoText ? infoText.textContent.trim() : '';
52            const name = nameDiv ? nameDiv.textContent.trim() : '';
53            
54            return {
55                element: li,
56                name: name,
57                level: level,
58                levelNum: extractLevel(level),
59                tenure: extractTenure(info),
60                hasReports: hasDirectReports(info),
61                info: info
62            };
63        });
64
65        console.log('Employee data:', employees.map(e => ({
66            name: e.name,
67            level: e.level,
68            levelNum: e.levelNum,
69            tenure: e.tenure,
70            hasReports: e.hasReports
71        })));
72
73        // Identify the last layer (employees without direct reports)
74        const lastLayerEmployees = employees.filter(emp => !emp.hasReports);
75        console.log(`Last layer has ${lastLayerEmployees.length} employees`);
76
77        if (lastLayerEmployees.length === 0) return;
78
79        // Sort last layer by:
80        // 1. Level (descending: L10, L7, L6, L5, etc.)
81        // 2. Tenure (descending: 13+ years, 8+ years, 4+ years, etc.)
82        lastLayerEmployees.sort((a, b) => {
83            // First sort by level (descending)
84            if (a.levelNum !== b.levelNum) {
85                return b.levelNum - a.levelNum; // Higher level first
86            }
87            // Then sort by tenure (descending)
88            return b.tenure - a.tenure; // More tenure first
89        });
90
91        console.log('Sorted last layer:', lastLayerEmployees.map(e => ({
92            name: e.name,
93            level: e.level,
94            tenure: e.tenure + ' years'
95        })));
96
97        // Find the parent container of the last layer
98        const parentUl = lastLayerEmployees[0].element.parentElement;
99        if (!parentUl) {
100            console.log('Parent container not found');
101            return;
102        }
103
104        // Reorder the DOM elements
105        lastLayerEmployees.forEach(emp => {
106            parentUl.appendChild(emp.element);
107        });
108
109        console.log('Org chart sorted successfully!');
110    }
111
112    function debounce(func, wait) {
113        let timeout;
114        return function executedFunction(...args) {
115            const later = () => {
116                clearTimeout(timeout);
117                func(...args);
118            };
119            clearTimeout(timeout);
120            timeout = setTimeout(later, wait);
121        };
122    }
123
124    function init() {
125        console.log('Amazon A to Z Org Chart Sorter initialized');
126
127        // Initial sort
128        setTimeout(() => {
129            sortOrgChart();
130        }, 2000);
131
132        // Watch for DOM changes (when expanding/collapsing teams)
133        const observer = new MutationObserver(debounce(() => {
134            console.log('DOM changed, re-sorting...');
135            sortOrgChart();
136        }, 500));
137
138        // Observe the entire document for changes
139        observer.observe(document.body, {
140            childList: true,
141            subtree: true
142        });
143    }
144
145    // Run when body is ready
146    if (document.body) {
147        init();
148    } else {
149        document.addEventListener('DOMContentLoaded', init);
150    }
151})();