AmourSpirit icon

evernoteedit.user.js 0.1 Released version

AmourSpirit | PRO | 12/25/15 11:43:50 AM UTC | 0 ⭐ | 545 👁️ | Never ⏰ | []
JavaScript |

13.18 KB

|

None

|

0 👍

/

0 👎

// ==UserScript==
// @name            TinyMce for Evernote
// @namespace       http://www.bigbytetech.ca/userscripts
// @version         0.1
// @description     Adds TinyMce in Evernote with custom options including source code. A new button is added to Evernote top toolbar section.
// @require         https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js
// @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     https://openuserjs.org/scripts/Amourspirit/TinyMce_for_Evernote
// @update          https://openuserjs.org/install/Amourspirit/TinyMce_for_Evernote.user.js
// ==/UserScript==
 
function GM_main () {
    // ensure top level window calling and not any iframes
    if (window.top != window.self) {
        return;
    }
    var gmPageInitialized = false;
    var gmTimer = null;
    var gmCounter = 0;
    
    window.onload = function(){
        if(gmPageInitialized) return;
        gmPageInitialized = true;
        // do not append to main edit window (window.top.frames[4].document) as this cause evernote to auto save the page
        // this results in a loop and page continues to call document ready event
        
        // set up timer to find evernote toolbar when it appears
        gmTimer = setInterval(function () {
            gmCounter++;
            console.log("turn no. " + gmCounter);
            var objLen = jQuery('.GOSDSN-CFFB').length;
            if(objLen) {
                console.log('found class GOSDSN-CFFB');
                // add my own toolbar button
                clearInterval(gmTimer);
                jQuery('.GOSDSN-CFFB').prepend(createToolbarHtml());
                
                addButtonClick();
            } else {
                console.log('unable to find class GOSDSN-CFFB');
            }
 
            if (gmCounter >= 20 || objLen > 0) {
                clearInterval(gmTimer);
            }
 
 
        }, 500);
    }
    
    function addButtonClick() {
        if(jQuery('#gm-edit-btn').length) {
            jQuery('#gm-edit-btn').click(function (){
            if(gmFullscreen)
            {
                tinyMCE.get('gminput').execCommand('mceFullScreen');    
            }
            tinymce.get('gminput').setContent(jQuery('#entinymce_170_ifr').contents().find('body').html());
            jQuery('.gmbackdrop, .gmbox').animate({'opacity':'.50'}, 300, 'linear');
            jQuery('.gmbox').animate({'opacity':'1.00'}, 300, 'linear');
            jQuery('.gmbackdrop, .gmbox').css('display', 'block');
            });
            console.log('Edit Button Click added');
        }
    }
    
    function createToolbarHtml() {
        var btnHtml = createToolbarEditBtn();
        var html = '';
        html += '<div id="gm-tb" title="Edit with TinyMCE" class="GOSDSN-CGGB">'+btnHtml+'</div>';
        return html;
    }
 
    function createToolbarEditBtn() {
        var html = '<div id="gm-edit-btn" name="gm-edit-btn" class="gm-btn"><i class="fi-page-edit"></i></div>';
        return html;
    }
}
 
function getCss() {
 return '.gmbackdrop,.gmbox{position:absolute;display:none}.gmbackdrop{top:0;left:0;width:100%;height:100%;background:#000;opacity:0;filter:alpha(opacity=0);z-index:201}.gmbox{top:20%;left:30%;width:650px;height:450px;background:#fff;z-index:202;padding:10px;-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;box-shadow:0 0 5px #444}.gmclose{float:right;margin-right:6px;cursor:pointer}.mce-panel{border:none}div.gmbox .mce-panel{border: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{margin-top:0}#gm-edit-btn{font-size:1.6em;color:#ABABAB}#gm-edit-btn:hover{color:#2DBE60}';
}
 
// function to add lightbox divs to page
function getLightBoxHtml(id, title) {
    id = typeof id !== 'undefined' ? id : 'gminput';
    title = typeof title !== 'undefined' ? title : '';
    var h = '<div class="gmbackdrop"></div>';
    h += '<div class="gmbox"><div class="gmclose"><i class="fi-x"></i></div>';
    h += title;
    h += '<textarea id="'+ id +'" rows="18" cols="68"></textarea>';
    h += '</div></div>';
    return h;
}
 
// ensure the script are being added to only the main window and not the iframes
if (window.top == window.self)
{
    addJS_Node (null, null, GM_main);
    addCss_Node(getCss(),'body');
    addLink_Node('https://cdnjs.cloudflare.com/ajax/libs/foundicons/3.0.0/foundation-icons.css');
    addLink_Node('https://cdnjs.cloudflare.com/ajax/libs/tinymce/4.3.2/skins/lightgray/skin.min.css');
    addHtml_Node(getLightBoxHtml());
    addJS_NodeToBody(getTinyMceInit());
    addJS_NodeToBody(getSaveAndCloseCode());
}
 
//-- This is a standard-ish utility function:
function addJS_Node (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);
}
 
function addJS_NodeToBody (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);
}
 
//-- This is a standard-ish utility function:
function addCss_Node (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);
}
 
function addLink_Node(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);
}
 
function addHtml_Node(html) {
    var D = document;
    var targ = D.getElementsByTagName ('body')[0] || D.body || D.documentElement;
    targ.insertAdjacentHTML('beforeend', html);
}
 
function getTinyMceInit(ver){
    ver = typeof ver !== 'undefined' ? ver : '4.1.0';
    var script = 'function myCustomInitInstance(i){jQuery(".mce-i-mysave").addClass("fi-save"),jQuery(".mce-i-myexit").addClass("fi-x")}var gmFullscreen=!1,gmTinyMceTimer=null,gmTinyMceTimerCounter=0;jQuery(document).ready(function(){window.top==window.self&&(gmTinyMceTimer=setInterval(function(){return gmTinyMceTimerCounter++,console.log("turn no. "+gmTinyMceTimerCounter+" looking for tinymce"),void 0!==typeof tinymce?(console.log("found tinymce library"),clearInterval(gmTinyMceTimer),void tinymce.init({selector:"textarea#gminput",init_instance_callback:"myCustomInitInstance",height:260,inline:!1,browser_spellcheck:!0,plugins:"",menubar:"edit insert format view tools table",toolbar1:"mysave myexit | insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent",toolbar2:"print preview media | forecolor backcolor | link image | insertdatetime table emoticons | code fullscreen",external_plugins:{fullscreen:"https://cdnjs.cloudflare.com/ajax/libs/tinymce/'+ver+'/plugins/fullscreen/plugin.min.js",textcolor:"https://cdnjs.cloudflare.com/ajax/libs/tinymce/'+ver+'/plugins/textcolor/plugin.min.js",nonbreaking:"https://cdnjs.cloudflare.com/ajax/libs/tinymce/'+ver+'/plugins/nonbreaking/plugin.min.js",code:"https://cdnjs.cloudflare.com/ajax/libs/tinymce/'+ver+'/plugins/code/plugin.min.js",hr:"https://cdnjs.cloudflare.com/ajax/libs/tinymce/'+ver+'/plugins/hr/plugin.min.js",searchreplace:"https://cdnjs.cloudflare.com/ajax/libs/tinymce/'+ver+'/plugins/searchreplace/plugin.min.js",table:"https://cdnjs.cloudflare.com/ajax/libs/tinymce/'+ver+'/plugins/table/plugin.min.js",list:"https://cdnjs.cloudflare.com/ajax/libs/tinymce/'+ver+'/plugins/lists/plugin.min.js",advlist:"https://cdnjs.cloudflare.com/ajax/libs/tinymce/'+ver+'/plugins/advlist/plugin.min.js",paste:"https://cdnjs.cloudflare.com/ajax/libs/tinymce/'+ver+'/plugins/paste/plugin.min.js",link:"https://cdnjs.cloudflare.com/ajax/libs/tinymce/'+ver+'/plugins/link/plugin.min.js",autolink:"https://cdnjs.cloudflare.com/ajax/libs/tinymce/'+ver+'/plugins/autolink/plugin.min.js",charmap:"https://cdnjs.cloudflare.com/ajax/libs/tinymce/'+ver+'/plugins/charmap/plugin.min.js",insertdatetime:"https://cdnjs.cloudflare.com/ajax/libs/tinymce/'+ver+'/plugins/insertdatetime/plugin.min.js",image:"https://cdnjs.cloudflare.com/ajax/libs/tinymce/'+ver+'/plugins/image/plugin.min.js",visualblocks:"https://cdnjs.cloudflare.com/ajax/libs/tinymce/'+ver+'/plugins/visualblocks/plugin.min.js",emoticons:"https://cdnjs.cloudflare.com/ajax/libs/tinymce/'+ver+'/plugins/emoticons/plugin.min.js"},setup:function(i){i.on("FullscreenStateChanged",function(i){gmFullscreen=i.state,i.state?jQuery("#gwt-debug-sidebar").hide():jQuery("#gwt-debug-sidebar").show()}),i.addButton("mysave",{title:"Save",onclick:function(){save_box()}}),i.addButton("myexit",{title:"Close",onclick:function(){jQuery(".gmclose").trigger("click")}})},content_css:"https://www.evernote.com/js/tinymce/skins/lightgray/content.min.css",content_style:\'a,blockquote,body,code,dd,del,dfn,div,dl,dt,em,h1,h2,h3,h4,h5,h6,html,iframe,img,li,ol,p,pre,q,ul{border:0;padding:0;margin:0}a,abbr,acronym,address,area,b,bdo,big,blockquote,caption,center,cite,code,col,colgroup,dd,del,dfn,div,dl,dt,em,font,h3,h4,h5,h6,hr,i,ins,kbd,li,map,ol,p,pre,q,s,samp,small,span,strike,strong,sub,sup,table,tbody,td,tfoot,th,thead,tr,tt,u,ul{line-height:1.57143em}a,body{margin:0}body,h1,h2{font-family:gotham,helvetica,arial,sans-serif}a,img[name=en-crypt]{cursor:pointer}h3,p{margin-bottom:.714285em}del{text-decoration:line-through}dfn{font-style:italic}body{box-sizing:border-box;color:#383838;font-size:14px;padding-right:1px;word-wrap:break-word}a:link,a:visited{color:#047ac6}a:active,a:hover{color:#2596de}h1{font-size:1.5em;font-weight:700;line-height:1.04762em;margin-bottom:.4761em;margin-top:.9523em}h2{font-size:1.286em;font-weight:700;line-height:1.22222em;margin-bottom:.5556em;margin-top:1.111em}h3,h4,h5,h6{font-size:1em;font-weight:700;font-family:gotham,helvetica,arial,sans-serif}h3{margin-top:1.4285em}div{font-family:gotham,helvetica,arial,sans-serif;font-size:14px}img.en-media{height:auto;margin-bottom:1.286em;max-width:100%}img.en-media[height="1"]{height:1px}p+div img,p+img{margin-top:.714285em}div+div img,div+img{margin-top:.857412em}div+div img+img,img+img,li ol,li ul{margin-top:0}ol,ul{list-style-position:outside;margin-bottom:.714285em;margin-left:2em;margin-top:.2857em;padding-left:0}li ol,li ul{margin-bottom:0}h1+ol,h1+ul,h2+ol,h2+ul,p+ol,p+ul{margin-top:-.428571em}blockquote{border-left:2px solid #bfbfbf;margin-bottom:1.4285em;margin-left:1.4285em;margin-top:1.4285em;padding-left:.714285em}code,pre{font-family:Monaco,Courier,monospace}cite{font-style:italic}table{font-size:1em}td,th{padding:.2em 2em .2em 0;text-align:left;vertical-align:top}button.en-ignore{margin-bottom:1em}.highlight{background:#c9f2d0;border:1px solid #62eb92}.Decrypted{background-color:#f7f7f7;padding:5px}.Decrypted .Header{color:#404040;font-family:gotham,helvetica,arial,sans-serif;font-size:11px;padding-bottom:5px}.Decrypted .Body{background-color:#fff;padding:5px}.canvas-container{background:url(/redesign/global/img/loading-spinner.gif) center center no-repeat #fff;border:1px solid #cacaca;margin-bottom:10px}\'})):void(gmTinyMceTimerCounter>=20&&clearInterval(gmTinyMceTimer))},500))});';
 
    return script;
}
function getSaveAndCloseCode() {
return 'function close_box(){return confirm("Are you sure you want to close this editor?")?(jQuery(".gmbackdrop, .gmbox").animate({opacity:"0"},300,"linear",function(){jQuery(".gmbackdrop, .gmbox").css("display","none")}),jQuery("textarea#gminput").val(""),tinymce.get("gminput").setContent(""),jQuery("#gwt-debug-sidebar").show(),!0):!1}function save_box(){var e=tinymce.get("gminput").getContent();jQuery(".gmbackdrop, .gmbox").animate({opacity:"0"},300,"linear",function(){jQuery(".gmbackdrop, .gmbox").css("display","none")}),jQuery("#entinymce_170_ifr").contents().find("body").html(e),jQuery("textarea#gminput").val(""),tinymce.get("gminput").setContent(""),jQuery("#gwt-debug-sidebar").show()}jQuery(document).ready(function(){window.top==window.self&&jQuery(".gmclose").click(function(){close_box()})});';   
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎