Skip to content

Commit 5ab9d1a

Browse files
committed
Fix menu item adding
1 parent cf7718f commit 5ab9d1a

File tree

1 file changed

+69
-70
lines changed

1 file changed

+69
-70
lines changed

src/github-custom-global-navigation.user.js

Lines changed: 69 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,92 +2168,89 @@
21682168
};
21692169
}
21702170

2171-
function waitForFeaturePreviewButton() {
2172-
log(VERBOSE, 'waitForFeaturePreviewButton()');
2173-
2174-
if (!HEADER) return;
2171+
function addMenuItemToActionList() {
2172+
log(VERBOSE, 'addMenuItemToActionList()');
21752173

21762174
const liElementId = 'custom-global-navigation-menu-item';
21772175

2178-
if (HEADER.querySelector(createId(liElementId))) return;
2176+
if (document.querySelector(createId(liElementId))) return;
21792177

2180-
const featurePreviewSearch = Array.from(
2181-
document.querySelectorAll('[data-position-regular="right"] span'),
2182-
)?.find(element => element.textContent === 'Feature preview') || null;
2178+
const actionList = document.querySelector(SELECTORS.sidebars.right.actionList);
2179+
if (!actionList) {
2180+
log(DEBUG, 'Action list not found');
2181+
return setTimeout(addMenuItemToActionList, 100);
2182+
}
21832183

2184-
if (featurePreviewSearch) {
2185-
const featurePreviewSpan = featurePreviewSearch;
2186-
const featurePreviewLabelDiv = featurePreviewSpan.parentNode;
2187-
const featurePreviewLi = featurePreviewLabelDiv.parentNode;
2184+
const signOutLink = actionList.querySelector(SELECTORS.sidebars.right.signOutLink);
2185+
if (!signOutLink) {
2186+
log(DEBUG, 'Logout link not found');
2187+
return;
2188+
}
21882189

2189-
const newLiElement = featurePreviewLi.cloneNode(true);
2190-
newLiElement.setAttribute('id', liElementId);
2190+
const signOutLi = signOutLink.parentNode;
21912191

2192-
newLiElement.onclick = () => {
2193-
const closeButton = document.querySelector(SELECTORS.sidebars.right.closeButton);
2194-
if (!closeButton) {
2195-
logError(`Selector '${SELECTORS.sidebars.right.closeButton}' not found`);
2196-
} else {
2197-
closeButton.click();
2198-
}
2192+
const newLiElement = signOutLi.cloneNode(true);
2193+
newLiElement.setAttribute('id', liElementId);
2194+
newLiElement.querySelector('a').removeAttribute('href');
21992195

2200-
GMC.open();
2201-
};
2196+
newLiElement.onclick = () => {
2197+
const closeButton = document.querySelector(SELECTORS.sidebars.right.closeButton);
2198+
if (!closeButton) {
2199+
logError(`Selector '${SELECTORS.sidebars.right.closeButton}' not found`);
2200+
} else {
2201+
closeButton.click();
2202+
}
22022203

2203-
const textElement = newLiElement.querySelector('button > span > span');
2204-
textElement.textContent = gmcGet('menu_item_title');
2204+
GMC.open();
2205+
};
22052206

2206-
const oldSvg = newLiElement.querySelector('svg');
2207+
const textElement = newLiElement.querySelector('a > [class^="prc-ActionList-ActionListSubContent-"]');
2208+
textElement.textContent = gmcGet('menu_item_title');
22072209

2208-
const menuItemIcon = gmcGet('menu_item_icon');
2209-
if (menuItemIcon === 'logo') {
2210-
const newSvg = document.createElement('img');
2211-
newSvg.setAttribute('height', '16px');
2212-
newSvg.setAttribute('width', '16px');
2213-
newSvg.src = `https://raw.githubusercontent.com/blakegearin/github-custom-global-navigation/main/img/${THEME}_logo.svg`;
2210+
const oldSvg = newLiElement.querySelector('svg');
22142211

2215-
oldSvg.parentNode.replaceChild(newSvg, oldSvg);
2216-
} else {
2217-
let svgString;
2218-
2219-
if (menuItemIcon === 'cog') {
2220-
svgString = `
2221-
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-gear">
2222-
<path d="M8 0a8.2 8.2 0 0 1 .701.031C9.444.095 9.99.645 10.16 1.29l.288 1.107c.018.066.079.158.212.224.231.114.454.243.668.386.123.082.233.09.299.071l1.103-.303c.644-.176 1.392.021 1.82.63.27.385.506.792.704 1.218.315.675.111 1.422-.364 1.891l-.814.806c-.049.048-.098.147-.088.294.016.257.016.515 0 .772-.01.147.038.246.088.294l.814.806c.475.469.679 1.216.364 1.891a7.977 7.977 0 0 1-.704 1.217c-.428.61-1.176.807-1.82.63l-1.102-.302c-.067-.019-.177-.011-.3.071a5.909 5.909 0 0 1-.668.386c-.133.066-.194.158-.211.224l-.29 1.106c-.168.646-.715 1.196-1.458 1.26a8.006 8.006 0 0 1-1.402 0c-.743-.064-1.289-.614-1.458-1.26l-.289-1.106c-.018-.066-.079-.158-.212-.224a5.738 5.738 0 0 1-.668-.386c-.123-.082-.233-.09-.299-.071l-1.103.303c-.644.176-1.392-.021-1.82-.63a8.12 8.12 0 0 1-.704-1.218c-.315-.675-.111-1.422.363-1.891l.815-.806c.05-.048.098-.147.088-.294a6.214 6.214 0 0 1 0-.772c.01-.147-.038-.246-.088-.294l-.815-.806C.635 6.045.431 5.298.746 4.623a7.92 7.92 0 0 1 .704-1.217c.428-.61 1.176-.807 1.82-.63l1.102.302c.067.019.177.011.3-.071.214-.143.437-.272.668-.386.133-.066.194-.158.211-.224l.29-1.106C6.009.645 6.556.095 7.299.03 7.53.01 7.764 0 8 0Zm-.571 1.525c-.036.003-.108.036-.137.146l-.289 1.105c-.147.561-.549.967-.998 1.189-.173.086-.34.183-.5.29-.417.278-.97.423-1.529.27l-1.103-.303c-.109-.03-.175.016-.195.045-.22.312-.412.644-.573.99-.014.031-.021.11.059.19l.815.806c.411.406.562.957.53 1.456a4.709 4.709 0 0 0 0 .582c.032.499-.119 1.05-.53 1.456l-.815.806c-.081.08-.073.159-.059.19.162.346.353.677.573.989.02.03.085.076.195.046l1.102-.303c.56-.153 1.113-.008 1.53.27.161.107.328.204.501.29.447.222.85.629.997 1.189l.289 1.105c.029.109.101.143.137.146a6.6 6.6 0 0 0 1.142 0c.036-.003.108-.036.137-.146l.289-1.105c.147-.561.549-.967.998-1.189.173-.086.34-.183.5-.29.417-.278.97-.423 1.529-.27l1.103.303c.109.029.175-.016.195-.045.22-.313.411-.644.573-.99.014-.031.021-.11-.059-.19l-.815-.806c-.411-.406-.562-.957-.53-1.456a4.709 4.709 0 0 0 0-.582c-.032-.499.119-1.05.53-1.456l.815-.806c.081-.08.073-.159.059-.19a6.464 6.464 0 0 0-.573-.989c-.02-.03-.085-.076-.195-.046l-1.102.303c-.56.153-1.113.008-1.53-.27a4.44 4.44 0 0 0-.501-.29c-.447-.222-.85-.629-.997-1.189l-.289-1.105c-.029-.11-.101-.143-.137-.146a6.6 6.6 0 0 0-1.142 0ZM11 8a3 3 0 1 1-6 0 3 3 0 0 1 6 0ZM9.5 8a1.5 1.5 0 1 0-3.001.001A1.5 1.5 0 0 0 9.5 8Z"></path>
2223-
</svg>
2224-
`;
2225-
} else if (menuItemIcon === 'compass') {
2226-
svgString = `
2227-
<svg xmlns="http://www.w3.org/2000/svg" height="1em" viewBox="0 0 512 512">
2228-
<!--! Font Awesome Free 6.4.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. -->
2229-
<path d="M464 256A208 208 0 1 0 48 256a208 208 0 1 0 416 0zM0 256a256 256 0 1 1 512 0A256 256 0 1 1 0 256zm306.7 69.1L162.4 380.6c-19.4 7.5-38.5-11.6-31-31l55.5-144.3c3.3-8.5 9.9-15.1 18.4-18.4l144.3-55.5c19.4-7.5 38.5 11.6 31 31L325.1 306.7c-3.2 8.5-9.9 15.1-18.4 18.4zM288 256a32 32 0 1 0 -64 0 32 32 0 1 0 64 0z"/>
2230-
</svg>
2231-
`;
2232-
}
2212+
const menuItemIcon = gmcGet('menu_item_icon');
2213+
if (menuItemIcon === 'logo') {
2214+
const newSvg = document.createElement('img');
2215+
newSvg.setAttribute('height', '16px');
2216+
newSvg.setAttribute('width', '16px');
2217+
newSvg.src = `https://raw.githubusercontent.com/blakegearin/github-custom-global-navigation/main/img/${THEME}_logo.svg`;
22332218

2234-
const parser = new DOMParser();
2235-
const svgDoc = parser.parseFromString(svgString, 'image/svg+xml');
2236-
const newSvg = svgDoc.documentElement;
2219+
oldSvg.parentNode.replaceChild(newSvg, oldSvg);
2220+
} else {
2221+
let svgString;
22372222

2238-
oldSvg.parentNode.replaceChild(newSvg, oldSvg);
2223+
if (menuItemIcon === 'cog') {
2224+
svgString = `
2225+
<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-gear">
2226+
<path d="M8 0a8.2 8.2 0 0 1 .701.031C9.444.095 9.99.645 10.16 1.29l.288 1.107c.018.066.079.158.212.224.231.114.454.243.668.386.123.082.233.09.299.071l1.103-.303c.644-.176 1.392.021 1.82.63.27.385.506.792.704 1.218.315.675.111 1.422-.364 1.891l-.814.806c-.049.048-.098.147-.088.294.016.257.016.515 0 .772-.01.147.038.246.088.294l.814.806c.475.469.679 1.216.364 1.891a7.977 7.977 0 0 1-.704 1.217c-.428.61-1.176.807-1.82.63l-1.102-.302c-.067-.019-.177-.011-.3.071a5.909 5.909 0 0 1-.668.386c-.133.066-.194.158-.211.224l-.29 1.106c-.168.646-.715 1.196-1.458 1.26a8.006 8.006 0 0 1-1.402 0c-.743-.064-1.289-.614-1.458-1.26l-.289-1.106c-.018-.066-.079-.158-.212-.224a5.738 5.738 0 0 1-.668-.386c-.123-.082-.233-.09-.299-.071l-1.103.303c-.644.176-1.392-.021-1.82-.63a8.12 8.12 0 0 1-.704-1.218c-.315-.675-.111-1.422.363-1.891l.815-.806c.05-.048.098-.147.088-.294a6.214 6.214 0 0 1 0-.772c.01-.147-.038-.246-.088-.294l-.815-.806C.635 6.045.431 5.298.746 4.623a7.92 7.92 0 0 1 .704-1.217c.428-.61 1.176-.807 1.82-.63l1.102.302c.067.019.177.011.3-.071.214-.143.437-.272.668-.386.133-.066.194-.158.211-.224l.29-1.106C6.009.645 6.556.095 7.299.03 7.53.01 7.764 0 8 0Zm-.571 1.525c-.036.003-.108.036-.137.146l-.289 1.105c-.147.561-.549.967-.998 1.189-.173.086-.34.183-.5.29-.417.278-.97.423-1.529.27l-1.103-.303c-.109-.03-.175.016-.195.045-.22.312-.412.644-.573.99-.014.031-.021.11.059.19l.815.806c.411.406.562.957.53 1.456a4.709 4.709 0 0 0 0 .582c.032.499-.119 1.05-.53 1.456l-.815.806c-.081.08-.073.159-.059.19.162.346.353.677.573.989.02.03.085.076.195.046l1.102-.303c.56-.153 1.113-.008 1.53.27.161.107.328.204.501.29.447.222.85.629.997 1.189l.289 1.105c.029.109.101.143.137.146a6.6 6.6 0 0 0 1.142 0c.036-.003.108-.036.137-.146l.289-1.105c.147-.561.549-.967.998-1.189.173-.086.34-.183.5-.29.417-.278.97-.423 1.529-.27l1.103.303c.109.029.175-.016.195-.045.22-.313.411-.644.573-.99.014-.031.021-.11-.059-.19l-.815-.806c-.411-.406-.562-.957-.53-1.456a4.709 4.709 0 0 0 0-.582c-.032-.499.119-1.05.53-1.456l.815-.806c.081-.08.073-.159.059-.19a6.464 6.464 0 0 0-.573-.989c-.02-.03-.085-.076-.195-.046l-1.102.303c-.56.153-1.113.008-1.53-.27a4.44 4.44 0 0 0-.501-.29c-.447-.222-.85-.629-.997-1.189l-.289-1.105c-.029-.11-.101-.143-.137-.146a6.6 6.6 0 0 0-1.142 0ZM11 8a3 3 0 1 1-6 0 3 3 0 0 1 6 0ZM9.5 8a1.5 1.5 0 1 0-3.001.001A1.5 1.5 0 0 0 9.5 8Z"></path>
2227+
</svg>
2228+
`;
2229+
} else if (menuItemIcon === 'compass') {
2230+
svgString = `
2231+
<svg xmlns="http://www.w3.org/2000/svg" height="1em" viewBox="0 0 512 512">
2232+
<!--! Font Awesome Free 6.4.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. -->
2233+
<path d="M464 256A208 208 0 1 0 48 256a208 208 0 1 0 416 0zM0 256a256 256 0 1 1 512 0A256 256 0 1 1 0 256zm306.7 69.1L162.4 380.6c-19.4 7.5-38.5-11.6-31-31l55.5-144.3c3.3-8.5 9.9-15.1 18.4-18.4l144.3-55.5c19.4-7.5 38.5 11.6 31 31L325.1 306.7c-3.2 8.5-9.9 15.1-18.4 18.4zM288 256a32 32 0 1 0 -64 0 32 32 0 1 0 64 0z"/>
2234+
</svg>
2235+
`;
22392236
}
22402237

2241-
const parentUl = featurePreviewLi.parentNode;
2242-
const settingsLi = document.querySelector('[data-position-regular="right"] a[href="/settings/profile"]').parentNode;
2243-
2244-
parentUl.insertBefore(newLiElement, settingsLi.nextSibling);
2238+
const parser = new DOMParser();
2239+
const svgDoc = parser.parseFromString(svgString, 'image/svg+xml');
2240+
const newSvg = svgDoc.documentElement;
22452241

2246-
const divider = featurePreviewLi.parentNode.querySelector(SELECTORS.sidebars.right.divider);
2247-
if (!divider) {
2248-
logError(`Selector '${SELECTORS.sidebars.right.divider}' not found`);
2249-
return;
2250-
}
2251-
const newDivider = divider.cloneNode(true);
2242+
oldSvg.parentNode.replaceChild(newSvg, oldSvg);
2243+
}
22522244

2253-
parentUl.insertBefore(newDivider, settingsLi.nextSibling);
2254-
} else {
2255-
setTimeout(waitForFeaturePreviewButton, 100);
2245+
const divider = signOutLi.parentNode.querySelector(SELECTORS.sidebars.right.divider);
2246+
if (!divider) {
2247+
logError(`Selector '${SELECTORS.sidebars.right.divider}' not found`);
2248+
return;
22562249
}
2250+
const newDivider = divider.cloneNode(true);
2251+
2252+
actionList.insertBefore(newLiElement, signOutLi);
2253+
actionList.insertBefore(newDivider, signOutLi);
22572254
}
22582255

22592256
function generateCustomConfig() {
@@ -3309,14 +3306,14 @@
33093306

33103307
if (!HEADER) return 'continue';
33113308

3312-
const featurePreviewButton = document.querySelector(SELECTORS.avatar.button);
3309+
const avatarButton = document.querySelector(SELECTORS.avatar.button);
33133310

3314-
if (!featurePreviewButton) {
3311+
if (!avatarButton) {
33153312
logError(`Selector ${SELECTORS.avatar.button} not found`);
33163313
return 'break';
33173314
}
33183315

3319-
featurePreviewButton.addEventListener('click', waitForFeaturePreviewButton);
3316+
avatarButton.addEventListener('click', addMenuItemToActionList);
33203317

33213318
CONFIG_NAME = {
33223319
'Off': 'off',
@@ -3643,6 +3640,8 @@
36433640
modalDialog: '#__primerPortalRoot__ > div > [data-position-regular="right"] > div',
36443641
closeButton: '#__primerPortalRoot__ button[aria-label="Close"]',
36453642
divider: 'li[data-component="ActionList.Divider"]',
3643+
actionList: '[class^="prc-ActionList-"]',
3644+
signOutLink: '[class^="prc-ActionList-"] a[href="/logout"]',
36463645
},
36473646
},
36483647
};

0 commit comments

Comments
 (0)