// ==UserScript==
// @name Evernote Web HTML editor
// @namespace http://andrealazzarotto.com/
// @version 2.1
// @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_old = "iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAMAAABhEH5lAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAzUExURUxpcYiIiIiIiExpcYiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiIiNINviEAAAAQdFJOUwCqZgDdzBEiuzNVRJl3/4jEJrZZAAAAhUlEQVQY05WPwQ7DIAxDDQokAdr5/7+2gVbrusOkWbnwYmwAfiizfqNkj2NVYM/1tvnYN3Rq4/AFWiHNMQSayJIDGUtcg7xmgFHeLmW/XWfWtseSqX80WgL08bDKjD8VP5PoZKKj0DmRQtRt1oEXEqF2QzbGnMirLJQK+4Uiy5erMVIo5QACmwWhG+ikMQAAAABJRU5ErkJggg==";
var icon_new = "iVBORw0KGgoAAAANSUhEUgAAABEAAAARCAMAAAAMs7fIAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAnUExURUxpcQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAK/XnHYAAAAMdFJOUwBEuzPMd2aZIt2I7sVV9wMAAABUSURBVBjTpY7bCsAgDEOjzOvy/9+7por6NBg7SmhCWwr8orbThQjkgissrzKGWRhM0luSuHs0uHp8T8k+eNDqp8uoT6OjU24kCpO9kRieYCveE/IBzfcCeIwe7AAAAAAASUVORK5CYII=";
var basic_html_formatter = function(code) {
var block_tags = "p|div|ol|ul|li|pre|blockquote|h[1-6]|table|thead|tbody|tr|td";
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, #en-note", $("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, #en-note", $("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"
});
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": "#2dbe60",
"color": "white",
"font-weight": "bold",
"border": 0,
"height": "2.5rem",
"min-width": "8rem",
"display": "inline-block"
});
var theTrick = $("<div><a href='https://lazza.me/EvernotePremium20USD'>Get Evernote Premium for €20 ($22) a year</a></div>");
theTrick.css({
"background": "rgba(0, 0, 0, 0.5)",
"padding": ".5em",
"font-family": "Gotham, sans-serif",
"z-index": "10001",
"position": "fixed",
"left": "4rem",
"bottom": "2rem"
});
$("a", theTrick).css("color", "white");
theDiv.append(theTextArea);
theDiv.append(theButtons);
theDiv.append(theTrick);
$("body").append(theDiv);
var editor = ace.edit("html_code_area");
editor.setTheme("ace/theme/tomorrow_night");
editor.getSession().setMode("ace/mode/html");
editor.setOptions({
fontSize: "18px",
wrap: "free",
enableBasicAutocompletion: true,
scrollPastEnd: true,
showPrintMargin: false,
enableSnippets: true,
enableLiveAutocompletion: true
});
$("#btn_submit", theButtons).click(function() {
setCurrentContent(editor.getValue());
theDiv.remove();
});
$("#btn_reset", theButtons).click(function() {
theDiv.remove();
});
};
var placeButton = function() {
// old Evernote Web
var prev = $("table:has(input)").first().find("td:nth-of-type(9)");
var code = "<td id='html_edit'></td>";
var icon = icon_old;
// new Evernote Web
if(!prev.length) {
prev = $("#gwt-debug-FormattingBar-strikeButton").parent();
code = "<div id='html_edit'></div>";
icon = icon_new;
}
if(!prev.length)
return false;
prev.after(code);
var btn = $("#html_edit");
btn.addClass(prev.attr('class'));
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").done(function(){
$.getScript("https://cdn.rawgit.com/ajaxorg/ace-builds/master/src-min-noconflict/ext-language_tools.js").done(function(){
// Place the button at the end of the formatting options
setTimeout(function() {
if(!placeButton())
setTimeout(arguments.callee, 400);
}, 400);
});
});
});