Fix kbin Code Blocks

Dirty fix for kbin code blocks federated from Lemmy. The latter is currently federating code blocks with weird span tags added in. This is fully legal in the spec, but kbin doesn't handle this currently and thinks Lemmy should fix it despite it not being Lemmy's problem. This means that code blocks federated from Lemmy contain weird <span> tags on every single line of the code, which really should be stripped out.

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

  1. // ==UserScript==
  2. // @name Fix kbin Code Blocks
  3. // @namespace pamasich-kbin
  4. // @version 1.0
  5. // @description Dirty fix for kbin code blocks federated from Lemmy. The latter is currently federating code blocks with weird span tags added in. This is fully legal in the spec, but kbin doesn't handle this currently and thinks Lemmy should fix it despite it not being Lemmy's problem. This means that code blocks federated from Lemmy contain weird <span> tags on every single line of the code, which really should be stripped out.
  6. // @author Pamasich
  7. // @match https://kbin.social/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=kbin.social
  9. // @license MIT
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. window.addEventListener('load', function() {
  17. for (let codeblock of document.querySelectorAll('pre code')) {
  18. let output = "";
  19. let found = false;
  20. for (let line of codeblock.innerHTML.split('\n')) {
  21. console.log(line);
  22. if (found == true) {
  23. line = line.slice(13, line.length);
  24. found = false;
  25. }
  26. console.log(line);
  27. if (line.startsWith('&lt;span style=\"color:#323232;\"&gt;')) {
  28. line = line.slice(35, line.length);
  29. found = true;
  30. }
  31. output += "\n" + line;
  32. }
  33. codeblock.innerHTML = output.slice(1,output.length);
  34. }
  35. });
  36. })();