vlada79 / NetSuite CodeMirror code editor

// ==UserScript==
// @name         NetSuite CodeMirror code editor
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  CodeMirror editor for NetSuite with sane defaults (start in CM vim mode, expand tabs, etc)
// @author       You
// @match        https://*.app.netsuite.com/app/common/record/edittextmediaitem.nl*
// @exclude      *://*/*syntaxHighlighting=T*
// @grant        none
// @run-at       document-body
// @license      MIT
// @copyright 2019, vlada79 (https://openuserjs.org/users/vlada79)
// @updateURL https://openuserjs.org/meta/vlada79/NetSuite_CodeMirror_code_editor.meta.js
// ==/UserScript==

(function() {
    'use strict';
    if (typeof(jQuery) == "undefined") return;

    jQuery('head').append(
        '<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.46.0/codemirror.css">' +
        '<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.46.0/addon/dialog/dialog.css">' +
        '<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.46.0/theme/midnight.css">' +
        '<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.46.0/theme/mbo.css">' +
        '<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.46.0/theme/solarized.css">');

    jQuery.getScript("https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.46.0/codemirror.js", () => {
        jQuery.when(
            jQuery.getScript("https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.46.0/addon/dialog/dialog.js"),
            jQuery.getScript("https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.46.0/addon/search/searchcursor.js"),
            jQuery.getScript("https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.46.0/mode/clike/clike.js"),
            jQuery.getScript("https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.46.0/addon/edit/matchbrackets.js"),
            jQuery.getScript("https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.46.0/keymap/vim.js")
        ).done(() => {
            var editor = CodeMirror.fromTextArea(document.getElementsByName("mCharData")[0], {
                lineNumbers: true,
                mode: "text/x-csrc",
                keyMap: "vim",
                matchBrackets: true,
                showCursorWhenSelecting: true,
                theme: 'midnight',
                indentWithTabs: false,
            });

            editor.setOption("extraKeys", {
                Tab: function(cm) {
                    var spaces = Array(cm.getOption("indentUnit") + 1).join(" ");
                    cm.replaceSelection(spaces);
                }
            });

            // Provide save function
            CodeMirror.commands.save = () => jQuery('input[type=submit]').click();

            // Remove surplus UI
            jQuery('.uir-page-title').hide();
            jQuery('.uir-header-buttons').hide();
            jQuery('.uir-label').hide();
            jQuery('.uir-secondary-buttons').hide();

            // Resize and focus editor
            editor.setSize(jQuery(window).width() - 15, jQuery(window).height() - 25);
            editor.focus();
        });
    });

    jQuery('body').append('<iframe style="display:none" name="hidden-form" src="about:blank"></iframe>');
    jQuery('form').attr('target', 'hidden-form'); // background submit to iframe
    jQuery('form').attr('onsubmit', ''); // turn off validations

    // Set window title
    document.title = jQuery('.uir-record-name').text();
})();