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

当前为 2023-05-11 提交的版本,查看 最新版本

  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 1
  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. // ==/UserScript==
  12.  
  13. /*
  14. * Copyright (c) 2023 Andrei Rybak
  15. *
  16. * Permission is hereby granted, free of charge, to any person obtaining a copy
  17. * of this software and associated documentation files (the "Software"), to deal
  18. * in the Software without restriction, including without limitation the rights
  19. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  20. * copies of the Software, and to permit persons to whom the Software is
  21. * furnished to do so, subject to the following conditions:
  22. *
  23. * The above copyright notice and this permission notice shall be included in all
  24. * copies or substantial portions of the Software.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  29. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  30. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  31. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34.  
  35. (function() {
  36. 'use strict';
  37.  
  38. const LOG_PREFIX = '[docs.gradle.org permalink]:';
  39.  
  40. function log(...toLog) {
  41. console.log(LOG_PREFIX, ...toLog);
  42. }
  43.  
  44. function warn(...toLog) {
  45. console.warn(LOG_PREFIX, ...toLog);
  46. }
  47.  
  48. function createPermalink() {
  49. const currentUrl = document.location.href;
  50. const versionElement = document.querySelector("#command_line_interface > div.layout > header > nav > div.site-header__navigation-header > div.site-header-version > ul > li > span > span");
  51.  
  52. // siteDecorateVersion is a variable in Gradle's own JS
  53. if (versionElement || typeof siteDecorateVersion !== 'undefined') {
  54. if (typeof siteDecorateVersion !== 'undefined') {
  55. log("siteDecorateVersion", siteDecorateVersion);
  56. } else {
  57. warn("siteDecorateVersion is not defined");
  58. }
  59.  
  60. const version = typeof siteDecorateVersion !== 'undefined' ? siteDecorateVersion : versionElement.innerHTML;
  61. const permaUrl = currentUrl.includes('/current/') ? currentUrl.replace('docs.gradle.org/current/', 'docs.gradle.org/' + version + '/') : currentUrl;
  62.  
  63. // element that contains the version selector dropdown
  64. const versionSelectorContainer = document.querySelector('div.layout > header > nav > div.site-header__navigation-header');
  65. log('Permalink:', permaUrl);
  66. const a = document.createElement('a');
  67. a.href = permaUrl;
  68. a.text = '[permalink ' + version + ']';
  69. a.style.cssText += `
  70. margin-top: 1.2em;
  71. margin-left: 1em;
  72. display: block;
  73. align-self: center;
  74. height: 36px;`;
  75. // add the permalink to the right of the dropdown:
  76. versionSelectorContainer.append(a);
  77. } else {
  78. log('Did not find version element and siteDecorateVersion is not defined. Trying again...');
  79. setTimeout(createPermalink, 500);
  80. }
  81. }
  82.  
  83. createPermalink();
  84. })();