HTML2BBCode

html2bb *tries* to convert html code to bbcode as accurately as possible

As of 2016-10-24. See the latest version.

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greatest.deepsurf.us/scripts/23948/154279/HTML2BBCode.js

  1. // ==UserScript==
  2. // @name HTML2BBCode
  3. // @namespace https://orbitalzero.ovh/scripts
  4. // @version 0.02
  5. // @description html2bb *tries* to convert html code to bbcode as accurately as possible
  6. // @author NeutronNoir
  7. //
  8. // ==/UserScript==
  9.  
  10. function html2bb(str) {
  11. if(typeof str === "undefined") return "";
  12. str = str.replace(/< *br *\/*>/g, "\n");
  13. str = str.replace(/< *u *>/g, "[u]");
  14. str = str.replace(/< *\/ *u *>/g, "[/u]");
  15. str = str.replace(/< *\/ *li *>/g, "");
  16. str = str.replace(/< *li *>/g, "[*]");
  17. str = str.replace(/< *\/ *ul *>/g, "");
  18. str = str.replace(/< *ul *class=\\*\"bb_ul\\*\" *>/g, "");
  19. str = str.replace(/< *h2 *class=\"bb_tag\" *>/g, "[u]");
  20. str = str.replace(/< *\/ *h2 *>/g, "[/u]");
  21. str = str.replace(/< *strong *>/g, "[b]");
  22. str = str.replace(/< *\/ *strong *>/g, "[/b]");
  23. str = str.replace(/< *i *>/g, "[i]");
  24. str = str.replace(/< *\/ *i *>/g, "[/i]");
  25. str = str.replace(/\&quot;/g, "\"");
  26. str = str.replace(/< *img *src="([^"]*)" *>/g, "[img]$1[/img]");
  27. str = str.replace(/< *b *>/g, "[b]");
  28. str = str.replace(/< *\/ *b *>/g, "[/b]");
  29. str = str.replace(/< *a [^>]*>/g, "");
  30. str = str.replace(/< *\/ *a *>/g, "");
  31. //Yeah, all these damn stars. Because people put spaces where they shouldn't.
  32. return str;
  33. }