Skip to content

Commit 01cb216

Browse files
authored
Activate tabs based on browser's operating system (#1216)
1 parent e95f908 commit 01cb216

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

_static/activate_tab.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Based on https://stackoverflow.com/a/38241481/724176
2+
function getOS() {
3+
const userAgent = window.navigator.userAgent,
4+
platform =
5+
window.navigator?.userAgentData?.platform || window.navigator.platform,
6+
macosPlatforms = ["macOS", "Macintosh", "MacIntel", "MacPPC", "Mac68K"],
7+
windowsPlatforms = ["Win32", "Win64", "Windows", "WinCE"],
8+
iosPlatforms = ["iPhone", "iPad", "iPod"];
9+
10+
if (macosPlatforms.includes(platform)) {
11+
return "macOS";
12+
} else if (iosPlatforms.includes(platform)) {
13+
return "iOS";
14+
} else if (windowsPlatforms.includes(platform)) {
15+
return "Windows";
16+
} else if (/Android/.test(userAgent)) {
17+
return "Android";
18+
} else if (/Linux/.test(platform)) {
19+
return "Unix";
20+
}
21+
22+
return "unknown";
23+
}
24+
25+
function activateTab(tabName) {
26+
// Find all label elements containing the specified tab name
27+
const labels = document.querySelectorAll(".tab-label");
28+
29+
labels.forEach((label) => {
30+
if (label.textContent.includes(tabName)) {
31+
// Find the associated input element using the 'for' attribute
32+
const tabInputId = label.getAttribute("for");
33+
const tabInput = document.getElementById(tabInputId);
34+
35+
// Check if the input element exists before attempting to set the "checked" attribute
36+
if (tabInput) {
37+
// Activate the tab by setting its "checked" attribute to true
38+
tabInput.checked = true;
39+
}
40+
}
41+
});
42+
}

conf.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
html_css_files = [
4646
'devguide_overrides.css',
4747
]
48+
html_js_files = [
49+
"activate_tab.js",
50+
]
4851
html_logo = "_static/python-logo.svg"
4952
html_favicon = "_static/favicon.png"
5053

index.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
Python Developer's Guide
33
========================
44

5+
.. raw:: html
6+
7+
<script>
8+
document.addEventListener('DOMContentLoaded', function() {
9+
activateTab(getOS());
10+
});
11+
</script>
12+
513
.. highlight:: bash
614

715
This guide is a comprehensive resource for :ref:`contributing <contributing>`

testing/run-write-tests.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
Running and writing tests
66
=========================
77

8+
.. raw:: html
9+
10+
<script>
11+
document.addEventListener('DOMContentLoaded', function() {
12+
activateTab(getOS());
13+
});
14+
</script>
15+
816
.. note::
917

1018
This document assumes you are working from an

0 commit comments

Comments
 (0)