[Erepublik] Extract BBCode of articles

Extracts BBCode of articles

  1. // ==UserScript==
  2. // @name [Erepublik] Extract BBCode of articles
  3. // @match *://www.erepublik.com/en/article/*
  4. // @version 0.1
  5. // @description Extracts BBCode of articles
  6. // @author Mike Ontry
  7. // @grant none
  8. // @namespace https://greasyfork.org/users/3941
  9. // ==/UserScript==
  10.  
  11. var main = jQuery(document).find('div[class="post_content"]').get(0);
  12.  
  13. jQuery(main).find('h2').before('<a class="std_global_btn smallSize blueColor" style="float:right;" id="extractBB"><span>Extract BBCode</span></a>');
  14.  
  15. jQuery(document).find('a[id="extractBB"]').on("click", function() {
  16. var content = jQuery(main).find('div[class="full_content"]').html();
  17. jQuery(document).find('div[class="holder"]').after('<textarea id="bbCodeCont" rows="20" cols="115">'+ HtmltoBB(content) +'</textarea>');
  18. location.hash = "bbCodeCont";
  19. });
  20.  
  21. function HtmltoBB(html) {
  22.  
  23. html = html.replace(/<br\s*[\/]?>/gi, "\n\r");
  24. html = html.replace(/<b>/gi, "[b]");
  25. html = html.replace(/<i>/gi, "[i]");
  26. html = html.replace(/<u>/gi, "[u]");
  27. html = html.replace(/<\/b>/gi, "[/b]");
  28. html = html.replace(/<\/i>/gi, "[/i]");
  29. html = html.replace(/<\/u>/gi, "[/u]");
  30. html = html.replace(/<em>/gi, "[b]");
  31. html = html.replace(/<\/em>/gi, "[/b]");
  32. html = html.replace(/<strong>/gi, "[b]");
  33. html = html.replace(/<\/strong>/gi, "[/b]");
  34. html = html.replace(/<strike>/gi, "[s]");
  35. html = html.replace(/<\/strike>/gi, "[/s]");
  36. html = html.replace(/<sub>/gi, "[sub]");
  37. html = html.replace(/<\/sub>/gi, "[/sub]");
  38. html = html.replace(/<sup>/gi, "[sup]");
  39. html = html.replace(/<\/sup>/gi, "[/sup]");
  40. html = html.replace(/<hr>/gi, "-----");
  41. //html = html.replace(/<div(.*?)style="text-align:(.*?)"(.*?)>([\s\S]*?)<\/div>?=*$/gmi, "[$2]$4[/$2]");
  42. html = html.replace(/<div(.*?)style="(.*?)"(.*?)>/gi, "[center]");
  43. html = html.replace(/<\/div>/gi, "[/center]");
  44.  
  45. html = html.replace(/<img(.*?)src="(.*?)"(.*?)>/gi, "[img]$2[/img]");
  46. html = html.replace(/<a(.*?)href="(.*?)"(.*?)>(.*?)<\/a>/gi, "[url=$2]$4[/url]");
  47.  
  48. html = html.replace(/\/\//gi, "/");
  49. html = html.replace(/http:\//gi, "http://");
  50.  
  51. html = html.replace(/<(?:[^>'"]*|(['"]).*?\1)*>/gmi, "");
  52. html = html.replace(/\r\r/gi, "");
  53. html = html.replace(/\[img]\//gi, "[img]");
  54. html = html.replace(/\[url=\//gi, "[url=");
  55.  
  56. html = html.replace(/(\S)\n/gi, "$1 ");
  57.  
  58. return html;
  59. }