- // ==UserScript==
- // @name Evernote Web HTML editor
- // @namespace http://andrealazzarotto.com/
- // @version 1.0
- // @description This scripts adds a button to edit the HTML code of a note in Evernote Web
- // @match http://www.evernote.com/Home.action*
- // @match https://www.evernote.com/Home.action*
- // @copyright 2015, Seb Maynard, Andrea Lazzarotto
- // @license Apache License, Version 2.0
- // @require http://code.jquery.com/jquery-latest.min.js
- // ==/UserScript==
-
- /*
- This program is a modified version of the Evernote HTML Editor bookmarklet
- created by Seb Maynard and released as open source software under the
- Apache 2.0 license. You can download the original software here:
-
- https://gist.github.com/sebmaynard/8f9f6b33247ab2f4bc85
- http://seb.so/html-source-editor-for-evernote-web-a-bookmarklet/
- */
-
- /*
- Copyright 2015 Seb Maynard, Andrea Lazzarotto
-
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
-
- var icon = "iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAQAAAD8x0bcAAABNUlEQVQoz43RMUibYRAG4CcaKhgcMnQILgaKg2hroWBtCF3axRDIkIA4FDoIcepcl3YtOLZQOohbp5ihBOwQWjCoZFAxDqKUgGAGKbQotDjo3yEkTVDRG+8evuPeL/TWzdVzRW/WX/03oZTv/lyHIobQ55mSmOhlNOq9hiyeiihJaFjy+D9Kq6jJq6ogZU/djq+mrdv2ooleSVgz4rkNTClhX0bcFw/MN9EHVU/sWjFh2D0lDCv4IW3XO8IoKho3Z0bSuRMV3JdS8NEqhDrCHHDXJ7/kMOjMz9Yg3BHBKZLy4Kgzp3BXjKf6bvstV6BADHU1gUDgs8BDbAmMCVovRfW4kPFIWcgyXmO8e13ZgbheEXfQ75ucl8rdKGvSYfuEXr9tWbTQjfYdO2ufEIh6gxWco27zHwRZUCDRa5r6AAAAAElFTkSuQmCC";
-
- var basic_html_formatter = function(code) {
- var block_tags = "p|div|ol|li|pre|blockquote|h[1-6]";
- code = code.replace(new RegExp("([^\n])(<[\/]?("+block_tags+")[^>]*>)", "g"), "$1\n$2");
- for (var i = 0; i<2; i++)
- code = code.replace(new RegExp("(<[\/]?("+block_tags+")[^>]*>)([^\n])", "g"), "$1\n$3");
- return code;
- }
-
-
- var getCurrentContent = function() {
- var content = $("#tinymce", $("iframe").contents()).first().clone();
- content.find("*").removeAttr("data-mce-style").removeAttr("data-mce-src").removeAttr("data-mce-href");
- return basic_html_formatter(content.html());
- };
- var setCurrentContent = function(content) {
- if(!$("table:has(input)").first().is(":visible")) // Activate the note editing mode again
- $("iframe.gwt-Frame").first().contents().find("body").click();
- $("#tinymce", $("iframe").contents()).first().html(content);
- $(".ennote", $("iframe").contents()).first().html(content);
- };
- var popupTextArea = function() {
- var theDiv = $("<div id='html_code_editor'></div>");
- theDiv.css({
- "z-index": "10000",
- "position": "fixed",
- "top": "0",
- "left": "0",
- "right": "0",
- "bottom": "0",
- "background-color": "rgba(0,0,0,0.5)"
- });
- var theTextArea = $("<div id='html_code_area' />");
- theTextArea.css({
- "width": "100%",
- "height": "100%",
- "box-sizing": "border-box",
- "outline": 0
- });
- theTextArea.text(getCurrentContent());
- var theButtons = $("<div><input id='btn_reset' type='reset'/><input id='btn_submit' type='submit'/></div>");
- theButtons.css({
- "z-index": "10001",
- "position": "fixed",
- "right": "4rem",
- "bottom": "2rem"
- });
- $("input", theButtons).css({
- "margin-left": "1.5rem",
- "background": $(".header").css("background-color"),
- "color": "white",
- "font-weight": "bold",
- "border": 0,
- "height": "2.5rem",
- "width": "8rem",
- "display": "inline-block"
- });
- theDiv.append(theTextArea);
- theDiv.append(theButtons);
- $("body").append(theDiv);
-
- var editor = ace.edit("html_code_area");
- editor.setTheme("ace/theme/ambiance");
- editor.getSession().setMode("ace/mode/html");
- editor.setOptions({
- fontSize: "18px",
- wrap: "free",
- enableBasicAutocompletion: true,
- scrollPastEnd: true,
- showPrintMargin: false,
- enableBasicAutocompletion: true,
- enableSnippets: true
- });
-
- $("#btn_submit", theButtons).click(function() {
- setCurrentContent(editor.getValue());
- theDiv.remove();
- });
- $("#btn_reset", theButtons).click(function() {
- theDiv.remove();
- });
- };
-
- var placeButton = function() {
- var prev = $("table:has(input)").first().find("td:nth-of-type(9)");
- if(!prev.length)
- return false;
- prev.after("<td id='html_edit'></td>");
- var btn = $("#html_edit");
- btn.attr("style", prev.attr("style"));
- prev.find("div").first().clone().appendTo(btn);
- btn.find("input").remove();
- btn.find("div").attr("title", "HTML");
- btn.find("div").css("background-image", "url('data:image/png;base64,"+icon+"')");
- btn.click(popupTextArea);
- return true;
- }
-
- $(document).ready(function() {
- $.getScript("https://cdn.rawgit.com/ajaxorg/ace-builds/master/src-min-noconflict/ace.js");
-
- // Place the button at the end of the formatting options
- setTimeout(function() {
- if(!placeButton())
- setTimeout(arguments.callee, 400);
- }, 400);
- });