Dirty Ghost Spellchecker

A solution for spellcheck in Ghost

当前为 2014-10-14 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Dirty Ghost Spellchecker
  3. // @namespace http://reaves.co/
  4. // @version 0.1.3.1
  5. // @description A solution for spellcheck in Ghost
  6. // @match http://*/ghost/editor/*
  7. // @copyright 2014+, Ryan Reaves
  8. // ==/UserScript==
  9.  
  10.  
  11.  
  12. $(document).ready(function(){
  13. executeDirty();
  14. });
  15.  
  16. var checkIt = setInterval(function()
  17. {
  18. if(document.getElementById('spellModal') == undefined){
  19. executeDirty();
  20. clearInterval(checkIt);
  21. }
  22. }, 500);
  23.  
  24. function executeDirty(){
  25. var helpLink = document.querySelectorAll('a.markdown-help')[0];
  26. var $floatingHeader = document.querySelectorAll('header.floatingheader')[0];
  27. var $btn = $('<a id="spellModal" href="#" style="float:right" class="spellchecker">SPELLCHECK</a>');
  28. $btn.appendTo($floatingHeader);
  29. var textArea;
  30. // define a handler
  31. function doc_keyDown(e) {
  32.  
  33. if (e.altKey && e.keyCode == 70) {
  34. document.querySelectorAll('div.CodeMirror-code')[0].childNodes[0].childNodes[0].innerHTML = "";
  35. theMagic();
  36. e.preventDefault();
  37. }
  38. else if (e.altKey && e.keyCode == 87) {
  39. $('a.close').trigger('click');
  40. e.preventDefault();
  41. }
  42. }
  43. // register the handler
  44. document.addEventListener('keydown', doc_keyDown, false);
  45. $btn.on('click', function(e) {
  46. theMagic();
  47. });
  48. function theMagic(){
  49. var cpeditorElements = document.querySelectorAll('div.CodeMirror-code')[0].children.length;
  50. var cpeditor = document.querySelectorAll('div.CodeMirror-code')[0].childNodes[0].childNodes[0];
  51. var textArea = cpeditor.innerHTML;
  52. textArea = textArea.replace(/<\/?span[^>]*>/g,"");
  53. textArea = textArea.replace(/\u200B/g,'');
  54. for(i=1;i<cpeditorElements;i++){
  55. cpeditor = document.querySelectorAll('div.CodeMirror-code')[0].childNodes[i].childNodes[0].innerHTML;
  56. cpeditor = cpeditor.replace(/<\/?span[^>]*>/g,"");
  57. textArea = textArea.replace(/\u200B/g,'');
  58. textSub = textArea.substr(textArea.length - 1);
  59. textArea = textArea +'&#13;'+ cpeditor;
  60. }
  61. //textArea = textArea.replace(/\u000D/g,'');
  62. //Content area will not highlight spelling mistakes with a space on the end, so I will attach it below
  63. lastChar = textArea.substr(textArea.length - 1);
  64. if(lastChar != " "){
  65. textArea = textArea + " ";
  66. }
  67. //initiate help link, but then override the content in the modal window for spellcheck
  68. helpLink.click();
  69. var spellModal = document.querySelectorAll('header.modal-header')[0];
  70. spellModal.childNodes[0].innerHTML = 'Dirty Ghost SpellCheck';
  71. if(document.getElementById('spellTips') == undefined){
  72. spellModal.childNodes[0].insertAdjacentHTML('afterend','<span id="spellTips" style="float: right; padding-right: 25px">Open - Alt+F | Close - Alt+W</span>');
  73. }
  74. var shortcuts = document.querySelectorAll('section.modal-body')[0];
  75. $(".modal").css("width","75%");
  76. if(shortcuts != undefined){
  77. shortcuts.remove();
  78. }
  79. var txtbox = "1.) When done press [Ctrl+A] & then [Ctrl+C]<br>2.) Close window [Alt+W] and press [Ctrl+A] & [Ctrl+P]<br>Note: Mac Users will use Cmd<br><textarea style='width:100%; max-width:100% !important; height:100%' rows='20' spellcheck='true' id='spellcheck'>"+textArea+"</textarea>";
  80. spellModal.insertAdjacentHTML('afterend',txtbox);
  81. $("#spellcheck").focus();
  82. }
  83. }