Add <br> around <code> tags on FreeCodeCamp

Adds <br> before and after <code> tags on FreeCodeCamp

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

  1. // ==UserScript==
  2. // @name Add <br> around <code> tags on FreeCodeCamp
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Adds <br> before and after <code> tags on FreeCodeCamp
  6. // @author Geromet
  7. // @match https://www.freecodecamp.org/learn/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14. function addLineBreaks() {
  15. const codeElements = document.getElementsByTagName('code');
  16. // Check if <br> tags are already added to avoid duplicates
  17. for (const codeElement of codeElements) {
  18. const prevSibling = codeElement.previousSibling;
  19. const nextSibling = codeElement.nextSibling;
  20. if (prevSibling && prevSibling.tagName === 'BR' && nextSibling && nextSibling.tagName === 'BR') {
  21. // Line breaks already added, skip this element
  22. continue;
  23. }
  24. // Create a <br> element before the <code> element
  25. const lineBreakBefore = document.createElement('br');
  26. codeElement.parentNode.insertBefore(lineBreakBefore, codeElement);
  27. // Create a <br> element after the <code> element
  28. const lineBreakAfter = document.createElement('br');
  29. codeElement.parentNode.insertBefore(lineBreakAfter, codeElement.nextSibling);
  30. }
  31. // Stop checking for <code> elements after they are found
  32. clearInterval(intervalId);
  33. }
  34.  
  35. // Check for the existence of <code> elements every 100 milliseconds
  36. const intervalId = setInterval(function() {
  37. const codeElements = document.getElementsByTagName('code');
  38. if (codeElements.length > 0) {
  39. // <code> elements found, stop the interval and add line breaks
  40. addLineBreaks();
  41. }
  42. }, 100);
  43. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址