Evernote Web HTML editor

This scripts adds a button to edit the HTML code of a note in Evernote Web

目前为 2015-03-30 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Evernote Web HTML editor
  3. // @namespace http://andrealazzarotto.com/
  4. // @version 1.0
  5. // @description This scripts adds a button to edit the HTML code of a note in Evernote Web
  6. // @match http://www.evernote.com/Home.action*
  7. // @match https://www.evernote.com/Home.action*
  8. // @copyright 2015, Seb Maynard, Andrea Lazzarotto
  9. // @license Apache License, Version 2.0
  10. // @require http://code.jquery.com/jquery-latest.min.js
  11. // ==/UserScript==
  12.  
  13. /*
  14. This program is a modified version of the Evernote HTML Editor bookmarklet
  15. created by Seb Maynard and released as open source software under the
  16. Apache 2.0 license. You can download the original software here:
  17. https://gist.github.com/sebmaynard/8f9f6b33247ab2f4bc85
  18. http://seb.so/html-source-editor-for-evernote-web-a-bookmarklet/
  19. */
  20.  
  21. /*
  22. Copyright 2015 Seb Maynard, Andrea Lazzarotto
  23.  
  24. Licensed under the Apache License, Version 2.0 (the "License");
  25. you may not use this file except in compliance with the License.
  26. You may obtain a copy of the License at
  27.  
  28. http://www.apache.org/licenses/LICENSE-2.0
  29.  
  30. Unless required by applicable law or agreed to in writing, software
  31. distributed under the License is distributed on an "AS IS" BASIS,
  32. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  33. See the License for the specific language governing permissions and
  34. limitations under the License.
  35. */
  36.  
  37. var icon = "iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAQAAAD8x0bcAAABNUlEQVQoz43RMUibYRAG4CcaKhgcMnQILgaKg2hroWBtCF3axRDIkIA4FDoIcepcl3YtOLZQOohbp5ihBOwQWjCoZFAxDqKUgGAGKbQotDjo3yEkTVDRG+8evuPeL/TWzdVzRW/WX/03oZTv/lyHIobQ55mSmOhlNOq9hiyeiihJaFjy+D9Kq6jJq6ogZU/djq+mrdv2ooleSVgz4rkNTClhX0bcFw/MN9EHVU/sWjFh2D0lDCv4IW3XO8IoKho3Z0bSuRMV3JdS8NEqhDrCHHDXJ7/kMOjMz9Yg3BHBKZLy4Kgzp3BXjKf6bvstV6BADHU1gUDgs8BDbAmMCVovRfW4kPFIWcgyXmO8e13ZgbheEXfQ75ucl8rdKGvSYfuEXr9tWbTQjfYdO2ufEIh6gxWco27zHwRZUCDRa5r6AAAAAElFTkSuQmCC";
  38.  
  39. var basic_html_formatter = function(code) {
  40. var block_tags = "p|div|ol|li|pre|blockquote|h[1-6]";
  41. code = code.replace(new RegExp("([^\n])(<[\/]?("+block_tags+")[^>]*>)", "g"), "$1\n$2");
  42. for (var i = 0; i<2; i++)
  43. code = code.replace(new RegExp("(<[\/]?("+block_tags+")[^>]*>)([^\n])", "g"), "$1\n$3");
  44. return code;
  45. }
  46.  
  47.  
  48. var getCurrentContent = function() {
  49. var content = $("#tinymce", $("iframe").contents()).first().clone();
  50. content.find("*").removeAttr("data-mce-style").removeAttr("data-mce-src").removeAttr("data-mce-href");
  51. return basic_html_formatter(content.html());
  52. };
  53. var setCurrentContent = function(content) {
  54. if(!$("table:has(input)").first().is(":visible")) // Activate the note editing mode again
  55. $("iframe.gwt-Frame").first().contents().find("body").click();
  56. $("#tinymce", $("iframe").contents()).first().html(content);
  57. $(".ennote", $("iframe").contents()).first().html(content);
  58. };
  59. var popupTextArea = function() {
  60. var theDiv = $("<div id='html_code_editor'></div>");
  61. theDiv.css({
  62. "z-index": "10000",
  63. "position": "fixed",
  64. "top": "0",
  65. "left": "0",
  66. "right": "0",
  67. "bottom": "0",
  68. "background-color": "rgba(0,0,0,0.5)"
  69. });
  70. var theTextArea = $("<div id='html_code_area' />");
  71. theTextArea.css({
  72. "width": "100%",
  73. "height": "100%",
  74. "box-sizing": "border-box",
  75. "outline": 0
  76. });
  77. theTextArea.text(getCurrentContent());
  78. var theButtons = $("<div><input id='btn_reset' type='reset'/><input id='btn_submit' type='submit'/></div>");
  79. theButtons.css({
  80. "z-index": "10001",
  81. "position": "fixed",
  82. "right": "4rem",
  83. "bottom": "2rem"
  84. });
  85. $("input", theButtons).css({
  86. "margin-left": "1.5rem",
  87. "background": $(".header").css("background-color"),
  88. "color": "white",
  89. "font-weight": "bold",
  90. "border": 0,
  91. "height": "2.5rem",
  92. "width": "8rem",
  93. "display": "inline-block"
  94. });
  95. theDiv.append(theTextArea);
  96. theDiv.append(theButtons);
  97. $("body").append(theDiv);
  98. var editor = ace.edit("html_code_area");
  99. editor.setTheme("ace/theme/ambiance");
  100. editor.getSession().setMode("ace/mode/html");
  101. editor.setOptions({
  102. fontSize: "18px",
  103. wrap: "free",
  104. enableBasicAutocompletion: true,
  105. scrollPastEnd: true,
  106. showPrintMargin: false,
  107. enableBasicAutocompletion: true,
  108. enableSnippets: true
  109. });
  110. $("#btn_submit", theButtons).click(function() {
  111. setCurrentContent(editor.getValue());
  112. theDiv.remove();
  113. });
  114. $("#btn_reset", theButtons).click(function() {
  115. theDiv.remove();
  116. });
  117. };
  118.  
  119. var placeButton = function() {
  120. var prev = $("table:has(input)").first().find("td:nth-of-type(9)");
  121. if(!prev.length)
  122. return false;
  123. prev.after("<td id='html_edit'></td>");
  124. var btn = $("#html_edit");
  125. btn.attr("style", prev.attr("style"));
  126. prev.find("div").first().clone().appendTo(btn);
  127. btn.find("input").remove();
  128. btn.find("div").attr("title", "HTML");
  129. btn.find("div").css("background-image", "url('data:image/png;base64,"+icon+"')");
  130. btn.click(popupTextArea);
  131. return true;
  132. }
  133.  
  134. $(document).ready(function() {
  135. $.getScript("https://cdn.rawgit.com/ajaxorg/ace-builds/master/src-min-noconflict/ace.js");
  136.  
  137. // Place the button at the end of the formatting options
  138. setTimeout(function() {
  139. if(!placeButton())
  140. setTimeout(arguments.callee, 400);
  141. }, 400);
  142. });