-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathoverride-version.js
52 lines (46 loc) · 1.69 KB
/
override-version.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Override "main" with version variable
document.addEventListener('DOMContentLoaded', function() {
// Check if this is the "docs" pytorch_project
const metaElement = document.querySelector('meta[name="pytorch_project"]');
console.log("PyTorch Project:", metaElement);
if (!metaElement || metaElement.getAttribute('content') !== 'docs') {
return; // Exit early if not the pytorch docs project
}
const version = document.documentElement.getAttribute('data-version');
// Function to check and update buttons and dropdown items
function updateElements() {
// Update buttons
const buttons = document.querySelectorAll('.version-switcher__button');
let buttonFound = false;
buttons.forEach(btn => {
console.log("Found button:", btn.innerText);
if (btn.innerText.includes('main')) {
btn.innerText = version;
if (btn.hasAttribute('data-active-version-name')) {
btn.setAttribute('data-active-version-name', version);
}
buttonFound = true;
}
});
// Update dropdown items
const dropdownItems = document.querySelectorAll('.dropdown-item.list-group-item');
let dropdownFound = false;
dropdownItems.forEach(item => {
if (item.getAttribute('data-version') === 'main') {
// Update span text only
const span = item.querySelector('span');
if (span && span.innerText.includes('main')) {
span.innerText = version;
dropdownFound = true;
}
}
});
// If not found, try again after a delay
if ((!buttonFound || !dropdownFound) && attempts < 10) {
attempts++;
setTimeout(updateElements, 500);
}
}
let attempts = 0;
updateElements();
});