docs.gradle.org: redirect current to actual version

Redirects /current/ to an exact version of Gradle docs to help create better permalinks to docs.gradle.org

目前为 2023-04-25 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name docs.gradle.org: redirect current to actual version
  3. // @description Redirects /current/ to an exact version of Gradle docs to help create better permalinks to docs.gradle.org
  4. // @version 2
  5. // @match https://docs.gradle.org/*
  6. // @namespace http://tampermonkey.net/
  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) 2022-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. function log(...toLog) {
  39. console.log('[gradle.org current redirecter]:', ...toLog);
  40. }
  41.  
  42. function redirect() {
  43. const currentUrl = document.location.href;
  44. const versionElement = document.querySelector("#command_line_interface > div.layout > header > nav > div.site-header__navigation-header > div.site-header-version > ul > li > span > span");
  45.  
  46. // siteDecorateVersion is a variable in Gradle's own JS
  47. if (versionElement || siteDecorateVersion) {
  48. log("siteDecorateVersion", siteDecorateVersion);
  49. const version = siteDecorateVersion ? siteDecorateVersion : versionElement.innerHTML;
  50. const permaUrl = currentUrl.replace('docs.gradle.org/current/', 'docs.gradle.org/' + version + '/');
  51. log('Redirecting to', permaUrl);
  52. document.location.assign(permaUrl);
  53. } else {
  54. log('Did not find version element and siteDecorateVersion is not defined. Trying again...');
  55. setTimeout(redirect, 500);
  56. }
  57. }
  58.  
  59. /*
  60. * For convenience the script is active on all documentation pages.
  61. * That way, it can be turned off even after redirection.
  62. * This is why we have to check the URL before trying to redirect.
  63. */
  64. if (document.location.href.includes('/current/')) {
  65. redirect();
  66. }
  67. })();