// ==UserScript== // @name TinyMce for Evernote // @namespace https://github.com/Amourspirit/TinyMce-for-Evernote // @version 1.2.1 // @description Adds TinyMce in Evernote with custom options including source code. A new button is added to Evernote top toolbar section. // @run-at document-end // @include /^https?:\/\/www\.evernote\.com\/home\.action.*n=.*$/ // @match http://www.evernote.com/Home.action* // @match https://www.evernote.com/Home.action* // @grant none // @noframes // @license MIT // @homepageURL http://amourspirit.github.io/TinyMce-for-Evernote/ // @update https://github.com/Amourspirit/TinyMce-for-Evernote/raw/master/TinyMce_for_Evernote.user.js // @contributionURL http://amourspirit.github.io/TinyMce-for-Evernote/#donate // @downloadURL none // ==/UserScript== 'use strict'; // #nsregion BIGBYTE // #region Methods var BIGBYTE = BIGBYTE || {}; if (typeof(BIGBYTE.createNS) == 'undefined') { BIGBYTE.createNS = function(namespace) { var nsparts = namespace.split("."); var parent = BIGBYTE; // we want to be able to include or exclude the root namespace so we strip // it if it's in the namespace)) if (nsparts[0] === "BIGBYTE") { nsparts = nsparts.slice(1); } // loop through the parts and create a nested namespace if necessary for (var i = 0; i < nsparts.length; i++) { var partname = nsparts[i]; // check if the current parent already has the namespace declared // if it isn't, then create it if (typeof parent[partname] === "undefined") { parent[partname] = {}; } // get a reference to the deepest element in the hierarchy so far parent = parent[partname]; } // the parent is now constructed with empty namespaces and can be used. // we return the outermost namespace return parent; }; } if (typeof(BIGBYTE.isjquery) == 'undefined') { BIGBYTE.isjquery = function(data) { // If data is already a jQuery object if (data instanceof jQuery) { // Do nothing different data = data; // Otherwise } else { // Convert to jQuery object data = jQuery(data); } // Return jQuery object return data; }; } // #endregion // #endnsregion BIGBYTE // #nsregion BIGBYTE.USERSCRIPT.DOCUMENT if (window.top != window.self) { // if this is an iframe then return return; } var bbDoc = BIGBYTE.createNS("BIGBYTE.USERSCRIPT.DOCUMENT"); bbDoc.ns = 'BIGBYTE.USERSCRIPT.DOCUMENT'; // #region LoadScripts if (typeof(bbDoc.loadScript) == 'undefined') { bbDoc.loadScript = function(scriptItm) { var lib = this; if (typeof(scriptItm.count) == 'undefined') { scriptItm.count = 0; } if (typeof(scriptItm.loaded) == 'undefined') { scriptItm.loaded = false; } if (typeof(scriptItm.text) == 'undefined') { scriptItm.text = ''; // timeout in seconds } if (typeof(scriptItm.timeout) == 'undefined') { scriptItm.timeout = 30; // timeout in seconds } var bbScriptLoadedEvent = new CustomEvent( "bbScriptLoaded", { detail: { message: "Script Loaded", time: new Date(), scriptItm: scriptItm }, bubbles: true, cancelable: false } ); switch (scriptItm.type) { case 'linkedjs': var skipTest = false; if (typeof(scriptItm.testMethod) == 'undefined' || (scriptItm.testMethod.length == 0)) { skipTest = true; } if (skipTest) { // there is no test for this item so we will and assume // all is fine/ scriptItm.loaded = true; lib.addJS_Node(scriptItm.text, scriptItm.src); // trigger event for loaded //jQuery(document).trigger("bbScriptLoaded", scriptItm); document.dispatchEvent(bbScriptLoadedEvent); return; } scriptItm.count++; var maxCount = scriptItm.timeout * 10; // multply by 10 to convert into 10th of seconds if (scriptItm.count > maxCount) { console.error('unable to load script, Aborting: ', scriptItm.src); return; } var testmethod; try { testmethod = eval(scriptItm.testMethod); } catch (e) { testmethod = undefined; } if (typeof(testmethod) == 'undefined') { if (!scriptItm.loaded) { scriptItm.loaded = true; lib.addJS_Node(scriptItm.text, scriptItm.src); } setTimeout(function() { lib.loadScript(scriptItm); }, 100); } else { // script item is loaded trigger an evert //jQuery(document).trigger("bbScriptLoaded", scriptItm); document.dispatchEvent(bbScriptLoadedEvent); } break; case 'css': if (typeof(scriptItm.tag) == 'undefined') { scriptItm.tag = 'body'; // timeout in seconds } lib.addCss_Node(scriptItm.src, scriptItm.tag); //jQuery(document).trigger("bbScriptLoaded", scriptItm); document.dispatchEvent(bbScriptLoadedEvent); break; case 'csslink': lib.addLink_Node(scriptItm.src); //jQuery(document).trigger("bbScriptLoaded", scriptItm); document.dispatchEvent(bbScriptLoadedEvent); break; default: // statements_def break; } } } // #endregion LoadScripts // #region BIGBYTE.USERSCRIPT.DOCUMENT Methods // gneric document related if (typeof(bbDoc.addJS_Node) == 'undefined') { bbDoc.addJS_Node = function(text, s_URL, funcToRun, runOnLoad) { var D = document; var scriptNode = D.createElement('script'); if (runOnLoad) { scriptNode.addEventListener("load", runOnLoad, false); } scriptNode.type = "text/javascript"; if (text) scriptNode.textContent = text; if (s_URL) scriptNode.src = s_URL; if (funcToRun) scriptNode.textContent = '(' + funcToRun.toString() + ')()'; var targ = D.getElementsByTagName('head')[0] || D.body || D.documentElement; targ.appendChild(scriptNode); }; } if (typeof(bbDoc.addJS_NodeToBody) == 'undefined') { bbDoc.addJS_NodeToBody = function(text, s_URL, funcToRun, runOnLoad) { var D = document; var scriptNode = D.createElement('script'); if (runOnLoad) { scriptNode.addEventListener("load", runOnLoad, false); } scriptNode.type = "text/javascript"; if (text) scriptNode.textContent = text; if (s_URL) scriptNode.src = s_URL; if (funcToRun) scriptNode.textContent = '(' + funcToRun.toString() + ')()'; var targ = D.getElementsByTagName('body')[0] || D.body || D.documentElement; targ.appendChild(scriptNode); }; } if (typeof(bbDoc.addCss_Node) == 'undefined') { bbDoc.addCss_Node = function(text, element) { element = typeof element !== 'undefined' ? element : 'head'; var D = document; var scriptNode = D.createElement('style'); scriptNode.type = "text/css"; if (text) scriptNode.textContent = text; var targ = D.getElementsByTagName(element)[0] || D.body || D.documentElement; targ.appendChild(scriptNode); }; } if (typeof(bbDoc.addLink_Node) == 'undefined') { bbDoc.addLink_Node = function(href, type, rel) { type = typeof type !== 'undefined' ? type : "text/css"; rel = typeof rel !== 'undefined' ? rel : "stylesheet"; var D = document; var scriptNode = D.createElement('link'); scriptNode.type = type; scriptNode.href = href; if (rel) scriptNode.rel = rel; var targ = D.getElementsByTagName('head')[0] || D.body || D.documentElement; targ.appendChild(scriptNode); }; } if (typeof(bbDoc.addHtml_Node) == 'undefined') { bbDoc.addHtml_Node = function(html) { var D = document; var targ = D.getElementsByTagName('body')[0] || D.body || D.documentElement; targ.insertAdjacentHTML('beforeend', html); }; } // #endregion BIGBYTE.USERSCRIPT.DOCUMENT Methods // #nsregion BIGBYTE.USERSCRIPT.UTIL var bbusu = BIGBYTE.createNS("BIGBYTE.USERSCRIPT.UTIL"); bbusu.ns = 'BIGBYTE.USERSCRIPT.UTIL'; // #region Methods if(typeof(bbusu.extend) == 'undefined') { /** * Extends an object to contain new Properties * @return {[Object]} the new merged oject */ bbusu.extend = function () { for (var i = 1; i < arguments.length; i++) for (var key in arguments[i]) if (arguments[i].hasOwnProperty(key)) arguments[0][key] = arguments[i][key]; return arguments[0]; }; } // #endregion Methods // #endnsregion BIGBYTE.USERSCRIPT.UTIL // #endnsregion BIGBYTE.USERSCRIPT.DOCUMENT" // #nsregion BIGBYTE.USERSCRIPT.EVERNOTE // #region Create NS var enus = BIGBYTE.createNS("BIGBYTE.USERSCRIPT.EVERNOTE"); enus.ns = 'BIGBYTE.USERSCRIPT.EVERNOTE'; // #endregion Create ns // #region Properties enus.btnSelector = ''; enus.iframeSelector = ''; enus.sidebarSelector = ''; enus.noteSelector = ''; enus.fullScreen = false; // light box related enus.lightBoxCss = '.gmbackdrop,.gmbox{position:absolute;display:none}.gmbackdrop{top:0;left:0;width:100%;height:100%;background:#000;opacity:0;'; enus.lightBoxCss += 'filter:alpha(opacity=0);z-index:201}.gmbox{background:#fff;z-index:202;padding:10px;'; enus.lightBoxCss += '-webkit-border-radius:5px;-moz-border-radius:5px;border-radius:5px;-moz-box-shadow:0 0 5px #444;-webkit-box-shadow:0 0 5px #444;'; enus.lightBoxCss += 'box-shadow:0 0 5px #444}.gmclose{float:right;margin-right:6px;cursor:pointer}.mce-panel{border:none}div.gmbox .mce-panel{border:'; enus.lightBoxCss += ' 0 solid rgba(0,0,0,.2)}div.mce-tinymce.mce-container.mce-panel{margin-top:2em}div.mce-tinymce.mce-container.mce-panel.mce-fullscreen'; enus.lightBoxCss += '{margin-top:0}#gm-edit-btn{font-size:1.6em;color:#ABABAB;cursor:pointer;}#gm-edit-btn:hover{color:#2DBE60}'; enus.lightBoxCss += '.gmbox-window{top:50%;left:50%;transform: translate(-50%, -50%);position: absolute;'; enus.lightBoxCss += 'width:650px;height:450px;}#gm-tb{display:inline-block;position:absolute;}'; // #endregion Properties // #region Init /** * Init for the main script */ enus.init = function() { if (window.top != window.self) { // if this is an iframe then return return; } BIGBYTE.USERSCRIPT.EVERNOTE.TMCE.version = '4.1.0'; if(typeof(tinyMCE) != 'undefined') { BIGBYTE.USERSCRIPT.EVERNOTE.TMCE.version = tinyMCE.majorVersion + '.' + tinyMCE.minorVersion; } var tinyMceVer = BIGBYTE.USERSCRIPT.EVERNOTE.TMCE.version; console.log('tinyMCE Version', tinyMceVer); // var pluginSrc = '//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery-1.8.0.min.js'; var pluginSrc = 'https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js'; // no jquery at this point use pure javascript events if (document.addEventListener) { // For all major browsers, except IE 8 and earlier document.addEventListener("bbScriptLoaded", BIGBYTE.USERSCRIPT.EVERNOTE.onBbScriptLoaded); } else if (document.attachEvent) { // For IE 8 and earlier versions document.attachEvent("onbbScriptLoaded", BIGBYTE.USERSCRIPT.EVERNOTE.onBbScriptLoaded); } if (document.addEventListener) { // For all major browsers, except IE 8 and earlier document.addEventListener("allScriptsLoaded", BIGBYTE.USERSCRIPT.EVERNOTE.onAllScriptsLoaded); } else if (document.attachEvent) { // For IE 8 and earlier versions document.attachEvent("onallScriptsLoaded", BIGBYTE.USERSCRIPT.EVERNOTE.onAllScriptsLoaded); } // only add jquery if we need it. if (typeof(jQuery) == 'undefined') { this.addScript('jquery', pluginSrc, 'linkedjs', 'jQuery'); } this.addScript('icons-css', '//cdnjs.cloudflare.com/ajax/libs/foundicons/3.0.0/foundation-icons.css', 'csslink'); //this.addScript('code-css','shi/css/shi_default.min.css','csslink'); // tiny mce if (typeof(tinyMCE) == 'undefined') { this.addScript('tinyMceJs', '//cdnjs.cloudflare.com/ajax/libs/tinymce/' + tinyMceVer + '/tinymce.min.js', 'linkedjs', 'tinyMCE'); } this.addScript('tinyMceCss', '//cdnjs.cloudflare.com/ajax/libs/tinymce/' + tinyMceVer + '/skins/lightgray/skin.min.css', 'csslink'); this.addScript('lightboxcss', this.lightBoxCss, 'css',null, {tag:'body'}); //this.addScript('tinymce_advanced_theme', '//cdnjs.cloudflare.com/ajax/libs/tinymce/' + tinyMceVer + '/themes/advanced/theme.min.js','linkedjs') // no checking required this.loadScripts(); }; /* * Load scripts will load one script only at a time. * When the script is loaded the next if any script will be * load via the onScripLoadeEvent * the onScriptLoaded Event calle this function over and over untll all the scripts are loaded */ enus.loadScripts = function() { var count = 0; for (var key in this.scripts) { count++; if (count > 1) { return; } BIGBYTE.USERSCRIPT.DOCUMENT.loadScript(this.scripts[key]); } }; /* * Array to hold all the scripts that are to be loaded */ enus.scripts = []; /* * Adds script item to the BIGBYTE.USERSCRIPT.EVERNOTE.scripts array * these are scripts tha will be loaded when the BIGBYTE.USERSCRIPT.EVERNOTE.init() is fired */ enus.addScript = function(name, src, type, testMethod, args) { var newItm = { name: name, src: src, type: type, testMethod: testMethod }; if(typeof(args) === undefined) { this.scripts[name] = newItm; } else { var extended = BIGBYTE.USERSCRIPT.UTIL.extend(newItm, args); this.scripts[name] = extended; } }; /* * Function to check and see if there are any scripts left to be loaded * @returns boolean, true if all the scripts are loaded; Otherwise false */ enus.isScriptsLoaded = function() { var lib = BIGBYTE.USERSCRIPT.EVERNOTE; for (var key in lib.scripts) { if (!lib.scripts[key].loaded) { return false; } } return true; }; // #endregion init // #region events enus.onBbScriptLoaded = function(e) { var lib = BIGBYTE.USERSCRIPT.EVERNOTE; // delete the added script delete lib.scripts[e.detail.scriptItm.name]; var done = lib.isScriptsLoaded(); if (done) { var allScriptsLoaded = new CustomEvent( "allScriptsLoaded", { detail: { message: "All Scripts Loaded", time: new Date(), }, bubbles: true, cancelable: false } ); document.dispatchEvent(allScriptsLoaded); //jQuery(document).trigger("allScriptsLoaded"); } else { // add the next script lib.loadScripts(); } } /* * Event Handler that fires when all scripts are loaded * this is main loading point for the script. */ enus.onAllScriptsLoaded = function(e) { console.log('all scripts have been loaded.'); jQuery(function($, undefined) { var lib = BIGBYTE.USERSCRIPT.EVERNOTE; lib.btnSelector = '.GJDCG5CEMB'; // #en-common-editor-iframe is chrome selector, firefox is different //if ($.browser.chrome) { if(/chrom(e|ium)/.test(navigator.userAgent.toLowerCase())) { // setup for Chrome lib.iframeSelector = '#en-common-editor-iframe'; lib.noteSelector = 'body > div.en-note > #en-note'; } else { // setup for Firefox lib.iframeSelector = '#entinymce_486_ifr'; lib.noteSelector = 'body'; } lib.sidebarSelector = '#gwt-debug-sidebar'; lib.ensurePlugins(); lib.addToolbarButton(); $(document).on('editBtnAdded', lib.onEditBtnAdded); $(document).on("tinymceInit", lib.onTinymceInit); $(document).on("tinymceSave", lib.onTinymceSave); $(document).on("tinymceCancel", lib.onTinymceCancel); $(document).on('tinymceFullScreen', lib.onTinyMceFulllscreen); lib.lightBoxAddCss(); lib.writeLightBox(); lib.TMCE.init(); $('.gmclose').click(function() { $.event.trigger({ type: "tinymceCancel", message: 'cancel', time: new Date(), tinyMceId: 'gminput' }); }); }); }; enus.onEditBtnAdded = function(e) { console.log('onEditBtnAdded event fired'); var lib = BIGBYTE.USERSCRIPT.EVERNOTE; var $ = jQuery; lib.addButtonClick(); }; /** * Event that fire when TinyMce is initiated */ enus.onTinymceInit = function(e) { console.log('Tiny Mce Init was triggered'); }; /** * Event that fire when TinyMce save is clicked */ enus.onTinymceSave = function(e) { var lib = BIGBYTE.USERSCRIPT.EVERNOTE; if (e.tinyMceId == 'gminput') { //console.log('Tiny Mce save was triggered'); lib.save(); lib.lightBoxReset(); tinyMCE.get(e.tinyMceId).setContent(''); // clean up tinyMCE } }; /** * Event that fire when TinyMce close is clicked */ enus.onTinymceCancel = function(e) { var lib = BIGBYTE.USERSCRIPT.EVERNOTE; if (e.tinyMceId == 'gminput') { if (lib.confirmExit()) { lib.lightBoxReset(); tinyMCE.get(e.tinyMceId).setContent(''); // clean up tinyMCE } } }; enus.onTinyMceFulllscreen = function(e) { var $ = jQuery; var lib = BIGBYTE.USERSCRIPT.EVERNOTE; if (e.tinyMceId == 'gminput') { lib.fullScreen = e.state; if (e.state) { // remove the class that keeps the window centered if needed if ($('#tinybox').hasClass('gmbox-window')) { $('#tinybox').removeClass('gmbox-window'); } } else { // add the class that keeps the window centered if needed if (!$('#tinybox').hasClass('gmbox-window')) { $('#tinybox').addClass('gmbox-window'); } } } }; // #endregion events // #region methods enus.addToolbarButton = function() { var $ = jQuery; var lib = BIGBYTE.USERSCRIPT.EVERNOTE; var gmCounter = 0; var gmTimer = setInterval(function() { gmCounter++; console.log("turn no. " + gmCounter); var objLen = $(lib.btnSelector).length; if (objLen) { console.log('found class ', lib.btnSelector); // add my own toolbar button clearInterval(gmTimer); $(lib.btnSelector).append(lib.createToolbarHtml()); $.event.trigger({ type: "editBtnAdded", message: 'Button Added', time: new Date() }); } else { console.log('unable to find class ', lib.btnSelector); } if (gmCounter >= 20 || objLen > 0) { clearInterval(gmTimer); } }, 500); }; enus.addButtonClick = function() { var $ = jQuery; var lib = this; if ($('#gm-edit-btn').length) { $('#gm-edit-btn').click(function() { if (this.fullScreen) { tinyMCE.get('gminput').execCommand('mceFullScreen'); } tinyMCE.get('gminput').setContent($(lib.iframeSelector).contents().find(lib.noteSelector).html()); $('.gmbackdrop, .gmbox').animate({ 'opacity': '.50' }, 300, 'linear'); $('.gmbox').animate({ 'opacity': '1.00' }, 300, 'linear'); $('.gmbackdrop, .gmbox').css('display', 'block'); }); console.log('Edit Button Click added'); } }; enus.ensurePlugins = function() { var $ = jQuery; if (typeof($.fn.tagName) == 'undefined') { $.fn.tagName = function(toLower) { var tn = this.prop("tagName"); if (toLower) { tn = tn.toLowerCase(); } return tn; }; } }; enus.createToolbarHtml = function() { var btnHtml = this.createToolbarEditBtn(); var html = ''; html += '