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-07-12 提交的版本。查看 最新版本

  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 2
  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 getLdJsonUrl() {
  49. const jsons = document.querySelectorAll('script[type="application/ld+json"]');
  50. for (let e of jsons) {
  51. const parsed = JSON.parse(e.textContent);
  52. if ('url' in parsed) {
  53. return parsed.url;
  54. }
  55. }
  56. return null;
  57. }
  58.  
  59. function createPermalink(version) {
  60. const currentUrl = document.location.href;
  61. const permaUrl = currentUrl.includes('/current/') ? currentUrl.replace('docs.gradle.org/current/', 'docs.gradle.org/' + version + '/') : currentUrl;
  62. log('Permalink:', permaUrl);
  63. const a = document.createElement('a');
  64. a.href = permaUrl;
  65. a.text = '[permalink ' + version + ']';
  66. a.style.cssText += `
  67. margin-top: 1.2em;
  68. margin-left: 1em;
  69. display: block;
  70. align-self: center;
  71. height: 36px;`;
  72. return a;
  73. }
  74.  
  75. // from https://stackoverflow.com/a/61511955/1083697 by Yong Wang
  76. function waitForElement(selector) {
  77. return new Promise(resolve => {
  78. if (document.querySelector(selector)) {
  79. return resolve(document.querySelector(selector));
  80. }
  81. const observer = new MutationObserver(mutations => {
  82. if (document.querySelector(selector)) {
  83. resolve(document.querySelector(selector));
  84. observer.disconnect();
  85. }
  86. });
  87.  
  88. observer.observe(document.body, {
  89. childList: true,
  90. subtree: true
  91. });
  92. });
  93. }
  94.  
  95. function addPermalink() {
  96. const versionElement = document.querySelector("#command_line_interface > div.layout > header > nav > div.site-header__navigation-header > div.site-header-version > ul > li > span > span");
  97.  
  98. // siteDecorateVersion is a variable in Gradle's own JS
  99. if (versionElement || typeof siteDecorateVersion !== 'undefined') {
  100. // log("JSON:", getLdJsonUrl());
  101.  
  102. if (typeof siteDecorateVersion !== 'undefined') {
  103. log("siteDecorateVersion", siteDecorateVersion);
  104. } else {
  105. warn("siteDecorateVersion is not defined");
  106. }
  107.  
  108. const version = typeof siteDecorateVersion !== 'undefined' ? siteDecorateVersion : versionElement.innerHTML;
  109. const a = createPermalink(version);
  110.  
  111. // element that contains either the version selector dropdown (in tutorial docs) or the version display `.site-header-version` (javadocs)
  112. waitForElement('nav .site-header__navigation-header').then(container => {
  113. container.append(a);
  114. });
  115. } else {
  116. log('Did not find version element and siteDecorateVersion is not defined. Trying again...');
  117. setTimeout(createPermalink, 500);
  118. }
  119. }
  120.  
  121. addPermalink();
  122. })();