docs.gradle.org: permalink to actual version

Adds a permalink for Gradle documentation pages (including /current/) to the exact version to help create better links to docs.gradle.org

  1. // ==UserScript==
  2. // @name docs.gradle.org: permalink to actual version
  3. // @description Adds a permalink for Gradle documentation pages (including /current/) to the exact version to help create better links to docs.gradle.org
  4. // @version 3
  5. // @match https://docs.gradle.org/*
  6. // @namespace https://github.com/rybak
  7. // @license MIT
  8. // @author Andrei Rybak
  9. // @icon https://www.google.com/s2/favicons?sz=64&domain=gradle.org
  10. // @grant none
  11. // @require https://cdn.jsdelivr.net/gh/rybak/userscript-libs@e86c722f2c9cc2a96298c8511028f15c45180185/waitForElement.js
  12. // ==/UserScript==
  13.  
  14. /*
  15. * Copyright (c) 2023 Andrei Rybak
  16. *
  17. * Permission is hereby granted, free of charge, to any person obtaining a copy
  18. * of this software and associated documentation files (the "Software"), to deal
  19. * in the Software without restriction, including without limitation the rights
  20. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  21. * copies of the Software, and to permit persons to whom the Software is
  22. * furnished to do so, subject to the following conditions:
  23. *
  24. * The above copyright notice and this permission notice shall be included in all
  25. * copies or substantial portions of the Software.
  26. *
  27. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  28. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  30. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  31. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  32. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33. * SOFTWARE.
  34. */
  35.  
  36. (function() {
  37. 'use strict';
  38.  
  39. const LOG_PREFIX = '[docs.gradle.org permalink]:';
  40.  
  41. function info(...toLog) {
  42. console.info(LOG_PREFIX, ...toLog);
  43. }
  44.  
  45. function warn(...toLog) {
  46. console.warn(LOG_PREFIX, ...toLog);
  47. }
  48.  
  49. function getPermaUrl(version) {
  50. const currentUrl = document.location.href;
  51. if (currentUrl.includes('/current/')) {
  52. return currentUrl.replace('docs.gradle.org/current/', 'docs.gradle.org/' + version + '/');
  53. } else {
  54. return currentUrl;
  55. }
  56. }
  57.  
  58. function createPermalink(version) {
  59. const permaUrl = getPermaUrl(version);
  60. info('Permalink:', permaUrl);
  61. const a = document.createElement('a');
  62. a.href = permaUrl;
  63. a.append(document.createTextNode("[permalink " + version + "]"));
  64. a.style.marginTop = '1.2em';
  65. a.style.marginLeft = '1em';
  66. a.style.display = 'block';
  67. a.style.alignSelf = 'center';
  68. a.style.height = '36px';
  69. return a;
  70. }
  71.  
  72. function addPermalink() {
  73. const versionElement = document.querySelector("#command_line_interface > div.layout > header > nav > div.site-header__navigation-header > div.site-header-version > ul > li > span > span");
  74.  
  75. // siteDecorateVersion is a variable in Gradle's own JS
  76. if (versionElement || typeof siteDecorateVersion !== 'undefined') {
  77. if (typeof siteDecorateVersion !== 'undefined') {
  78. info('siteDecorateVersion', siteDecorateVersion);
  79. } else {
  80. warn('siteDecorateVersion is not defined');
  81. }
  82.  
  83. const version = typeof siteDecorateVersion !== 'undefined' ? siteDecorateVersion : versionElement.innerHTML;
  84. const a = createPermalink(version);
  85.  
  86. /*
  87. * element that contains either the version selector dropdown (in tutorial docs) or
  88. * the version display `.site-header-version` (javadocs)
  89. */
  90. waitForElement('nav .site-header__navigation-header').then(container => {
  91. container.append(a);
  92. info('Navigation header has loaded. Added permalink.');
  93. });
  94. } else {
  95. info('Did not find version element and siteDecorateVersion is not defined. Trying again...');
  96. setTimeout(createPermalink, 500);
  97. }
  98. }
  99.  
  100. addPermalink();
  101. })();