Skip to content

Commit 9ed281a

Browse files
Copilothumitos
andcommitted
Add documentation for CustomScript addon feature
Co-authored-by: humitos <244656+humitos@users.noreply.github.com>
1 parent 6db7b8e commit 9ed281a

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

docs/user/addons.rst

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,61 @@ Here's a complete example showing how to create a version selector using the Add
388388
}
389389
);
390390

391+
Custom script
392+
-------------
393+
394+
The Custom Script addon allows you to inject a custom JavaScript file into your documentation at serve time.
395+
This enables you to modify or enhance frozen documentation without rebuilding it.
396+
397+
.. note::
398+
399+
The Custom Script addon is not currently exposed in the user interface.
400+
If you would like to use this feature,
401+
please :doc:`contact support </support>`.
402+
403+
Using custom scripts
404+
~~~~~~~~~~~~~~~~~~~~
405+
406+
This addon allows specifying a URL to a JavaScript file that will be injected into all pages of your documentation.
407+
The script can be hosted on Read the Docs itself (as a relative URL) or on an external server (as an absolute URL).
408+
409+
Common use cases for custom scripts include:
410+
411+
* Fixing bugs or adding features to frozen documentation
412+
* Injecting analytics or tracking code
413+
* Customizing the appearance or behavior of specific documentation versions
414+
415+
Accessing Addons data from your script
416+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
417+
418+
Custom scripts are loaded after the ``readthedocs-addons-data-ready`` event has been fired.
419+
To access the Addons data from your custom script,
420+
check the ``window.ReadTheDocsEventData`` object first,
421+
then subscribe to the event for future updates (for example, when the URL changes in a single-page application):
422+
423+
.. code-block:: javascript
424+
425+
function handleReadTheDocsData(data) {
426+
// Access the Addons data here
427+
console.log("Project slug:", data.projects.current.slug);
428+
429+
// You can filter by version to apply changes selectively
430+
if (data.versions.current.slug === "v3.0") {
431+
// Do something specific for version v3.0
432+
}
433+
}
434+
435+
// The event has already fired when custom scripts load.
436+
// Check for window.ReadTheDocsEventData first.
437+
if (window.ReadTheDocsEventData !== undefined) {
438+
handleReadTheDocsData(window.ReadTheDocsEventData.data());
439+
}
440+
441+
// Subscribe to the event for future data updates
442+
document.addEventListener("readthedocs-addons-data-ready", function (event) {
443+
handleReadTheDocsData(event.detail.data());
444+
});
445+
391446
Diving deeper
392447
-------------
393448

0 commit comments

Comments
 (0)